如何在c#

时间:2017-07-13 15:02:39

标签: c# wcf asynchronous named-pipes

以下是我的情况:

  1. 我有一个基类和70个继承的类 此。
  2. 我正在实现WCF命名管道,这70个类将是 客户端。
  3. 需要异步接收和发送数据
  4. 对于第三篇文章我正在使用[CallbackBehaviorAttribute(ConcurrencyMode = ConcurrencyMode.Multiple)]属性,当我将其插入到已交付的类时,它可以正常工作。但是我应该在基类上插入属性,我不会花时间在所有交付的类上插入新属性或删除现有属性。但是当我在基类上插入时,它并不是异步工作的。因为CallbackBehaviorAttribute不是继承属性。

    那么我怎样才能解决上面提到的问题呢?我尝试创建一个继承自CallbackBehaviorAttribute的新属性,但它已被密封。

    客户代码:

    [CallbackBehaviorAttribute(ConcurrencyMode = ConcurrencyMode.Multiple)]
    public abstract partial class BaseService : IDownloaderCallbackService
    {
        public void connect_server_manager()
        {
            string address = "net.pipe://localhost/servermanager/";
            var factory = new DuplexChannelFactory<IDownloaderServiceContract>(new InstanceContext(this), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), new EndpointAddress(address));
            server_manager = factory.CreateChannel();
            logger.Info("Client Connected");
    
            logger.Info(string.Format(server_manager.Ping(this.this_service_id, new byte[1] { 0012 })));
            logger.Info(string.Format(server_manager.SetLastRequstTime(this.this_service_id, DateTime.Now)));
    
        }
    }
    

    服务器代码:

    string address = "net.pipe://localhost/servermanager/";
    
    serviceHost = new ServiceHost(service_manager);
    NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
    serviceHost.AddServiceEndpoint(typeof(IDownloaderServiceContract), binding, address);
    serviceHost.Open();
    
    Console.WriteLine("ServiceHost running. Press Return to Exit");
    

    IDownloaderServiceContract代码:

    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IDownloaderCallbackService))]
    
    public interface IDownloaderServiceContract
    {
        [OperationContract]
        string Ping(uint service_id, byte[] p);
        [OperationContract]
        string Pong(uint service_id, byte[] p);
    
        [OperationContract]
        dynamic SetLastRequstTime(uint service_id, dynamic data);
    }
    

1 个答案:

答案 0 :(得分:0)

我通过在连接代码块中将ConcurrencyMode设置为True来解决了这个问题。

    string address = "net.pipe://localhost/servermanager/";
    var factory = new DuplexChannelFactory<IDownloaderServiceContract>(new InstanceContext(this), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), new EndpointAddress(address));
    ((CallbackBehaviorAttribute)factory.Endpoint.EndpointBehaviors[typeof(CallbackBehaviorAttribute)]).ConcurrencyMode = ConcurrencyMode.Multiple;
    server_manager = factory.CreateChannel();