admin管理员组

文章数量:1301507

I have a .NET 4.6 WCF Client which works perfectly fine. When I convert WCF to .NET 8.0, it doesn't work. It keeps saying:

C# .NET 8.0 WCF client cannot call WCF service. Content Type application/soap+xml; charset=utf-8; was not supported by service. he client and service bindings may be mismatched.

This makes no sense because the service uses CustomBindings with SOAP 1.2 (HTTPS and Basic Authentication)

I have tried every possible bindings combination but I still keep getting the same exception.

Any ideas?

        var binding = new CustomBinding();
        binding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap12, Encoding.UTF8));
        binding.Elements.Add(new HttpsTransportBindingElement());

        var endpointAddress = new EndpointAddress(serviceUri);
        var channelFactory = new ChannelFactory<IReceiveFileInfoService>(binding, endpointAddress);
        channelFactory.Credentials.UserName.UserName = userName;
        channelFactory.Credentials.UserName.Password = password;

        IReceiveFileInfoService client = channelFactory.CreateChannel();
        
        var model = GetSampleModel();
        
        try
        {

            // Call the service
            var response = await client.SubmitInfoAsync(model);
            Console.WriteLine("Service response: " + response);

        }
        catch (Exception ex)
        {
            Console.WriteLine("Error calling service: " + ex.Message);
        }

This one didn't work either


        // Set a custom certificate validation callback
        System.Net.ServicePointManager.ServerCertificateValidationCallback +=
            (sender, certificate, chain, sslPolicyErrors) => true;

        // Define the service endpoint address (HTTPS)
        var endpointAddress = new EndpointAddress(serviceUri);

        // Create a binding for HTTPS with Basic Authentication
        var binding = new BasicHttpBinding();
        binding.Security.Mode = BasicHttpSecurityMode.Transport; // Use HTTPS
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; // Basic Authentication

        // Create the ChannelFactory
        var channelFactory = new ChannelFactory<IFileInfoService>(binding, endpointAddress);

        // Set the credentials (username and password)
        channelFactory.Credentials.UserName.UserName = userName;
        channelFactory.Credentials.UserName.Password = password;

        // Create the channel (proxy)
        IFileInfoService client = channelFactory.CreateChannel();

        var model = GetSampleModel();

        try {

            // Call the service method
            var result = await client.SubbmitInfoAsync(model);
            Console.WriteLine("Service response: " + result);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error calling service: " + ex.Message);
        }

本文标签: