繁体   English   中英

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

[英]WCF RoutingService - redirect with query string

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

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

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

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

我们解决了这个问题。

您必须创建新的绑定:

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

暂无
暂无

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

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