我有一个示例服务来测试WCF net.tcp通信。这是一个非常简单的服务,它所做的只是订阅客户端到服务,然后调用callbackchannel来通知所有连接的客户端有关广播的消息。该服务托管在IIS 7.5中。
这是服务代码和测试客户端来测试它。
[ServiceContract(CallbackContract = typeof(ISampleServiceCallBack), SessionMode = SessionMode.Required)]
public interface ISampleCuratioService
{
[OperationContract(IsOneWay = true)]
void SubcribeToService(string sub);
[OperationContract]
string GetData(int value);
[OperationContract(IsOneWay = true)]
void Broadcast(string message);
}
public interface ISampleServiceCallBack
{
[OperationContract(IsOneWay = true)]
void NotifyClient(string message);
}
以下是服务实施:
[ServiceBehavior(Name = "CuratioCSMService", InstanceContextMode = InstanceContextMode.PerSession)]
public class Service1 : ISampleCuratioService
{
private static List<ISampleServiceCallBack> JoinedClien = new List<ISampleServiceCallBack>();
public void SubcribeToService(string sub)
{
var subscriber = OperationContext.Current.GetCallbackChannel<ISampleServiceCallBack>();
if (!JoinedClien.Contains(subscriber))
{
JoinedClien.Add(subscriber);
}
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public void Broadcast(string message)
{
JoinedClien.ForEach(c => c.NotifyClient("message was received " + message));
}
}
我无法理解运行它时的行为。在第一个客户端运行后,一切正常,但当我关闭并打开测试客户端应用程序时,它会抛出异常,通知该通道不能用于通信,因为它处于故障状态。
这是示例测试客户端:
static void Main(string[] args)
{
var callneckclient = new ServiceClientProxy();
var client = new SampleCuratioServiceClient(new InstanceContext(callneckclient));
client.SubcribeToService("me");
Console.ReadLine();
for (int i = 0; i < 15; i++)
{
Console.WriteLine(client.GetData(5));
client.Broadcast("this is from client me");
}
client.Close();
Console.Read();
}
public class ServiceClientProxy : ISampleCuratioServiceCallback, IDisposable
{
public void NotifyClient(string message)
{
Console.WriteLine(message);
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
当我运行5个客户端时,情况甚至出现问题。不是那些发送或接收消息。
答案 0 :(得分:3)
当客户端调用SubcribeToService
时,将其操作上下文添加到名为JoinedClien
的列表中。
当您在服务器中调用Broadcast
时,在所有收集的操作上下文中为每个已连接的客户端调用方法NotifyClient
。
问题是,断开连接的客户端不会从JoinedClien
列表中删除。
当您尝试在断开连接的操作上下文上调用操作方法时,会出现通道处于故障状态错误。
要解决此问题,您应该订阅Channel_Closed
和Channel_Faulted
事件,并在回调客户端时删除CommunicationException
并删除故障客户端的操作上下文:< / p>
public void Broadcast(string message)
{
// copy list of clients
List<OperationContext> clientsCopy = new List<OperationContext>();
lock(JoinedClien) {
clientsCopy.AddRange(JoinedClien);
}
// send message and collect faulted clients in separate list
List<OperationContext> clientsToRemove = new List<OperationContext>();
foreach (var c in JoinedClien)
{
try {
c.NotifyClient("message was received " + message));
}
catch (CommunicationException ex) {
clientsToRemove.Add(c);
}
}
foreach (var c in clientsToRemove)
{
lock(JoinedClien) {
if(JoinedClien.Contains(c))
JoinedClien.Remove(c);
}
}
}
添加新客户端时,您也必须锁定该操作:
var subscriber = OperationContext.Current.GetCallbackChannel<ISampleServiceCallBack>();
lock(JoinedClien)
{
if (!JoinedClien.Contains(subscriber))
{
JoinedClien.Add(subscriber);
}
}