简体   繁体   中英

WCF RoutingService - redirect with query string

We are working on WCF Routing Service which redirects different soap actions to different endpoints.

We want to this service rewrite query string included in router url: #router url#?param=param to endpoint: #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.

Do you know how add this strings to the end of endpoint url in every request?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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