问题与所有WCF回调客户端故障时,当一个故障 [英] Issue with all WCF Callback clients faulting when one faults

查看:161
本文介绍了问题与所有WCF回调客户端故障时,当一个故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出为什么当您有多个回调客户端连接使用知名的发布 - 订阅模式时,当一个故障或一个断开连接而不取消订阅所有客户端状态设置为关闭然后故障。

  [ServiceContract(SessionMode = SessionMode.Required,CallbackContract = typeof(ICallback))] 
public interface IPubSubService
{
[OperationContract(IsOneWay = false,IsInitiating = true)]
void Subscribe();

[OperationContract(IsOneWay = false,IsInitiating = true)]
void UnSubscribe();

[OperationContract(IsOneWay = false)]
void BroadcastMessage(string message);
}


[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class PubSubService:IPubSubService
{
private ICallback _callbackClient;
public static event Action< string> _行动;

public void Subscribe()
{
_callbackClient = OperationContext.Current.GetCallbackChannel< ICallback>();
_action + = ActionInvoked;
}

public void UnSubscribe()
{
_action - = ActionInvoked;
}

public void BroadcastMessage(string message)
{
_action.Invoke(message);
}

public void ActionInvoked(string message)
{
_callbackClient.SendMessage(message);
}
}

public interface ICallback
{
[OperationContract(IsOneWay = true)]
void SendMessage(string message);
}


//未订阅的发布者只发送消息
[CallbackBehaviorAttribute(UseSynchronizationContext = false)]
public partial class Form1 :Form,ICallback
{
public Form1()
{
InitializeComponent();
}

private ServiceClient _proxy;

private void button1_Click(object sender,EventArgs e)
{
try
{
_proxy = new ServiceClient(new InstanceContext(this));
_proxy.BroadcastMessage(textBox1.Text);
}
catch(异常异常)
{
Console.WriteLine(exception);
}
}


public void SendMessage(string message)
{

}
}

public static class ControlExtensions
{
public static void Invoke(this Control Control,Action Action)
{
Control.Invoke(Action);
}
}

public class ServiceClient:DuplexClientBase< IPubSubService>,IPubSubService
{
public ServiceClient(InstanceContext callbackInstance)
:base callbackInstance)
{}

public void Subscribe()
{
Channel.Subscribe();
}

public void UnSubscribe()
{
Channel.UnSubscribe();
}

public void BroadcastMessage(string message)
{
Channel.BroadcastMessage(message);
}
}

//订阅者
[CallbackBehaviorAttribute(UseSynchronizationContext = false)]
public partial class Form1:Form,ICallback
{
public Form1()
{
InitializeComponent();
}

private ServiceClient _proxy;

private void button1_Click(object sender,EventArgs e)
{
_proxy = new ServiceClient(new InstanceContext(this));
_proxy.Subscribe();
this.Invoke(()=> textBox1.AppendText(Subscribed ...));

}

public void SendMessage(string message)
{
this.Invoke(()=> textBox1.AppendText \\ r \\\
));
}

private void button2_Click(object sender,EventArgs e)
{
if(_proxy!= null& _proxy.State == CommunicationState.Opened )
{
_proxy.UnSubscribe();
}
}

private void button3_Click(object sender,EventArgs e)
{
Thread.Sleep(new TimeSpan(0,1,0) );
}
}

public static class ControlExtensions
{
public static void Invoke(this Control Control,Action Action)
{
Control.Invoke(Action);
}
}

public class ServiceClient:DuplexClientBase< IPubSubService>,IPubSubService
{
public ServiceClient(InstanceContext callbackInstance):base(callbackInstance)
{}

public void Subscribe()
{
Channel.Subscribe();
}

public void UnSubscribe()
{
Channel.UnSubscribe();
}

public void BroadcastMessage(string message)
{
Channel.BroadcastMessage(message);
}
}


//客户端发布者和订阅者的配置

< configuration>
< system.windows.forms jitDebugging =true/>
< system.serviceModel>
< bindings>
< netTcpBinding>
< binding name =netTCPBinding>
< reliableSession ordered =trueinactivityTimeout =00:10:00enabled =true/>
< security mode =None>
< / security>
< / binding>
< / netTcpBinding>
< / bindings>
< client>
< endpoint
address =net.tcp:// localhost:8008 / PubSubService
binding =netTcpBinding
bindingConfiguration =netTCPBinding
contract = ServiceLibrary.IPubSubService
name =netTCPBinding>
< identity>
< dns value =localhost/>
< / identity>
< / endpoint>
< / client>
< /system.serviceModel>
< startup>
< / startup>
< / configuration>

