WCF add query parameter not expected in signature

67 Views Asked by At

I have a contract used in a WCF POST. During the call, I need to add an extra parameter that I cannot add in the signature, because I get disambiguation issues.

Contract:

    [OperationContract]
    [WebInvoke(UriTemplate = "", Method = "POST")]
    Y Create(Stream x);

    [OperationContract]
    [WebInvoke(UriTemplate = "?cmd=put", Method = "POST")]
    Y Create2(Stream x);

What I'm trying to do is to alter the WebOperationContext.Current.OutgoingRequest to add this parameter, bool allowOverwrite.

The only way to make it work was by adding a header, which is not a happy choice. WebOperationContext.Current.OutgoingRequest.Headers.Add(...)

Any idea how I can improve this?

Note: I cannot do major changes to the contract as it is mainly legacy code.

1

There are 1 best solutions below

0
On

You can install WCF Web Extensions nuget package (nuget link). Then you will be able to add optional query parameters even outside an WebOperationContext scope, like this:

using (var factory = new WebChannelFactory<IQueryParametersTestService>(new WebHttpBinding()))
{
     factory.Endpoint.Address = new EndpointAddress(ServiceUri);
     factory.Endpoint.EndpointBehaviors.Add(new QueryParametersServiceBehavior());
     using (var client = factory.CreateWebChannel())
     {
           client.AddQueryParameter("format", "xml");
           client.AddQueryParameter("version", "2");
           var result = client.Channel.GetReport();
     }
}

Server-side you can retrieve the optional query parameters using WebOperationContext.