首页 新闻 搜索 专区 学院

WCF服务客户端调用很慢

0
悬赏园豆:20 [已关闭问题] 关闭于 2014-12-16 10:15

各位大侠,我的WinForm的客户端在调用WCF服务的时候发现,就算函数返回一个很小的数,也很慢。我打了一下断点跟踪了一下,发现是在客户端代理调用WCF服务端函数的时候,要很长一段时间才能从客户端转到服务端,就像客户端调用的函数要找到服务端的服务很费劲一样,但是从服务端返回却很快,不知道是怎么回事

fanshisuiyuan的主页 fanshisuiyuan | 初学一级 | 园豆:102
提问于:2014-12-11 12:57
< >
分享
所有回答(2)
0

很长一段时间是几年?

爱编程的大叔 | 园豆:30753 (高人七级) | 2014-12-11 13:48

一两秒钟

支持(0) 反对(0) fanshisuiyuan | 园豆:102 (初学一级) | 2014-12-11 15:32
0

贴上代码,给你分析

田麦成 | 园豆:1988 (小虾三级) | 2014-12-11 14:18

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);
}
}

支持(0) 反对(0) fanshisuiyuan | 园豆:102 (初学一级) | 2014-12-11 15:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册