How do I get the service to run from global.asax without having to call it?
I have a simple application where I use global.asax to map a serviceroute to a wcf service via a custom servicehostfactory in Application_Start. The constructor for this service does some initial processing to configure the service, which takes a little time.
I need this constructor to run when its serviceroute is added automatically. I tried to create a clientchannel from global.asax and make a dummy call to deploy the service, but found that the service hasn't stopped yet - seems like the application_start application should return?
So how do I get the constructor of the service to run on first display via global.asax without having to manually delete the service? Unfortunately AppFabric is not an option for us, so I can't just use its built-in autoplay.
UPDATE
I was asked for a little more detail:
This is similar to a route management service. So I have Service1 - it is added as serviceroute in global.asax. Now I have http: // localhost / Service1
Inside Service1 I have a method called addServiceRoute. When called, it also registers a route for Service2. Now I have http: // localhost / Service1 / Service2 .
My original solution from global.asax was to build a channel using http: // localhost / Service1 , but that won't work. Service1 has not been enabled yet and will not come until Application_Start returns (still not sure why?). So I thought I was cheating and moving this initial adderviceroute call to the service1 constructor. Also didn't work.
It was mentioned that it shouldn't be in the constructor - I agree, it's just a code check.
A singleton was also mentioned, which might be ok, but I intend to have more than one instance of Service1 on a machine (in the same application pool), so I don't think it would work?
** UPDATE # 2 ** I asked for a sample code .. here it is from global.asax (slightly shortened for brevity). So http: // localhost / Test MUST EXIT .. But if I need to use appfabric to warm up Test and run its constructor, then I don't need Test.svc or whatever? How can I get appfabric even if this service exists?
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}");
RouteTable.Routes.Add(
new ServiceRoute("Test", new MyServiceHostFactory(ITestService, BindingType.BasicHttpBinding, true), TestService));
}
source to share
What you are describing requires a single user service (which you should avoid) because usually every call or session gets a new instance = new constructor call. In a standalone WCF service, you can instantiate a Singleton instance and pass it to the constructor ServiceHost
. In the case of hosting IIS used along with ServiceRoute
, you can try to create your own class derived from ServiceHostFactory
and pass the created service instance as a parameter to its constructor. In this class, the factory implements the method CreateServiceHost
and passes this existing service instance to the constructor ServiceHost
. To make this work, your service class must still be treated as a singleton via the service behavior .
Btw. the constructor shouldn't take long. The constructor is intended for building an object, not for initializing the infrastructure. Using a constructor for such initialization is, in the first place, bad practice.
source to share
AppFabric autostart is what I would recommend - even if you say you can't use it - this is the problem it had to solve (warming up your service).
Alternatively, before AppFabric arrives, you will have to use a scheduled task (aka cron job) with an executable that calls the service you want to initialize. How autostart works AppFabric uses named pipes ( net.pipe
) to trigger a warm - up - but it just does it exactly when the service is being reworked. The difference between a scheduled task approach and AppFabric autostart is that the scheduled task doesn't know when your application pool has been reworked - you need to poll your service periodically to keep it warm.
Alternatively, you might consider hosting your WCF application outside of IIS using self-hosting. This will avoid a warm-up, but you will not achieve many of the benefits of an IIS-hosted container. See the HttpSelfHostServer
new MVC web interface or an overview using the standardServiceHost
.
source to share