【发布时间】:2015-11-06 10:16:48
【问题描述】:
我们能否将多个服务行为附加到 WCF 中的同一服务。如果可以,我们如何做到这一点 - 通过配置文件或作为属性?
【问题讨论】:
标签: wcf wcf-data-services wcf-ria-services wcf-binding wcf-security
我们能否将多个服务行为附加到 WCF 中的同一服务。如果可以,我们如何做到这一点 - 通过配置文件或作为属性?
【问题讨论】:
标签: wcf wcf-data-services wcf-ria-services wcf-binding wcf-security
是的,你可以。
ServiceEndpoint 有一个Behaviors 集合。 因此,如果您在 C# 代码中创建服务,则可以向此集合添加任何行为:标准行为或您的行为。如何创建自定义行为并将其添加到endpoint see here 的示例。请记住,您可以根据需要创建和添加任意数量的行为。
如果你想在配置中添加行为,你需要创建行为配置扩展。 Here is an example hot to create it 并将其添加到配置文件中的端点。
编辑:
【讨论】:
是的,你可以。 您需要配置您的行为,并在服务标签中配置每个行为,如下所示:
<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
【讨论】:
是的,我们可以创建多个端点
<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 一起使用
【讨论】: