簡體   English   中英

如何在WCF服務的服務行為中獲取請求消息的類型(如request.GetType)

[英]How to get type(as in request.GetType) of a request message in Service Behavior of WCF Service

我正在編寫一個自定義ServiceBehavior,希望它能知道請求消息的類型以推斷該消息是否由自定義屬性修飾。

我的樣本合同可能看起來像:

    [DataContract]
[Auditable]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    [Audit]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    [Audit]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

我正在嘗試通過使用以下方式在行為方面確定自定義屬性:

public object AfterReceiveRequest(ref Message request, IClientChannel channel,
    InstanceContext instanceContext)
{
    var typeOfRequest = request.GetType();

    if (!typeOfRequest.GetCustomAttributes(typeof (AuditableAttribute), false).Any())
    {
        return null;
    }
}

is always coming in as a 但是總是以

有沒有一種方法可以通過使用請求來推斷消息的類型?

注意:我直接引用了包含合同的程序集,而服務未通過wsdl進行引用。

or ) instead use a parameter Inspector (as in ). 解決上述問題的方法不是使用MessageInspector(如 ),而是使用參數Inspector(如 )。

在BeforeCall方法中,我們可以執行以下操作:

public object BeforeCall(string operationName, object[] inputs)
{

        var request = inputs[0];

        var typeOfRequest = request.GetType();

        if (!typeOfRequest.GetCustomAttributes(typeof(CustomAttribute), false).Any())
        {
            return null;
        }
}

暫無
暫無

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

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