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?
service.Our_Service_Host_Factoryfor the factory - could it be something as simple as it should beService.Our_Service_Host_Factory(uppercase vs lowercase)? - Tim<%@Assembly name="MyAssembly" %>to your svc file? Replace MyAssembly with your assembly's name, of course. :) - Tim