// Service for service

<?xml version =1.0?>
< configuration>
< system.serviceModel>
< services>
< service name =ServiceLibrary.PubSubService>
< endpoint address =net.tcp:// localhost:8008 / PubSubService
binding =netTcpBinding
bindingConfiguration =netTCPBinding
contract =ServiceLibrary.IPubSubService />
< / service>
< / services>
< bindings>
< netTcpBinding>
< binding name =netTCPBindingcloseTimeout =00:00:10openTimeout =00:00:10receiveTimeout =00:00:10sendTimeout =00:00:10transactionFlow = falsetransferMode =BufferedmaxBufferPoolSize =524288maxBufferSize =65536maxConnections =1000maxReceivedMessageSize =65536>
< readerQuotas maxDepth =32maxStringContentLength =8192maxArrayLength =16384maxBytesPerRead =4096maxNameTableCharCount =16384/>
< reliableSession ordered =trueinactivityTimeout =00:10:00enabled =true/>
< security mode =None>
< / security>
< / binding>
< / netTcpBinding>
< / bindings>
< /system.serviceModel>
< startup>< supportedRuntime version =v4.0sku =。NETFramework,Version = v4.0/>< / startup>< / configuration&


解决方案

导致事件提升在处理其调用列表时抛出异常,因此所有剩余的客户端不会被调用 - 您可以手动处理调用列表,例如

  List< Action< string>> targets = _action.GetInvocationList()。Cast< Action< string>>()。ToList(); 

foreach(var target in targets)
{
try
{
target(message);
}
catch(CommunicationException)
{
_action - = target;
}
}



您正在使用NetTcpBinding,这本来就是 sessionful 。该会话将在以下两种情况之一中断开(断开连接):当客户端关闭代理服务器时,或者在请求之间超过服务receiveTimeout时,



在您的PubSubService主机中,接收超时(影响订阅者会话)设置为5秒,与sendTimeout相同(影响您将等待的时间在决定用户在广播时死亡)。因此,当您意识到订阅者已过期时,所有其他订阅者已超时其会话



将PubSubService主机中的receiveTimeout设置为您想要的时间该订阅将有效,并且它将正常工作


