WCF在同一配置中的多个服务相同的合同 [英] WCF multiple services same contract in same Config
问题描述
我正在尝试托管同一合同的不同服务实现:
Im trying to host to different service implementations of the same contract:
原因是需要一个用于外部测试的虚拟实现.
The reason is that need a dummy implementation for out-of-the-house testing.
我试图在同一个 WindowsService 中托管两者:
Im trying to host both in the same WindowsService:
private ServiceHost _host;
private ServiceHost _dummy;
protected override void OnStart(string[] args)
{
_host = new ServiceHost(typeof(Service));
_host.Open();
//trying to avoid the app.config beeing used - because its already been hoste by _host
_dummy = new ServiceHost(typeof(TestDummyService));
_dummy.Description.Endpoints.Clear();
_dummy.AddServiceEndpoint(typeof(IService),
new WebHttpBinding(),
@"<link>/Dummy.svc/");
_dummy.ChannelDispatchers.Clear();
_dummy.Open();
}
这是配置文件:
<system.serviceModel>
<services>
<service name="namespace.Service">
<host>
<baseAddresses>
<add baseAddress="<link>/Service.svc"/>
</baseAddresses>
</host>
<endpoint address=""
binding="webHttpBinding"
contract="namespace.IService"
behaviorConfiguration="web" />
<endpoint address="/mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors >
<behavior>
<serviceMetadata httpGetEnabled="true"
httpGetUrl="<link>/Service.svc/About" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name ="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
位于/Service.svc/About with Contracts ‘IHttpGetHelpPageAndMetadataContract’ 的 ChannelDispatcher 无法打开.
The ChannelDispatcher at /Service.svc/About with Contracts ‘IHttpGetHelpPageAndMetadataContract’ is unable to open.
感谢任何帮助.
更新 1我的目标是在一个 WindowsService 中托管同一合同 (IService
) 的 2 个不同实现.
Update 1
My goal is to have 2 different implementations of the same contract (IService
) hosted in one WindowsService.
我还想在配置文件中配置它们.
I would also like to configure both of them in the config file.
推荐答案
所以我发现,即使 testdummy 服务以编程方式添加,它仍然得到服务元数据行为.
so i found out, that even thow the testdummy service was added programatic, it still got the service metadatabehavior.
我的解决方案是不将行为设为默认值 - 以名称命名:
My solution was to not make the dehavior default - given it at name:
app.config:
app.config:
<service name="namespace.Service" behaviorConfiguration="someName">
//.. 稍后:
<behavior name="someName">
<serviceMetadata httpGetEnabled="true"
httpGetUrl="<link>/Service.svc/About" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
其余代码保持不变
这篇关于WCF在同一配置中的多个服务相同的合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!