Subscribe via RSS
24Jul/150

WCF Clients: Set credentials BEFORE you do anything else!

I've just lost hours of my time and, Microsoft, I'd love it back. I had HTTPS WCF services running from a web app on IIS and I needed to merge in a NET.TCP service as well. Web.config hell, I tell you, but it eventually worked.

I then tried to set the ASP.NET Membership Authentication 'required' on the push services (DuplexChannelFactory) via NET.TCP. Everything I read online said I needed a certificate, so I specified this on the server, using the server certificate as I already had HTTPS configured.

This all worked OK... but trying to specify the Username and Password on the channel factory for the generated Client was a nightmare!

It turns out that, long story short, you SHOULD NOT do anything to the generated client after instantiating it. Set the username and password immediately.

This will work:

WcfClient = new WcfClient (new InstanceContext(WcfCallback), "");
WcfClient.ChannelFactory.Credentials.UserName.UserName = "username";
WcfClient.ChannelFactory.Credentials.UserName.Password = "password";
WcfClient.InnerChannel.Opened += WcfClient_Opened;

This wont:

WcfClient = new WcfClient (new InstanceContext(WcfCallback), "");
WcfClient.InnerChannel.Opened += WcfClient_Opened;
WcfClient.ChannelFactory.Credentials.UserName.UserName = "username";
WcfClient.ChannelFactory.Credentials.UserName.Password = "password";

Adding events to the ChannelFactory worked OK, if done prior to the credential configuration. But as soon as you touch the InnerChannel it'll throw a read-only exception as follows:

InvalidOperationException was caught
Object is read-only.

Anyway, the re-ordering worked like a charm... just keep an eye on it!

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


*

No trackbacks yet.