简体   繁体   中英

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.

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();
     }

This is the config file:

  <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>

The ChannelDispatcher at /Service.svc/About with Contracts 'IHttpGetHelpPageAndMetadataContract' is unable to open.

any help is appreciated.

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.

Well i would like to know what's the business scenario. All I guess is, the client should not know the implementation, its just the URL of the service would indicate (or route) to the implementation.

Kindly clarify.


Refer to this existing post and let me know if it makes sense.


The above post is hinting the implementation, refer to this post for deployment details.

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:

<service name="namespace.Service" behaviorConfiguration="someName">

//.. later:

    <behavior name="someName">
      <serviceMetadata httpGetEnabled="true"
                       httpGetUrl="<link>/Service.svc/About" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>

The rest of the code, statyed the same

Can't you add another endpoint and fill in the adress with a distinct name:

<endpoint address="/SecondService" 
              binding="webHttpBinding2" 
              contract="namespace.IService" 
               />

Url becomes /Service.svc/SecondService

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM