【问题标题】:WCF service multiple behaviorsWCF 服务多种行为
【发布时间】:2016-01-16 05:26:37
【问题描述】:

我已经做了一些 WCF,但已经有一段时间了,而且我从来没有从头开始配置服务。

显然,虽然您只能有一个行为配置,但可以将多个行为附加到 WCF 服务:

ServiceHost serviceHost = new ServiceHost(typeof(Services.FooService), ServiceEndpointUri);
WebHttpBinding binding = new WebHttpBinding();       
ServiceEndpoint sep = serviceHost.AddServiceEndpoint(typeof(Contracts.IFooService), binding, string.Empty);
sep.Behaviors.Add(new WebHttpBehavior());
sep.Behaviors.Add(new MyCustomEndpointBehavior());

我想知道:

  1. 是否可以将多个服务行为附加到一个服务? ...或者,就此而言,多个端点行为?还是您最多只能使用一种?从这个 sn-p 中我不清楚这一点(这是迄今为止我在搜索中找到的最接近答案的东西)。
  2. 是否可以通过配置(而不是通过编程)来做到这一点?

【问题讨论】:

    标签: .net wcf


    【解决方案1】:

    您只需创建一个命名行为,然后将您的行为列为命名行为的子项。

      <system.serviceModel>
        <behaviors>
          <endpointBehaviors>
            <behavior name="MyExampleBehavior">
              <webHttp />
              <MyCustomEndpointBehavior />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <services>
          <service name="Example.Endpoints.MyEndpoint">
             <endpoint behaviorConfiguration="MyExampleBehavior" .../>
        </service>
      </services>
    

    使用SvcConfigEditor.exe(它应该已包含在您的Visual Studio 安装中,名称为“服务配置编辑器”),它可以帮助您编辑和配置.config 文件并轻松创建上述复杂的绑定。

    【讨论】:

    • &lt;service&gt; 元素中我会将behaviorConfiguration 设置为命名行为(MyExampleBehavior)?
    • 是的(我认为)...我只会使用编辑器,您可以通过 GUI 在其中设置配置
    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 2013-01-03
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    相关资源
    最近更新 更多