C#: setting credentials when using MetadataExchangeClient
===

would appreciate some help with this - I feel I must be missing something obvious. I want to use the System.ServiceModel.Description.MetadataExchangeClient to retrieve information about a Web service. If the Web service is unsecured, then I have no problem, but if the Web service is a WCF service secured by Windows authentication, then I get an error along the lines of: Metadata contains a reference that cannot be resolved: 'http://server/Service.svc?WSDL'.

My code is fairly simple:

    Uri endpointUri = new Uri("http://server/Service.svc?WSDL");
    System.ServiceModel.Description.MetadataExchangeClient mexClient = new System.ServiceModel.Description.MetadataExchangeClient(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);
    mexClient.ResolveMetadataReferences = true;
    System.Net.NetworkCredential mexNetworkCredentials = new System.Net.NetworkCredential(username, password, domain);
    mexClient.HttpCredentials = mexNetworkCredentials;
    System.ServiceModel.Description.MetadataSet metadataSet = mexClient.GetMetadata(endpointUri, System.ServiceModel.Description.MetadataExchangeClientMode.HttpGet);

Am I just missing something in the way that the client needs to specify authentication? I have googled around for a sample code, but nothing seems to match what I have in mind.

Thanks for any help, Martin
