简体   繁体   English

如何为IIS托管的WCF服务上的所有操作添加OperationBehavior?

[英]How to add OperationBehavior for all operations on IIS hosted WCF service?

I have a custom OperationBehavior. 我有一个自定义的OperationBehavior。 I would like to apply it for all operations at once. 我想一次将其应用于所有操作。 Unfortunately, OperationBehaviors cannot be configured per entire service or in web.config. 不幸的是,无法针对整个服务或在web.config中配置OperationBehaviors。

When hosting WCF service in a test application, I can do the following: 在测试应用程序中托管WCF服务时,我可以执行以下操作:

        foreach (var ep in _serviceHost.Description.Endpoints)
        {
            foreach (OperationDescription od in ep.Contract.Operations)
            {
                od.Behaviors.Add(new MyOperationBehavior());
            }
        }

        _serviceHost.Open();

But how do I do it in a IIS hosted web application? 但是,如何在IIS托管的Web应用程序中执行此操作?

I tried to get OperationContext.Current.Host.Description.Endpoints in Application_Start but of course OperationContext.Current is not available before any operation has started, so my approach fails. 我尝试在Application_Start获取OperationContext.Current.Host.Description.Endpoints ,但是在开始任何操作之前当然无法使用OperationContext.Current ,所以我的方法失败了。

You can use a ServiceHostFactory to do that. 您可以使用ServiceHostFactory来做到这一点。 With it, you can get access to the OM prior to the service being opened. 使用它,您可以在打开服务之前访问OM。

This is an example: 这是一个例子:

public class MyFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);
        foreach (var ep in host.Description.Endpoints)
        {
            foreach (OperationDescription od in ep.Contract.Operations)
            {
                od.Behaviors.Add(new MyOperationBehavior());
            }
        }

        return host;
    }
}

And you can get more information about service host factories at http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx 您可以在http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx上获取有关服务主机工厂的更多信息。

At the end I found an alternative solution: use a contract behavior which injects any other behvaior as needed. 最后,我找到了一个替代解决方案:使用合同行为,根据需要注入其他行为。 Like this: 像这样:

public class InjectAllOperationsBehavior : Attribute, IContractBehavior
{
    private IOperationBehavior _operationBehavior = null;

    public InjectAllOperationsBehavior(Type operationBehaviorType)
    {
        _operationBehavior = 
            (IOperationBehavior)Activator.CreateInstance(operationBehaviorType);
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        foreach (OperationDescription opDescription in contractDescription.Operations)
        {
            opDescription.Behaviors.Add(_operationBehavior);
        }
    }

... other IContractBehavior methods can be left empty. ...其他IContractBehavior方法可以保留为空。 You might want also to use the ApplyClientBehavior method with the same code from the ApplyDispatchBehavior method. 您可能还希望将ApplyClientBehavior方法与ApplyDispatchBehavior方法中的相同代码一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM