WAS Non-HTTP activation - hooking application startup
I'm trying to integrate a netTcpBinding based application that is hosted inside WAS with an IoC container (autofac/spring). Unfortunately, when it starts inside WAS and due 开发者_如何学JAVAto the fact that it is not an Http based application, no events are fired inside the Global application class.
I need to catch the application domain startup so that I can configure the IoC container. Is there any way to do this when hosting in WAS?
I've seen horrible things involving using static classes inside App_Code folders, but I'd like something somewhat more testable and not quite as dirty.
You can implement IInstanceProvider (see http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iinstanceprovider.aspx) and tell the WCF runtime to use it via a service behavior.
The service behavior can be configured with a custom host, configuration or with an attribute. Here an example of the latter:
public class DependencyInjectionServiceBehaviorAttribute : Attribute, IServiceBehavior
{
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (var cdb in serviceHostBase.ChannelDispatchers)
{
var cd = cdb as ChannelDispatcher;
if (cd != null)
{
foreach (EndpointDispatcher ed in cd.Endpoints)
{
ed.DispatchRuntime.InstanceProvider = new MyServiceFactory(serviceDescription.ServiceType);
}
}
}
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {}
}
精彩评论