WCF服务器自动连接到客户端当连接被中止
问题描述:
我在我的WindowsApplication中使用WCF服务...当我运行应用程序服务器和客户端时,服务器在几分钟内断开连接....应该如何我自动重新连接客户端连接时被中止....WCF服务器自动连接到客户端当连接被中止
这是我的客户端代码:
public void connecttoserver()
{
D:
try
{
EndpointAddress ea = new EndpointAddress(@"net.tcp://10.0.3.33:2222/ClsPCMain");
EndpointAddress ea = new EndpointAddress(StrAddress);
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, false);
binding.MaxBufferPoolSize = Int32.MaxValue;
binding.MaxReceivedMessageSize = Int32.MaxValue;
binding.PortSharingEnabled = true;
binding.ReceiveTimeout = TimeSpan.MaxValue;
binding.SendTimeout = TimeSpan.MaxValue;
binding.OpenTimeout = TimeSpan.MaxValue;
binding.CloseTimeout = TimeSpan.MaxValue;
binding.MaxReceivedMessageSize = Int32.MaxValue;
binding.MaxBufferPoolSize = Int32.MaxValue;
binding.MaxConnections = Int16.MaxValue;
binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
binding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;
binding.ReaderQuotas.MaxDepth = Int32.MaxValue;
binding.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
binding.Security.Mode = SecurityMode.None;
ChannelFactory<InterfaceClass.IService> Client = new ChannelFactory<InterfaceClass.IService>(binding,ea);
InterfaceClass.IService serviceobj = Client.CreateChannel(ea);
clsStatus.connectstatus = false;
ClsPC objclsPc = serviceobj.PCInfoMethod(Environment.UserName, Environment.UserDomainName, Dns.GetHostName(), Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString());
if (objclsPc.imageid == 1)
{
clsStatus.FullSizeImage = true;
clsStatus.ThumbnailImage = false;
}
else
{
clsStatus.ThumbnailImage = true;
clsStatus.FullSizeImage = false;
}
Client.Close();
Client=null;
//serviceobj = null;
}
catch (Exception ex)
{
logobj.Write(ex);
}}
这是我的服务器代码:
public clsHostService()
{
string StrAddress = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "url2.txt");
ServiceHost host = new ServiceHost(typeof(clsService));
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None, false);
ServiceEndpoint endpointinfo = host.AddServiceEndpoint(typeof(IService), binding, StrAddress);
endpointinfo.Binding.CloseTimeout = TimeSpan.MaxValue;
endpointinfo.Binding.OpenTimeout = TimeSpan.MaxValue;
endpointinfo.Binding.ReceiveTimeout = TimeSpan.MaxValue;
endpointinfo.Binding.SendTimeout = TimeSpan.MaxValue;
XmlDictionaryReaderQuotas BindingQuota = binding.ReaderQuotas;
BindingQuota.MaxArrayLength = Int32.MaxValue;
BindingQuota.MaxBytesPerRead = Int32.MaxValue;
BindingQuota.MaxDepth = Int32.MaxValue;
binding.MaxConnections = Int16.MaxValue;
binding.MaxBufferPoolSize = Int32.MaxValue;
binding.MaxBufferSize = Int32.MaxValue;
binding.MaxReceivedMessageSize = Int32.MaxValue;
binding.CloseTimeout = TimeSpan.MaxValue;
binding.OpenTimeout = TimeSpan.MaxValue;
binding.ReceiveTimeout = TimeSpan.MaxValue;
binding.SendTimeout = TimeSpan.MaxValue;
ServiceThrottlingBehavior throttlingBehavior =new ServiceThrottlingBehavior();
throttlingBehavior.MaxConcurrentCalls = Int32.MaxValue;
throttlingBehavior.MaxConcurrentInstances = Int32.MaxValue;
throttlingBehavior.MaxConcurrentSessions = Int32.MaxValue;
host.Description.Behaviors.Add(throttlingBehavior);
host.Open();
Console.WriteLine("Server Started");
Console.ReadLine();
}
现在我该如何自动连接到客户端当服务器切断连接? 任何人告诉我这个问题的解决方案... 在此先感谢.....
答
我不完全理解你的问题,我怕 - 你的Winforms应用程序托管的服务,还是它客户端调用WCF服务?
WCF通常不会使用客户端和服务器之间具有恒定连接的概念。
客户端构建一个客户端代理,在该客户端代理上调用服务器公开的方法。基本上,每个呼叫都独立于所有其他呼叫 - 在呼叫期间,只有客户端和服务器之间存在连接。连接不是总是上去 - 只有在实际发生呼叫时才有。
所以我不完全明白你想要“重新连接” - 首先并不总是开启连接。
可能发生的情况是,如果在服务器端发生异常并且未被正确捕获和处理,则客户端代理可能会失效。在WCF术语中,客户端和服务器之间的“通道”已经“故障”,例如,已经变得无法使用。如果您使用处于故障状态的客户端代理再次调用服务器,则会收到客户端异常。
您可以拨打电话与此代码之前检查在客户端代理有故障的信道状态:
if(client.State == CommunicationState.Faulted)
{
client = new YourServiceClient();
}
如果通道确实出现故障,则需要再次重新创建代理你应该恢复营业。
答
我用的是这样的:
//Somewhere in the main
ConfigureWcf();
ConnectToServer();
//...
void ConnectToServer()
{
myService = new ServiceReference.ServiceClient(context);
myService.Open();
myService.InnerChannel.UnknownMessageReceived += InnerChannel_UnknownMessageReceived;
myService.InnerChannel.Closed += InnerChannel_Closed;
}
void StartConnecting()
{
//use 5 attempts to connect to server
ConnectToServer();
}
void InnerChannel_Closing(object sender, EventArgs e)
{
//Connection to server closed!
//Write to log
StartConnecting();
}