【问题标题】:WCF RoutingService - redirect with query stringWCF RoutingService - 使用查询字符串重定向
【发布时间】:2017-07-06 12:26:57
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# web-services wcf soap routing


    【解决方案1】:

    我们解决了这个问题。

    您必须创建新的绑定:

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

    【讨论】:

      猜你喜欢
      • 2014-11-03
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 2012-11-29
      相关资源
      最近更新 更多