无法连接到 DotNet Core 3.1 中的 WCF Web 服务参考

Cannot connect to WCF Web Service Reference in DotNet Core 3.1

从 dotnet core 3.1 连接到我的 WCF 服务时出现问题。为了添加服务引用,我使用 Add Connected Service,然后输入 WCF URI http://10.10.10.10:8330/mywcfservice。如果我像之前所说的那样输入远程地址,它工作正常并且服务器要求验证我的请求。但在我输入凭据后,在状态框中显示:

An error occurred while attempting to find services at '10.10.10.10/mywcfservice'. The remote server returned an error:(400) Bad Requst.

如果我在 WCF URI 后添加 /,在状态框中显示:

An error occurred while attempting to find services at '10.10.10.10/mywcfservice/'. The remote server returned an error:(401) Unauthorized.

我用 WCF Storm 测试了我的 WCF 服务,它工作得很好。另一方面,我可以像魅力一样从 .Net Standard Project 连接到 WCF 服务。那么问题出在哪里呢?

根据你提供的信息,我不知道你的服务器发生了什么。你可以参考这个link来记录在server-side上发生的错误:

Trying to add a service reference results in Bad Request (400) in one project, otherwise runs fine

另一个解决方案是使用dotnet-svcutil命令:

使用浏览器访问服务的WSDL文件,然后保存到本地。

然后使用dotnet-svcutil命令生成代理class。

最后,将代理class添加到项目中。

要使用代理class还需要添加这两个包:

在核心中调用 WCF 服务:

 BasicHttpBinding binding = new  BasicHttpBinding();
 EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8000/GettingStarted/CalculatorService");
 CalculatorClient calculatorClient = new CalculatorClient(binding,endpointAddress);

另外需要注意的是,有些WCF函数在core中是不支持的。详情请参考下方link:

WCF service works in MVC client but not in .Net Core 3 client

如果问题仍然存在,请随时告诉我。