WCF RoutingService - 使用查询字符串重定向

时间:2017-07-06 12:26:57

标签: c# web-services wcf soap routing

我们正在开发WCF路由服务,它将不同的soap操作重定向到不同的端点。

我们希望此服务重写查询字符串包含在路由器URL:#router url#?param=param到端点:#endpoint url#?param=param

我们的webservices在直接调用时接受查询字符串,这个字符串在路由器(上下文)中可见,但最后这个字符串从url中删除。

您知道如何在每个请求中将此字符串添加到端点url的末尾吗?

1 个答案:

答案 0 :(得分:0)

我们解决了这个问题。

您必须创建新的绑定:

public class QueryHttpBinding : BasicHttpBinding
{
    public override BindingElementCollection CreateBindingElements()
    {
        var result = base.CreateBindingElements();

        var http = result.Find<HttpTransportBindingElement>();
        if (http != null)
        {
            http.ManualAddressing = true;
        }

        var https = result.Find<HttpsTransportBindingElement>();
        if (https != null)
        {
            https.ManualAddressing = true;
        }

        return result;
    }
}

和客户邮件检查员:

public class CustomInspectorBehavior : IClientMessageInspector
{
    object IClientMessageInspector.BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        UriBuilder builder = new UriBuilder(channel.RemoteAddress.ToString());
        builder.Path += "?" + ((HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name]).QueryString;
        request.Headers.To = builder.Uri;
        return null;
    }

    void IClientMessageInspector.AfterReceiveReply(ref Message reply, object correlationState)
    {
    }

}

接下来,您必须创建新的端点行为:

public class Behavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        var inspector = new CustomInspectorBehavior();
        clientRuntime.MessageInspectors.Add(inspector);
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}