I am trying to figure out the lifetime of a WebServiceHost. I thought that it would initially be per-call/per-request (i.e. Similar to an ASP.Net page being created for each request). I've created a Custom WebServiceHostFactory and WebServiceHost, but noticed that the Factory and Host are only created once.
The CustomWebServiceHostFactory overrides CreateServiceHost and the CustomWebServiceHost overrides OnOpening to spit out some diagnostics to track lifetimes.
My Service File
[ServiceContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public partial class ProductService
{
... // code
}
My Global.asax File
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("products", new CustomServiceHostFactory(), typeof(ProductService)));
}
Is anyone able to explain why two requests are hitting the same instance of CustomWebServiceHost, and how I would create a new Host for each service request?
Thanks
Creating the host for each new request doesn't make sense - the service host is the WCF runtime on the server side - the piece of code that will
Creating the service host is a pretty expensive and extensive operation - in the case of hosting in IIS, this will be done "on demand" when the first request comes in, but the service host will stay around for a given time and be reused.
When self-hosting, you typically create a single service host instance in your managed application, and then leave that host open and active until you want to shut down your services.
What will be created on a per-call basis for each request is the service class instance that actually handles the request coming in (but not the service host).