0

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?

smsClient = new SubmissionManagerClient();

SetProxySettings(smsClient, !string.IsNullOrEmpty(settings.Address), settings.Address, settings.Port, settings.Username, settings.Password);
     
retval = smsClient.sendSmsSubmission(authInfo, smsSubmission);

        private void SetProxySettings<TChannel>(ClientBase<TChannel> client,
            bool useProxy, string address, int port, string login, string password)
            where TChannel : class
        {
            if (!useProxy) return;

            var b = client.Endpoint.Binding as BasicHttpBinding;
            if (b == null)
            {
                this.logger.LogWarning("Binding of this endpoint is not BasicHttpBinding");
                return;
            }

            b.ProxyAddress = new Uri(string.Format("http://{0}:{1}", address, port));
            b.UseDefaultWebProxy = false;
            b.Security.Mode = BasicHttpSecurityMode.Transport;
            b.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; 
            b.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;

            if (client.ClientCredentials == null)
            {
                this.logger.LogWarning("client.ClientCredentials is null");
                return;
            }            

            client.ClientCredentials.UserName.UserName = login;
            client.ClientCredentials.UserName.Password = password;
        }

Error message

2022-05-03 22:15:33.1715|ERROR|00-9bcc09397d897a49b6f6ea013c203147-c68511c7504cf944-00|Altice.InfrastructureCore.SmsExpress.MessageSender|Error sending sms. System.ArgumentException: The provided URI scheme 'https' is invalid; expected 'http'. (Parameter 'via')
   at System.ServiceModel.Channels.TransportChannelFactory`1.ValidateScheme(Uri via)
   at System.ServiceModel.Channels.HttpChannelFactory`1.ValidateCreateChannelParameters(EndpointAddress remoteAddress, Uri via)
   at System.ServiceModel.Channels.HttpChannelFactory`1.OnCreateChannelCore(EndpointAddress remoteAddress, Uri via)
   at System.ServiceModel.Channels.HttpChannelFactory`1.OnCreateChannel(EndpointAddress remoteAddress, Uri via)
   at System.ServiceModel.Channels.ChannelFactoryBase`1.InternalCreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.Channels.ChannelFactoryBase`1.CreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.Channels.ServiceChannelFactory.ServiceChannelFactoryOverRequest.CreateInnerChannelBinder(EndpointAddress to, Uri via)
   at System.ServiceModel.Channels.ServiceChannelFactory.CreateServiceChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.Channels.ServiceChannelFactory.CreateChannel[TChannel](EndpointAddress address, Uri via)
   at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.ChannelFactory`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannelInternal()
   at System.ServiceModel.ClientBase`1.get_Channel()

Thanks a lot.

nnunes10
  • 550
  • 4
  • 14
  • Is there any error message? .net 6 calls wcf to ensure the download requires the [nuget package](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-5#windows-communication-foundation). You can check out [this post](https://stackoverflow.com/questions/951523/how-can-i-set-an-http-proxy-webproxy-on-a-wcf-client-side-service-proxy). – Lan Huang May 04 '22 at 02:50
  • @LanHuang i added the error message. I'm using the last version (4.9.0) of System.ServiceModel.Duplex, System.ServiceModel.Federation, System.ServiceModel.Http, System.ServiceModel.NetTcp and System.ServiceModel.Security. – nnunes10 May 04 '22 at 08:30

1 Answers1

0

You can try to use TransportCredentialOnly instead of Transport in binding safe mode.
EDIT
Maybe you can try the code below:

var binding = new BasicHttpBinding();
        binding.ProxyAddress = new Uri(string.Format("http://{0}:{1}", proxyAddress, proxyPort));
        binding.UseDefaultWebProxy = false;
        binding.Security.Mode = BasicHttpSecurityMode.Transport;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;

        var endpoint = new EndpointAddress("serviceadress");

        var authenticationClient = new WOKMWSAuthenticateClient(binding, endpoint);
        authenticationClient.ClientCredentials.UserName.UserName = username;
        authenticationClient.ClientCredentials.UserName.Password = password;


Make sure that the securitymode of the server and client is the same.

useful links:
The provided URI scheme 'https' is invalid; expected 'http'. Parameter name: via
https://stackoverflow.com/a/48008591/17218587

Lan Huang
  • 613
  • 2
  • 5