dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.24k stars 5.88k forks source link

Setup proxy to invoke WCF service reference #29231

Open nnunes10 opened 2 years ago

nnunes10 commented 2 years ago

I added a wcf service reference to external soap web service. It's all good until i publish the solution to an internal machine which need to setup a proxy (with Basic auth user/password) to be able to invoke WS.

In .net framework, it was just add a section config to setup the proxy. I need the same in a .net 6 project.. I already tried to create a CustomBinding object to wrap BasicHttpBinding, in order to set the proxy but without success... Anybody can give me some tips to achieve that? Do you have any sample with this use case?

Thanks a lot.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

nnunes10 commented 2 years ago

My code to setup a http proxy in WCF client (without success).

    EndpointAddress endpoint =
        new System.ServiceModel.EndpointAddress("https://.../service");

    var binding = new BasicHttpBinding();
    binding.MaxBufferSize = int.MaxValue;
    binding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
    binding.MaxReceivedMessageSize = int.MaxValue;
    binding.AllowCookies = true;
    binding.Security.Mode = BasicHttpSecurityMode.Transport;

    // Create customBinding to send the request through proxy which requires user and password (basic auth)
    var customBinding = new CustomBinding(binding);    
    var htbe = customBinding.Elements.Find<HttpsTransportBindingElement>(); // HTTPS                   

    htbe.ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Basic;
    htbe.UseDefaultWebProxy = false;
    //htbe.ProxyAddress = new Uri(string.Format("http://{0}:{1}", settings.Address, settings.Port));
    htbe.BypassProxyOnLocal = true;

    WebProxy proxy = new WebProxy(new Uri(string.Format("http://{0}:{1}", settings.Address, settings.Port)), true, null);
    proxy.Credentials = new NetworkCredential(settings.Username, settings.Password);
    htbe.Proxy = proxy;

    smsClient = new SubmissionManagerClient(customBinding, endpoint);

    //smsClient.ClientCredentials.UserName.UserName = settings.Username;
    //smsClient.ClientCredentials.UserName.Password = settings.Password;

It seems the proxy is ignored.. I receive the following exception:

System.ServiceModel.CommunicationException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (smsexpress.cloud.altice-empresas.pt:443)
 ---> System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.