WCF宿主WINFORM多客户端操作变慢
偉Vae 2016-07-01 09:07:01 使用WINFORM作为WCF宿主,使用两个客户端同时查询表的时候查询速度会变慢,WINFORM宿主界面会无响应,查找不到问题原因,请教各位大神们帮忙
附上设计代码:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="MyTcpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<services>
<service behaviorConfiguration="MyBehavior" name="VCFService.Service1">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8081//"/>
</baseAddresses>
</host>
<endpoint address="S1" binding="netTcpBinding" bindingConfiguration="MyTcpBinding" contract="VCFService.IService1"></endpoint>
<endpoint address="mex111" binding="netTcpBinding" contract="IMetadataExchange" bindingConfiguration="MyTcpBinding" ></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="6553600"/>
</behavior>
<behavior name="ThrottledBehavior">
<serviceThrottling maxConcurrentCalls="5000" maxConcurrentSessions="5000" maxConcurrentInstances="5000" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" />
</system.webServer>
<runtime>
<gcServer enabled="true" />
</runtime>
<appSettings>
<add key="server" value="127.0.0.1" />
<add key="DataBaseName" value="dsportmis" />
<add key="DBUserId" value="sa" />
<add key="DBPassWord" value="12345" />
</appSettings>
</configuration>
服务端源码:
const int SleepTime = 100;
public ServiceHost _serviceHost = null;
private Thread _thread;
private bool _isRunning;
public ThreadServiceHost(Type serviceType)
{
try
{
_serviceHost = new ServiceHost(serviceType);
_thread = new Thread(RunService);
_thread.Start();
}
catch (System.Exception ex)
{
ShwMsgForView.ShwMsgforView(Comm.memoEdit, "启动服务失败......." + ex.ToString());
}
}
void RunService()
{
try
{
_serviceHost.Open();
_isRunning = true;
VCFService.ShwMsgForView.ShwMsgforView(Comm.memoEdit, "服务启动完成.......");
//while (_isRunning)
//{
// Thread.Sleep(SleepTime);
//}
//_serviceHost.Close();
//((IDisposable)_serviceHost).Dispose();
}
catch (Exception ex)
{
if (_serviceHost != null)
_serviceHost.Abort();
ShwMsgForView.ShwMsgforView(Comm.memoEdit, "启动服务失败......." + ex.ToString());
//throw ex;
}
}
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, UseSynchronizationContext = false, ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1 : IService1,IDisposable
{
public string GetXml(CurUser user, string DtSql)
{
Thread.Sleep(100);
Func<CurUser,string, string> method = GetXml1;
IAsyncResult cookie = method.BeginInvoke(user, DtSql, null, null);
ShwMsgForView.ShwMsgforView(Comm.memoEdit, DtSql);
string result = method.EndInvoke(cookie);
return result;
}
public string GetXml1(CurUser user, string DtSql)
{
using (DbHelper dbhelper = new DbHelper())
{
Thread.Sleep(100);
return dbhelper.GetXml(user, DtSql);
}
}
public string GetXml(CurUser user, string DtSql)
{
try
{
DataSet dtResult;
string strResult;
StringBuilder strSql = new StringBuilder();
strSql.Append(DtSql);
dtResult = GetDataSetByProc(user, CommandType.Text, strSql.ToString());
return strResult=XmlUtil.Serializer(typeof(DataTable), dtResult.Tables[0]);
}
catch (System.Exception ex)
{
Comm.OuntPutLog("检索数据'" + DtSql + "'发生错误:" + ex.ToString());
throw new FaultException(ex.ToString());
}
}
public DataSet GetDataSetByProc(CurUser user, CommandType type, string procName, params SqlParameter[] parameters)//在用,查询表
{
if (user == null)
{
user = new CurUser();
}
ShwMsgForView.ShwMsgforView(Comm.memoEdit, user.UserName + "(" + user.IPAddress + ")" + "用户执行:" + procName);
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = procName;
cmd.CommandType = type;
cmd.CommandTimeout = timeout;
cmd.Parameters.AddRange(parameters);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataSet ds = new DataSet())
{
try
{
da.Fill(ds);
conn.Close();
return ds;
}
catch (System.Exception ex)
{
throw new FaultException(ex.ToString());
}
}
}
}
}
}
public class ShwMsgForView
{
delegate void ShwMsgforViewCallBack(DevExpress.XtraEditors.MemoEdit memoEdit1, string text);
public static void ShwMsgforView(DevExpress.XtraEditors.MemoEdit memoEdit1, string text)
{
Comm.OuntPutLog(text);
if (memoEdit1==null)
{
return;
}
if (memoEdit1.InvokeRequired)
{
ShwMsgforViewCallBack shwMsgforViewCallBack = ShwMsgforView;
memoEdit1.Invoke(shwMsgforViewCallBack, new object[] { memoEdit1, text });
}
else
{
string Strlog = "\r\n===========" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "===========\r\n" + text + "\r\n";
memoEdit1.Text = Strlog + memoEdit1.Text;
if (memoEdit1.Lines.Length>=5000)
{
memoEdit1.Text = "";
}
}
}
}
public static void OuntPutLog(string text, Boolean IsOpen = false)//操作日志,最新日志放在最前面
{
try
{
string Strlog = "\r\n===========" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "===========\r\n" + text + "\r\n";
string strFilePath = sPath + "\\Log\\OptLog" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
File.AppendAllText(strFilePath, "", System.Text.Encoding.Default);
string strOldText = File.ReadAllText(strFilePath, System.Text.Encoding.Default);
File.WriteAllText(strFilePath, Strlog, System.Text.Encoding.Default);
File.AppendAllText(strFilePath, strOldText, System.Text.Encoding.Default);
if (IsOpen)
{
try
{
Process p = new Process();
p.StartInfo.FileName = strFilePath;
p.Start();
}
catch (System.Exception ex)
{
throw new FaultException(ex.ToString());
}
}
}
catch (System.Exception ex)
{
//ErrorLogWrite("写操作日志错误:" + ex.ToString());
}
}