簡體   English   中英

如何使用IDispatchMessageInspector獲取WCF合同操作的自定義屬性值

[英]How to get a Custom Attribute value of WCF Contract's Operation using IDispatchMessageInspector

問題是在AfterReceiveRequest中,如何使用OperationDescription找出在Operation上設置的自定義屬性? 如果有辦法,最好在服務協定接口或服務實現類的操作聲明中設置自定義屬性?

為了進一步說明這個問題:

public interface IGetterSetterService
{
    [OperationContract, GetterRequest]
    Data[] GetData();
    [OperationContract, SetterRequest]
    bool SetData(string Data);
}

要么

[WebInvoke(Method = "*", ResponseFormat = WebMessageFormat.Json, UriTemplate = "xyz"]
[GetterRequest]
public Data[] GetData()
{
    return new Data[];
}
[WebInvoke(Method = "*", ResponseFormat = WebMessageformat.Json, UriTemplate = "xyz/{data}"]
[SetterRequest]
public bool SetData(string data)
{
    return true;
}

現在,IDispatchMessageInspector:

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
    //Here how to find out the GetterRequest or SetterRequest custom attribute set on an
    //operation, may be using OperationDescription for the current context?
}

我的完整解決方案如下所示,並且可以正常運行:
1.首先獲得此處討論的操作說明
2.然后在“服務”界面的“操作”中找到設置的自定義屬性:

private UserAction GetIntendedUserAction(OperationDescription opDesc)
{
    Type contractType = opDesc.DeclaringContract.ContractType;
    var attr = contractType.GetMethod(opDesc.Name).GeCustomAttributes(typeof(RequestedAction), false) as RequestedAction[];
    if (attr != null && attr.Length > 0)
    {
        return attr[0].ActionName;
    }
    else
    {
        return UserAction.Unknown;
    }
}
public enum UserAction
{
    Unknown = 0,
    View = 1,
    Control = 2,
    SysAdmin = 3,
}
[AttributeUsage(AttributeTargets.Method)]
public class RequestedAction : Attribute
{
    public UserAction ActionName { get; set; }
    public RequestedAction(UserAction action)
    {
        ActionName = action;
    }
}

我認為您可以使用以下代碼:

public class operationdispatcher : IDispatchMessageInspector
{
    List<Type> MyAttrybutes = new List<Type>() { typeof(behaviorattribute) };

    public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        var serviceType = instanceContext.Host.Description.ServiceType;
        var operationName = OperationContext.Current.IncomingMessageHeaders.Action;
        var methodName = operationName.Substring(operationName.LastIndexOf("/") + 1);

        var method = serviceType.GetMethods().Where(m => m.Name == methodName && m.IsPublic).SingleOrDefault();
        var attributes = method.GetCustomAttributes(true).Where(a => MyAttrybutes.Contains(a.GetType()));

        foreach (var attribute in attributes)
        {
            // you might want to instantiate an attribute and do something
        }

        return null;
    }
}

注意:您在此處使用服務實現類,而不是接口。 如果使用method.GetCustomAttributes(true),則將獲取指定方法(也繼承自interface的方法)上的所有自定義屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM