各位大侠,我的WinForm的客户端在调用WCF服务的时候发现,就算函数返回一个很小的数,也很慢。我打了一下断点跟踪了一下,发现是在客户端代理调用WCF服务端函数的时候,要很长一段时间才能从客户端转到服务端,就像客户端调用的函数要找到服务端的服务很费劲一样,但是从服务端返回却很快,不知道是怎么回事
很长一段时间是几年?
一两秒钟
贴上代码,给你分析
private void initDB()
{
try
{
if (serviceHosts != null)
{
foreach (ServiceHost t in serviceHosts)
{
if (t != null)
t.Close();
}
serviceHosts.Clear();
}
else
{
serviceHosts = new List<ServiceHost>();
}
string serviceAddress = string.Format("net.tcp://{0}:{1}", Dns.GetHostByName(Dns.GetHostName()).AddressList[0].ToString(),
"8090");
//string serviceAddress = string.Format("net.tcp://{0}:{1}", "192.168.1.10",
// "8090");
string endpointAddress = string.Empty;
Type[] types = new[]
{
typeof(IService1) ,typeof (IService2),typeof(IService3),typeof(IService4)
};
NetTcpBinding netTcpBinding = null;
ServiceMetadataBehavior smb = null;
System.ServiceModel.Channels.Binding mexBinding = null;
string tName = string.Empty;
foreach (Type t in types)
{
tName = t.Name.Substring(1);
endpointAddress = serviceAddress + tName;
if (!serviceAddress.EndsWith("/"))
{
endpointAddress = string.Format("{0}/{1}", serviceAddress, tName);
}
serviceHost = new ServiceHost(typeof(Service1), new Uri(endpointAddress));
//加载元数据结点
smb = new ServiceMetadataBehavior { HttpGetEnabled = false };
serviceHost.Description.Behaviors.Add(smb);
serviceHost.AddServiceEndpoint(typeof(IMetadataExchange),
MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
netTcpBinding = new NetTcpBinding();
netTcpBinding.Security.Mode = SecurityMode.None;
netTcpBinding.ReceiveTimeout = TimeSpan.Parse("00:30:00");
netTcpBinding.MaxBufferPoolSize = 2147483647;
netTcpBinding.MaxBufferSize = 2147483647;
netTcpBinding.MaxConnections = 10;
netTcpBinding.ReaderQuotas.MaxDepth = 2147483647;
netTcpBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
netTcpBinding.ReaderQuotas.MaxArrayLength = 2147483647;
netTcpBinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
netTcpBinding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
netTcpBinding.MaxReceivedMessageSize = 2147483647;
serviceHost.AddServiceEndpoint(t, netTcpBinding, endpointAddress);
ServiceDebugBehavior serviceDebug = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
if (serviceDebug == null)
{
serviceDebug = new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true };
serviceHost.Description.Behaviors.Add(serviceDebug);
}
else
{
serviceDebug.IncludeExceptionDetailInFaults = true;
}
serviceHost.Open();
serviceHosts.Add(serviceHost);
}
string[] s;
//s = System.Configuration.ConfigurationSettings.AppSettings.GetValues("SqlconString");
s = ConfigurationManager.AppSettings.GetValues("SqlconString");
if (s.Count() > 0)
sqlstring = s[0];
s = sqlstring.Split(';');
textBox2.Text = s[0].Substring(7);
textBox3.Text = s[1].Substring(4);
textBox4.Text = s[2].Substring(4);
SqlDBHelper.conString = sqlstring;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}