6.14.2012

Lost and found headers and identity for WCF service


If you are aware where is your headers and identity that were specified in config file and disappeared during runtime, check may be you are assigning an endpoint address form the code.

Look at EndpointAddress on MSDN. You can see 2 interesting properties there: EndpointAddress.Headers and EndpointAddress.Identity. This properties will be loaded from configuration you set in config file. So, when you want change endpoint address at runtime, make sure you copied headers and\or indentity from old instance:

var endpointAddress = new EndpointAddress(newUri, client.Endpoint.Address.Identity, client.Endpoint.Address.Headers.ToArray());
client.Endpoint.Address = endpointAddress;

Or, you can use reflection and extension method:

internal static class EndpointAddressExtensions
{
    private static FieldInfo uriField = typeof(EndpointAddress).GetField("uri", BindingFlags.NonPublic | BindingFlags.Instance);
 
    public static void ChangeUri(this EndpointAddress self, string uri)
    {
         uriField.SetValue(self, uri); 
    }
}

// ... in your code:

client.Endpoint.Address.ChangeUri(newUri);

But if last method is more elegant, you can't be sure that next update from Microsoft leave private field uri unchanged.

PS: code provided in article were written from scratch, so may be not free from minor issues

HTH


Shout it

kick it on DotNetKicks.com

No comments:

Post a Comment