【问题标题】:Windows service with WCF programmatically created: how to expose it with Per-Call InstanceContextMode?以编程方式创建 WCF 的 Windows 服务:如何使用 Per-Call InstanceContextMode 公开它?
【发布时间】:2011-09-19 16:27:01
【问题描述】:

我的应用程序由许多不同的 Windows 服务组成。他们每个人都以编程方式创建 WCF 服务。我尝试使用“每次调用”实例上下文模式配置我的服务,但它似乎一次处理一个请求。

我该怎么办? 实现服务接口的类用这个属性修饰:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

虽然服务是这样实例化的:

ServiceHost _host = new ServiceHost(typeof(IMultiMarketBatchNotification));
_host.AddServiceEndpoint(typeof(IMultiMarketBatchNotification), binding, myAddress);

地点:

  • IMultiMarketBatchNotification 是服务接口
  • binding 是绑定的实例 (NetTcpBinding)
  • myAddress 是一个包含服务 url 的字符串(如 net.tcp://...)

这还不够吗?

谢谢, 马可

【问题讨论】:

    标签: c# .net wcf windows-services


    【解决方案1】:

    您需要在设置 ServiceHost 之后,但在打开它之前添加这些代码行:

    // look for the "ServiceBehavior" 
    ServiceBehaviorAttribute srvBehavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
    
    if (srvBehavior == null)
    {
       // if we didn't find the service behavior - create one and add it to the list of behaviors
       srvBehavior = new ServiceBehaviorAttribute();
       srvBehavior.InstanceContextMode == InstanceContextMode.PerCall;
       host.Description.Behaviors.Add(srvBehavior);
    }
    else
    {
       // if we found it - make sure the InstanceContextMode is set up properly
       srvBehavior.InstanceContextMode == InstanceContextMode.PerCall;
    }
    

    应该可以的。

    【讨论】:

    • 嗨,马克,感谢您的回复。我没有尝试它,因为缺少的东西是关于 CoucurrencyMode。加上这一点,我的服务开始变得多线程!无论如何感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多