How to add endpoint behavior to open IIS hosted WCF service
I have an IIS hosted WCF service and I need to add an endpoint behavior to it. I can't just add it to web.config. (We need to support a plugin architecture and plugin writers won't have access to my web.config.) I tried putting this in the static constructor for the service:
var endpointDispatcher = OperationContext.Current.Endpoin开发者_如何学编程tDispatcher;
SilverlightFaultMessageInspector inspector = new SilverlightFaultMessageInspector();
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
but it throws an exception saying "This value cannot be changed after the ServiceHost has opened." If I call host.Close() before adding the inspector object, it still throws the same exception.
I tried this as well:
var host = OperationContext.Current.Host;
host.Description.Endpoints[0].Behaviors.Add(new SilverlightFaultBehavior());
but it seems to have no effect. The endpoint behavior never gets called.
So is it possible to add an endpoint behavior to an IIS hosted WCF service?
Endpoint behavior can be added only before ServiceHost
opens (starts hosting the service). So it requires resetting your service. Access to ServiceHost
for services hosted in IIS is through custom ServicHostFactory
which must be defined in .svc file. So you generally need something like custom ServiceHostFactory
which will load behaviours from some repository and use them in CreateServiceHost
method. Behaviours will be applied only after restarting application.
精彩评论