C# 将WCF属性应用于服务中的所有方法

C# 将WCF属性应用于服务中的所有方法,c#,wcf,attributes,C#,Wcf,Attributes,我有一个自定义属性,希望应用于WCF服务中的每个方法 我这样说: [MyAttribute] void MyMethod() { } 问题是我的服务包含数百个方法,我不想在所有方法之上编写[Attribute]。有没有办法将该属性应用于我服务中的所有方法 这是我的属性的签名: //[AttributeUsage(AttributeTargets.Class)] public class SendReceiveBehaviorAttribute : Attribute, /*IServiceB

我有一个自定义属性,希望应用于WCF服务中的每个方法

我这样说:

[MyAttribute]
void MyMethod()
{

}
问题是我的服务包含数百个方法,我不想在所有方法之上编写[Attribute]。有没有办法将该属性应用于我服务中的所有方法

这是我的属性的签名:

//[AttributeUsage(AttributeTargets.Class)]
public class SendReceiveBehaviorAttribute : Attribute, /*IServiceBehavior,*/ IOperationBehavior
在Aliostad回答后编辑:

我试过这个:

public void ApplyDispatchBehavior(ServiceDescription desc, ServiceHostBase host)
{
    foreach (ChannelDispatcher cDispatcher in host.ChannelDispatchers)
    {
        foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
        {
            foreach (DispatchOperation op in eDispatcher.DispatchRuntime.Operations)
            {
                op.Invoker = new OperationInvoker(op.Invoker);
            }
        }
    }
}
而且:

public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
    foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
    {
        foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
        {
            foreach (DispatchOperation op in eDispatcher.DispatchRuntime.Operations)
            {
                op.Invoker = new OperationInvoker(op.Invoker);
            }
        }
    }
}
public void AddBindingParameters(ServiceDescription ServiceDescription、ServiceHostBase ServiceHostBase、集合终结点、BindingParameterCollection bindingParameters)
{
foreach(serviceHostBase.ChannelDispatchers中的ChannelDispatcher CDDispatcher)
{
foreach(CDDispatcher.Endpoints中的EndpointDispatcher eDispatcher)
{
foreach(eDiscovery.DispatchRuntime.Operations中的DispatchOperation op)
{
op.Invoker=新的OperationInvoker(op.Invoker);
}
}
}
}
但它仍然不起作用。

根据文档,如果实现此接口并创建属性并将其置于类级别,则它将应用于所有操作:

创建一个自定义属性 实现IServiceBehavior并使用它 要标记要删除的服务类,请执行以下操作: 被改进的。当ServiceHost对象 构造,使用反射 发现服务上的属性 类型。如果实现了任何属性 IServiceBehavior,它们被添加到 上的行为集合 服务描述


更新 不要实现
IOperationBehaviour
,而是通过循环所有操作在
IServiceBehaviour
中添加所需的行为:

foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
{
    epDisp.DispatchRuntime.MessageInspectors.Add(this);
    foreach (DispatchOperation op in epDisp.DispatchRuntime.Operations)
    {
        op.ParameterInspectors.Add(this); // JUST AS AN EXAMPLE 
    }                        
}

定义服务行为属性,在服务级别上应用该属性。在行为中,遍历契约中定义的所有方法,并执行所有的MyAtgigy属性。

< P>可以考虑使用AOP或POST生成操作,以便将属性注入编译的代码中。

< P>使用服务行为添加调用调用程序的正确操作行为。
public class MyAttribute : Attribute, IServiceBehavior, IOperationBehavior
{
    #region IServiceBehavior Members
    public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase host)
    {
        foreach (ServiceEndpoint endpoint in host.Description.Endpoints)
        {
            foreach (var operation in endpoint.Contract.Operations)
            {
                operation.Behaviors.Add(this);
            }
        }
    }
    ...
    #endregion

    #region IOperationBehavior Members
    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        dispatchOperation.Invoker = new OperationInvoker(dispatchOperation.Invoker);
    }
    ...
    #endregion
}

为什么不使用T4生成可重复代码? 在编译时使用反射比在运行时更好


从逻辑上讲,服务方法是业务方法的调用方。因此,通过读取业务类的公共方法,您可以轻松地生成具有所需属性的服务方法。它只需要在我们实现业务类时予以注意。

取决于特定属性,更重要的是,取决于使用它的代码。感谢您的帮助。您是对的,我将此属性应用于类,并且它的一部分可以工作,但是如果我不将其应用于所有方法,则使用IOperationBehavior的部分将无法工作。谢谢您,查看我的编辑以了解我的尝试。这很有意义,但仍然不起作用。好的,回家后我再打给你。除非你重写ServiceHost发现方法在serviceHow中公开的方式,否则它不会起作用?问题是如何?@SaeedNeamati阅读几乎所有.net AOP库的文档,如postSharp、AOP.net等。它们可以作为构建后操作包含在文档中。如果你对此有特别的问题,请在这里随意提问。这对我来说是一个很好的答案,但它不起作用。我今天在生产代码中使用它,它起作用了。我不确定你有什么问题。我不知道如何进一步帮助。我仔细检查了代码,没有什么比我的生产代码更突出的了。谢谢。@Aarron Stainback真是罪过!你的代码工作得很好;我只是忘记删除vs studio插入的throw NotImplementedException())。谢谢