簡體   English   中英

如何根據消息內容將WCF服務調用重定向到其他操作

[英]How to redirect WCF service calls to different operations based on the message content

我在WCF服務中有一項操作(方法)。 該操作具有Json內容的參數。

[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
string NotifyAuditLineUpdated(AuditLineUpdatedModel notification);

對於此參數AuditLineUpdatedModel,我使用DataContractAttributes和DataMemberAttributes創建了預定義的類,以在反序列化期間將json消息映射到對象。

但是,我有一個問題是,客戶端在相同的字段名稱下具有不同的Json消息結構 ,因此我無法將所有情況組合在一個類中。 換句話說,Json消息具有一個可以具有不同結構(非值)的字段; 因此,我試圖將調用定向到可以滿足各種Json消息的其他操作

到目前為止,我發現WCF在服務級別上提供路由。 我想知道是否可以在操作級別上路由呼叫。 換句話說,我只有一個服務,具有兩個不同參數類型的操作。 是否可以接聽電話並檢查消息內容,然后根據該消息將呼叫定向到適當的操作?

為您提供信息,我嘗試了WCF的IDispatchMessageInspector (消息檢查器功能)。 我可以檢查郵件的內容,但無法重定向或更改目標地址(到uri)。 注意:此外,對於兩種不同的情況,客戶端服務無法發送不同的uri請求。

這只是一個例子。 該代碼是概念性的,您將必須以所需的方式實現它。

[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
string NotifyAuditLineUpdated(AuditLineUpdatedModel notification);

// you can host this somewhere else
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
string MyInternalService(AuditLineUpdatedModel1 notification);

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
{
    object response;
    var isCallToMyInternalServiceRequired = VerificationMethod(request, out response);
    if(!isCallToMyInternalServiceRequired)
    {
        using(var client = new NotifyAuditLineUpdatedClient())
        {
            return client.NotifyAuditLineUpdated(response as AuditLineUpdatedModel);
        }
    }

    using(var client = new MyInternalServiceClient())
    {
        return client.MyInternalServiceClient(response as AuditLineUpdatedModel1);
    }
}   

private bool VerificationMethod(object notification, out object output)
{
    // your validation method.
}

暫無
暫無

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

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