Search code examples
c#asp.netwcfweb-services

Custom ServiceHostFactory: The contract name 'IMetadataExchange' could not be found


I'm following the example from the following page. http://msdn.microsoft.com/en-us/library/aa395224.aspx but I get the following error when running it

The contract name 'IMetadataExchange' could not be found in the list of contracts implemented by the service MyServiceLibrary. Add a ServiceMetadataBehavior to the configuration file or to the ServiceHost directly to enable support for this contract.

The MyServiceHostFactory is pretty much copied from that page and I am adding it into my route table like follows.

RouteTable.Routes.Add(new ServiceRoute("V1", new MyServiceHostFactory(), typeof(MyServiceLibrary)));

My Config file

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web> 

  <system.serviceModel>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true" />
  </system.webServer>

</configuration>

If I add config update(below) to my config file the service runs but that makes it find the ServiceMetaDataBehaviour and really makes the whole SelfDescribingServiceHost redundant?

ServiceMetadataBehavior mexBehavior = this.Description.Behaviors.Find<ServiceMetadataBehavior>();

Config

<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

Solution

  • I found the issue. I was getting this error because I forgot to add the mexBehaviour before calling AddServiceEndpoint()on the ServiceHost class

    mexBehavior = new ServiceMetadataBehavior();
    Description.Behaviors.Add(mexBehavior);