简体   繁体   English

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

[英]WCF RoutingService - redirect with query string

We are working on WCF Routing Service which redirects different soap actions to different endpoints.我们正在研究WCF路由服务,它将不同的soap操作重定向到不同的端点。

We want to this service rewrite query string included in router url: #router url#?param=param to endpoint: #endpoint url#?param=param .我们希望此服务重写路由器 url 中包含的查询字符串: #router url#?param=param到端点: #endpoint url#?param=param

Our webservices accepts query strings when call directly, this strings are visible in router (context) but on the end this strings are removed from url.我们的 web 服务在直接调用时接受查询字符串,这个字符串在路由器(上下文)中可见,但最后这个字符串从 url 中删除。

Do you know how add this strings to the end of endpoint url in every request?你知道如何在每个请求中将这个字符串添加到端点 url 的末尾吗?

We solved the problem.我们解决了这个问题。

You must create new binding:您必须创建新的绑定:

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;
    }
}

And Client message inspector:和客户端消息检查器:

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)
    {
    }

}

Next you must create new endpoint Behavior:接下来您必须创建新的端点行为:

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)
    {
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM