【问题标题】:Muliple Service Behaviors to same service in WCFWCF 中同一服务的多个服务行为
【发布时间】:2015-11-06 10:16:48
【问题描述】:

我们能否将多个服务行为附加到 WCF 中的同一服务。如果可以,我们如何做到这一点 - 通过配置文件或作为属性?

【问题讨论】:

    标签: wcf wcf-data-services wcf-ria-services wcf-binding wcf-security


    【解决方案1】:

    是的,你可以。

    ServiceEndpoint 有一个Behaviors 集合。 因此,如果您在 C# 代码中创建服务,则可以向此集合添加任何行为:标准行为或您的行为。如何创建自定义行为并将其添加到endpoint see here 的示例。请记住,您可以根据需要创建和添加任意数量的行为。

    如果你想在配置中添加行为,你需要创建行为配置扩展Here is an example hot to create it 并将其添加到配置文件中的端点。

    编辑:

    服务行为 can be added in absolute same way

    【讨论】:

      【解决方案2】:

      是的,你可以。 您需要配置您的行为,并在服务标签中配置每个行为,如下所示:

      <service 
          name="Microsoft.ServiceModel.Samples.CalculatorService"
          behaviorConfiguration="CalculatorServiceBehavior">
        <!-- First behavior:
             http://localhost/servicemodelsamples/service.svc  -->
        <endpoint address=""
                  binding="basicHttpBinding"
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />
        <!-- Second behavior, with secure endpoint exposed at {base address}/secure:
             http://localhost/servicemodelsamples/service.svc/secure -->
        <endpoint address="secure"
                  binding="wsHttpBinding"
                  contract="Microsoft.ServiceModel.Samples.ICalculator" />
      </service>
      

      ICalcular 的相同服务,用于两种不同的行为。

      在此处阅读更多信息:https://msdn.microsoft.com/en-us/library/ms751515.aspx

      【讨论】:

      • 感谢 Ricardo 的回复,但我这里需要的是向同一个服务添加两个行为配置。在您展示的示例中,我们正在添加两个端点行为。是否可以再添加一个行为配置像验证服务行为到同一个计算器服务。像 。这行得通吗?
      • 不,对于服务器端点,您需要在服务标签中设置行为,因此您需要创建不同的服务和不同的端点,但您可以使用相同的合同。
      【解决方案3】:

      是的,我们可以创建多个端点

      <services>
        <service name="ReportService.ReportService">       
          <endpoint 
                 address="ReportService"  
                 binding="netTcpBinding"           
                 contract="ReportService.IReportService">
          </endpoint>
           <endpoint 
                 address="ReportService"  
                 binding="basicHttpBinding"           
                 contract="ReportService.IReportService">
          </endpoint>        
        </service>
      </services>
      

      我们可以在客户端 app.config 或 webconfig 文件中创建多个像 this.in 这样显示的端点

      <bindings>
              <netTcpBinding>
                  <binding name="netTcpBinding_IReportService" />
              </netTcpBinding>
      
              <basicHttpBinding>
                  <binding name="basicHttpBinding_IReportService" />
              </basicHttpBinding>
          </bindings>
          <client>
              <endpoint address="" binding="netTcpBinding"
                  bindingConfiguration="netTcpBinding_IReportService" contract="ServiceReference.IReportService"
                  name="netTcpBinding_IReportService">                
              </endpoint>
      
              <endpoint address="" binding="basicHttpBinding"
                  bindingConfiguration="basicHttpBinding_IReportService" contract="ServiceReference.IReportService"
                  name="basicHttpBinding_IReportService">                
              </endpoint>
          </client>
      

      那么我们应该在引用的时候提到绑定名称

            ServiceReference.ReportServiceClient client = new ServiceReference.ReportServiceClient(netTcpBinding_IReportService); 
      

      现在它将与 netTcpBinding 一起使用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-12
        • 1970-01-01
        • 1970-01-01
        • 2019-03-13
        • 2020-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多