Reputation: 400
I want to have some initialization code for my IIS-hosted service. I have read this article, it says (one of variants):
By deriving from ServiceHost type, you can implement the protected method ServiceHostBase.InitializeRuntime.
My question: Can I provide my own custom ServiceHost type to IIS?
Upvotes: 0
Views: 989
Reputation: 28530
Another way to do this is via file-less activation in the Web.config file. In your <system.serviceModel>
section, add the following:
<serviceActivations>
<add relativeAddress="Service.svc"
service="SomeNamespace.Service1"
factory="SomeNamespace.MyServiceHostFactory"/>
</serviceActivations>
This enables you to host a service in IIS without having a physical .svc file. This is a WCF 4.0+ feature, however.
Upvotes: 1
Reputation: 22739
As the article suggests, you can provide a ServiceHostFactory
to IIS that initializes your service host.
First, define the factory in the .svc
file:
<%@ServiceHost Language="C#" Factory="SomeNamespace.MyServiceHostFactory" %>
Then create the factory and host classes:
class MyServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new MyServiceHost(serviceType, baseAddresses);
}
}
class MyServiceHost : ServiceHost
{
public MyServiceHost()
{
// initialize, add endpoints, behaviors, etc.
}
}
You can also override InitializeRuntime
if you like. In the article they use it to log the virtual directory in which the service is running.
Upvotes: 3