在哪里是以编程方式添加IOperationBehavior的正确位置

时间:2009-05-25 11:31:32

标签: wcf

如何在iis上运行时以编程方式添加IOperationBehavior? 不在自定义wcf主机上。

感谢

Ali TAKAVCI

2 个答案:

答案 0 :(得分:8)

您可以将其作为属性附加:

public class CustomInspectorAttribute : Attribute, IOperationBehavior, IParameterInspector
{
    #region IOperationBehavior Members

    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
        // Attribute could be used on client side
        clientOperation.ParameterInspectors.Add(this);
    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        // Attribute could be used on server side
        dispatchOperation.ParameterInspectors.Add(this);
    }

    public void Validate(OperationDescription operationDescription)
    {
    }

    #endregion




    #region IParameterInspector Members

    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
    {
         // Do something with returned values from operation
    }

    public object BeforeCall(string operationName, object[] inputs)
    {
        // Do something with incoming parameters before invoking actual operation
        return null;
    }

    #endregion
}

将属性附加到操作

[ServiceContract]
public interface ICustomServiceContract
{
    [CustomInspector]
    [OperationContract]
    void MyOperation();
}

答案 1 :(得分:3)

您需要构建custom service host,然后设置.svc文件以使用它。在自定义服务主机中,您可以在服务启动之前执行任何您喜欢的操作,包括设置行为。因为您要使用操作行为,所以应该在OnOpening()方法中执行 - 因为服务工厂应用会在配置端点行为后重置操作行为。您将能够遍历OnOpening中的端点和操作。