Are you getting the following exception when calling a simple ASMX service via a WCF service reference from behind a firewall (in this case ISA Server):
"Server encountered an internal error. For more information, turn off customErrors in the server's .config file."
You'd expect there to be an inner exception, but there isn't. You can get to the actual exception by enabling all exceptions to cause a breakpoint in VS2005/2008 (Menu: Debug->Exceptions and then click the checkbox at the root of all .Net exceptions - be careful doing this in a web site, though, because a tonne of exceptions get thrown and swallowed by the Asp.Net engine!).
The actual error that is occuring is a WebException: [407] Proxy Authentication Required.
In my case, I've also been using Fiddler to debug the SOAP that's going to and from (if you haven't got it, then get it now!) my machine, and had also noticed that when it was running, the exception would go away. This is probably because of some behaviour on ISA Server where it will reuse existing credentials for a connection to a particular outgoing domain until a certain expiry time is reached - did throw me a curve however, until I figured that out!
Quick note - if you're looking at writing stuff to leverage TFS via it's web services and want to know how the Team Explorer and the Source Control Explorer do the things they do - it's a good idea to use Fiddler to examine the SOAP.
The problem here is an age old one where you have a proxy (in this case ISA Server) through which all your outgoing network traffic is performed. In my environment, the ISA Server requires credentials for a user on the domain that is allowed through the proxy. Without it, the connection will be refused, causing a RemotingException.
Now, in the past this could be solved using the System.Net.CredentialCache.DefaultNetworkCredentials property, and assigning it to a property of the SOAP client proxy that is generated - done. At first glance, there appears to be nothing as simple as that on the ClientBase<T>-deriving type that the WCF service reference generator in VS creates for you.
Looking closer, however, we see the member ClientBase<T>.ClientCredentials, which has a 'Windows' property, which in turn has a 'ClientCredential' property of type NetworkCredentials. Thinking that this would solve my problem, I wrote the following code:
- MyWebServiceClient client = new MyWebServiceClient();
- client.ClientCredentials.Windows.ClientCredential =
- System.Net.CredentialCache.DefaultNetworkCredentials;
I should mention at this point, that the project I'm working on doesn't bind it's web service references using the WCF config files - because I have to bind to a dynamic endpoint. Anybody working with the configs, however, should still be able to use this post to correct this problem when using the config files.
Anyway, the exception still gets raised.
I then tried manually filling out the user name, password and domain, but that didn't work either [bangs head on wall].
Found this MSDN forum where the guy solved his problem using config. not a great use for me, given that I'm configuring declaratively but there is one nugget in there - where the guy sets the HttpTransportBindingElement.UseDefaultWebProxy property to false, and then specifies the address of the proxy in the HttpTransportBindingElement.ProxyAddress property.
This involved also using the CustomBinding class - which I don't want to use. I simply want to create a BasicHttpBinding object, set some properties, and have done with it.
So I tried switching off the default proxy behaviour, and then give WCF the address of the default proxy(!) - read it again if you can't see why I think this is ludicrous.
Here's my eventual code:
- [TestMethod]
- public void TestMethod1()
- {
- //create a binding
- BasicHttpBinding binding = new BasicHttpBinding
- (BasicHttpSecurityMode.TransportCredentialOnly);
- binding.Security.Transport.ClientCredentialType =
- HttpClientCredentialType.Ntlm;
- binding.Security.Transport.ProxyCredentialType =
- HttpProxyCredentialType.Ntlm;
- //don't read the system proxy
- binding.UseDefaultWebProxy = false;
- //in my case - the URL I placed here was the same
- //as the one assigned by group policy! (WTF?)
- binding.ProxyAddress =
- new Uri("http://proxy:portnum");
- //configure the endpoint address
- EndPointAddress addr =
- new EndPointAddress("http://domain/service.asmx");
- //create an instance of the client type generated by the add
- //service reference tool using the binding and the endpoint
- //address that we've configured manually
- MyWebServiceClient c = new MyWebServiceClient(binding, addr);
- //make sure that the network credentials are set on the client.
- ClientCredentials creds =
- c.ClientCredentials;
- creds.Windows.ClientCredential =
- CredentialCache.DefaultNetworkCredentials;
- //should bee true by default - but be safe.
- creds.Windows.AllowNtlm = true;
- //if the web service at the other end needs to use these credentials
- //for impersonation or whatever, then this level should be set
- //accordingly.
- creds.Windows.AllowedImpersonationLevel =
- System.Security.Principal.TokenImpersonationLevel.Identification;
- //now call the method
- MyWebServiceClient.Foo();
- }
And it works fine.
A quick note on this. I am a great fan of MSDN - typically my first port of call is the F1 help, go to the index, type the class into that, and then browse through. Typically you get some choice examples, with some extended documentation on the impact of different values of various settings etc. In some parts of the framework, you even find links out to high-level conceptual topics to see 'where you are' in the overall process of whatever that framework is trying to achieve.
Not with WCF - the F1 help for all the WCF classes is shoddy. It has to be the least well-documented significant framework since SMO (don't even get me started on that) - not joined up in any way.
Anyway, I hope this post helps somebody out!
Comments
Post a Comment