1
votes

I have some one-time initialization tasks to do on service startup, and in order to do this I have defined a custom ServiceHostFactory and ServiceHost to override InitializeRuntime.

Service1.svc markup:

<%@ ServiceHost Language="C#" Debug="true" Factory="service.Our_Service_Host_Factory" Service="service.Service1" CodeBehind="Service1.svc.cs" %>

Service Host definitions:

public class Our_Service_Host_Factory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return new Our_Service_Host(serviceType, baseAddresses);
    }
}

class Our_Service_Host : ServiceHost
{
    public Our_Service_Host(Type serviceType, Uri[] baseAddresses)
        : base(serviceType, baseAddresses) { }

    protected override void InitializeRuntime()
    {
         // one time setup code
    }
}

Snippet from web.config:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>    
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

If I remove the Factory attribute on the ServiceHost markup, the WCF Test Client can successfully connect to my service. If I put it back in, it can't find the metadata endpoint with the following error:

Error: Cannot obtain Metadata from http://localhost:50154/Service1.svc If this is a Windows (R)
Communication Foundation service to which you have access, please check that you have enabled 
metadata publishing at the specified address.  For help enabling metadata publishing, please 
refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata 
Exchange Error    URI: http://localhost:50154/Service1.svc    Metadata contains a reference that 
cannot be resolved: 'http://localhost:50154/Service1.svc'.    There was no endpoint listening at 
http://localhost:50154/Service1.svc that could accept the message. This is often caused by an 
incorrect address or SOAP action. See InnerException, if present, for more details.    The 
remote server returned an error: (404) Not Found.HTTP GET Error    URI: 
http://localhost:50154/Service1.svc    There was an error 
downloading 'http://localhost:50154/Service1.svc'.    The request failed with HTTP status 404: 
Not Found.

It looks like having a custom ServiceHost/ServiceHostFactory might break simplified configuration. Is this the case, or is there something I'm overlooking which would make this continue to work?

1
I noticed that you have service.Our_Service_Host_Factory for the factory - could it be something as simple as it should be Service.Our_Service_Host_Factory (uppercase vs lowercase)? - Tim
Also, have you tried adding <%@Assembly name="MyAssembly" %> to your svc file? Replace MyAssembly with your assembly's name, of course. :) - Tim
Nope, the namespace it's in is definitely service. - Nicole DesRosiers

1 Answers

3
votes

If you override InitializeRuntime(), make sure you call base.InitializeRuntime() or you may see this behavior. After calling base.InitializeRuntime() before calling my additional startup logic, my service is now being correctly routed to.