I'm trying to figure out why when you have multiple callback clients connected using the well-known publish-subscribe pattern when one faults or when one disconnects without unsubscribing all the clients states are set to Closed then Faulted.

   [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallback))]
   public interface IPubSubService
   {
      [OperationContract(IsOneWay = false, IsInitiating = true)]
      void Subscribe();

      [OperationContract(IsOneWay = false, IsInitiating = true)]
      void UnSubscribe();

      [OperationContract(IsOneWay = false)]
      void BroadcastMessage(string message);
   }


   [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
   public class PubSubService : IPubSubService
   {
      private ICallback _callbackClient;
      public static event Action<string> _action;

      public void Subscribe()
      {
         _callbackClient = OperationContext.Current.GetCallbackChannel<ICallback>();
         _action += ActionInvoked;
      }

      public void UnSubscribe()
      {
         _action -= ActionInvoked;
      }

      public void BroadcastMessage(string message)
      {
         _action.Invoke(message);
      }

      public void ActionInvoked(string message)
      {
         _callbackClient.SendMessage(message);
      }
   }

   public interface ICallback
   {
      [OperationContract(IsOneWay = true)]
      void SendMessage(string message);
   }


// The Publisher that doesn't subscribe only sends the message
[CallbackBehaviorAttribute(UseSynchronizationContext = false)]
   public partial class Form1 : Form, ICallback
   {
      public Form1()
      {
         InitializeComponent();
      }

      private ServiceClient _proxy;

      private void button1_Click(object sender, EventArgs e)
      {
         try
         {
            _proxy = new ServiceClient(new InstanceContext(this));
            _proxy.BroadcastMessage(textBox1.Text);
         }
         catch (Exception exception)
         {
            Console.WriteLine(exception);
         }
      }


      public void SendMessage(string message)
      {

      }
   }

   public static class ControlExtensions
   {
      public static void Invoke(this Control Control, Action Action)
      {
         Control.Invoke(Action);
      }
   }

   public class ServiceClient : DuplexClientBase<IPubSubService>, IPubSubService
   {
      public ServiceClient(InstanceContext callbackInstance)
         : base(callbackInstance)
      { }

      public void Subscribe()
      {
         Channel.Subscribe();
      }

      public void UnSubscribe()
      {
         Channel.UnSubscribe();
      }

      public void BroadcastMessage(string message)
      {
         Channel.BroadcastMessage(message);
      }
   }

// The Subscriber
[CallbackBehaviorAttribute(UseSynchronizationContext = false)]
   public partial class Form1 : Form, ICallback
   {
      public Form1()
      {
         InitializeComponent();
      }

      private ServiceClient _proxy;

      private void button1_Click(object sender, EventArgs e)
      {
         _proxy = new ServiceClient(new InstanceContext(this));
         _proxy.Subscribe();
         this.Invoke(() => textBox1.AppendText("Subscribed..."));

      }

      public void SendMessage(string message)
      {
         this.Invoke(() => textBox1.AppendText(message + "\r\n"));
      }

      private void button2_Click(object sender, EventArgs e)
      {
         if (_proxy != null && _proxy.State == CommunicationState.Opened)
         {
            _proxy.UnSubscribe();
         }
      }

      private void button3_Click(object sender, EventArgs e)
      {
         Thread.Sleep(new TimeSpan(0, 1, 0));
      }
   }

   public static class ControlExtensions
   {
      public static void Invoke(this Control Control, Action Action)
      {
         Control.Invoke(Action);
      }
   }

   public class ServiceClient : DuplexClientBase<IPubSubService>, IPubSubService
   {
      public ServiceClient(InstanceContext callbackInstance) : base(callbackInstance)
      { }

      public void Subscribe()
      {
         Channel.Subscribe();
      }

      public void UnSubscribe()
      {
         Channel.UnSubscribe();
      }

      public void BroadcastMessage(string message)
      {
         Channel.BroadcastMessage(message);
      }
   }


// config for both clients publisher and subscriber

<configuration>
  <system.windows.forms jitDebugging="true" />
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="netTCPBinding">
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="true"/>
          <security mode="None">
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint
        address="net.tcp://localhost:8008/PubSubService"
        binding="netTcpBinding"
        bindingConfiguration="netTCPBinding"
        contract="ServiceLibrary.IPubSubService"
        name="netTCPBinding">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
  <startup>
  </startup>
</configuration>

// config for Service

<?xml version="1.0"?>
<configuration>  
  <system.serviceModel>
    <services>
      <service name="ServiceLibrary.PubSubService">
        <endpoint address="net.tcp://localhost:8008/PubSubService"
                  binding="netTcpBinding"
                  bindingConfiguration="netTCPBinding"
                  contract="ServiceLibrary.IPubSubService"/>
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="netTCPBinding" closeTimeout="00:00:10" openTimeout="00:00:10" receiveTimeout="00:00:10" sendTimeout="00:00:10" transactionFlow="false" transferMode="Buffered" maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="1000" maxReceivedMessageSize="65536">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="true"/>
          <security mode="None">
          </security>
        </binding>
      </netTcpBinding>      
    </bindings>
  </system.serviceModel>  
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

解决方案

Isn't it simply a case of the disconnected client causes the event raising to throw an exception when processing its invocation list and therefore all remaining clients don;t get invoked - you could process the invocation list manually something like this

        List<Action<string>> targets = _action.GetInvocationList().Cast<Action<string>>().ToList();

        foreach(var target in targets)
        {
           try
           {
               target(message);
           }
           catch(CommunicationException)
           {
               _action -= target;
           }
        }

Edit (After looking at the code)

You are using NetTcpBinding which is inherently sessionful. That session will be torn down (disconnected) in one of two situations - when the client closes their proxy or when the services receiveTimeout is exceeded between requests.

In your PubSubService host you had the receive timeout (which affects the subscribers sessions) set to 5 seconds, the same as the sendTimeout (which affects the time you'll wait before deciding the subscriber is dead when you are broadcasting). So by the time you realise the subscriber is dead, all of the other subscribers have timed out their sessions

Increase your receiveTimeout in the PubSubService host to the amount of time you want the subscriptions to be valid and it will work fine

这篇关于问题与所有WCF回调客户端故障时,当一个故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