0

我在 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 请求。

4

1 回答 1

0

这只是一个例子。代码是概念性的,你必须按照你想要的方式来实现它。

[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.
}
于 2017-04-12T01:36:22.530 回答