Home > database >  Can not connect to WCF service on a Windows Server with .NET 6
Can not connect to WCF service on a Windows Server with .NET 6

Time:09-13

I have a WCF service and I created its client using "Add WCF Service Reference" it has a custom SSL client certificate and I added it to client. It works on my own computer but when I move it to my server it doesn't work. The code is a very simple function call like this:

using ConsoleAppFramework.MyServiceReference;
using System;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel.Security;
using System.Threading.Tasks;

namespace ConsoleAppFramework
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            SoapServicesClient client = new SoapServicesClient();

            client.ClientCredentials.ClientCertificate.Certificate = new X509Certificate2(@"C:\My-Client-Cert.p12", "pass");

            client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication()
            {
                CertificateValidationMode = X509CertificateValidationMode.None,
                RevocationMode = X509RevocationMode.NoCheck
            };

            client.Open();

            var res = await client.loginStaticAsync(Array.Empty<contextEntry>(), new userInfoRequestBean()
            {
                username = "user",
                password = "pass"
            });

            client.Close();

            Console.WriteLine([email protected]);
        }
    }
}

When I use .NET Framework 4.7.2 it work perfectly and I get the result but with .NET 6 I get this error:

Unhandled exception. System.ServiceModel.CommunicationException: An error occurred while sending the request.
 ---> System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine..
 ---> System.Net.Sockets.SocketException (10053): An established connection was aborted by the software in your host machine.
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStream.<FillBufferAsync>g__InternalFillBufferAsync|215_0[TReadAdapter](TReadAdapter adap, ValueTask`1 task, Int32 min, Int32 initial)
   at System.Net.Security.SslStream.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)
   at System.Net.Http.HttpConnection.FillAsync()
   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.AuthenticationHelper.SendWithNtAuthAsync(HttpRequestMessage request, Uri authUri, ICredentials credentials, Boolean isProxyAuth, HttpConnection connection, HttpConnectionPool connectionPool, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.AuthenticationHelper.SendWithAuthAsync(HttpRequestMessage request, Uri authUri, ICredentials credentials, Boolean preAuthenticate, Boolean isProxyAuth, Boolean doRequestAuth, HttpConnectionPool pool, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpClientRequestChannel.HttpClientChannelAsyncRequest.SendRequestAsync(Message message, TimeoutHelper timeoutHelper)
   --- End of inner exception stack trace ---
   at ConsoleAppCore3_1.Program.Main(String[] args) in C:\Users\......\Program.cs:line 45
   at ConsoleAppCore3_1.Program.<Main>(String[] args)

Is there anything changed between WCF client service on .NET Framework 4.7.2 and .NET 6?

What I tried

I try this with .NET Core 3.1 and I get the same error, only with .NET Framework I can get it to work. The server is a Windows Server 2019, the code works on my computer with .NET 6 and .NET Core 3.1 but on my server these two versions throw the same error.

CodePudding user response:

It has to do something that is closing your active http connection.

try this :

_client.DefaultRequestHeaders.ConnectionClose = true;

or you can use fiddler to further investigate what is happening.

CodePudding user response:

After moving from the.NET Framework to the.NET Core, not all features are available. You can try using CoreWCF instead of the original functionality, see this article for more information.

  • Related