简体   繁体   English

无法连接到 .NET 6 的 Windows 服务器上的 WCF 服务

[英]Can not connect to WCF service on a Windows Server with .NET 6

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.我有一个 WCF 服务,我使用“添加 WCF 服务参考”创建了它的客户端,它有一个自定义的 SSL 客户端证书,我将它添加到客户端。 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:代码是一个非常简单的 function 调用,如下所示:

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(res.@return.sessionId);
        }
    }
}

When I use .NET Framework 4.7.2 it work perfectly and I get the result but with .NET 6 I get this error:当我使用 .NET Framework 4.7.2 时,它工作得很好,我得到了结果,但是使用 .NET 6 我得到了这个错误:

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? .NET Framework 4.7.2 和 .NET 6 上的 WCF 客户端服务之间是否有任何变化?

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.我用 .NET Core 3.1 尝试这个,我得到了同样的错误,只有 .NET 框架我才能让它工作。 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.服务器是 Windows 服务器 2019,代码在我的计算机上运行,.NET 6 和 .NET Core 3.1 但在我的服务器上,这两个版本抛出相同的错误。

It has to do something that is closing your active http connection.它必须做一些事情来关闭您的活动 http 连接。

try this:尝试这个:

_client.DefaultRequestHeaders.ConnectionClose = true;

or you can use fiddler to further investigate what is happening.或者您可以使用提琴手进一步调查正在发生的事情。

After moving from the.NET Framework to the.NET Core, not all features are available.从 .NET Framework 迁移到 .NET Core 后,并非所有功能都可用。 You can try using CoreWCF instead of the original functionality, see this article for more information.您可以尝试使用 CoreWCF 代替原始功能,有关详细信息,请参阅本文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM