无法为WSDL文件创建Web参考

问题描述

我已经用Visual Studio 2019创建了一个ASP.Net core 3.1项目。我想将WSDL服务添加web reference。但是我不能。为什么Advanced按钮以这种形式删除

Visual Studio 2019:

enter image description here

旧版本:

enter image description here

我在某处读到过,不推荐使用Web参考。 我该怎么办?

UPDATE1:当我将其添加为服务参考时,文档示例代码错误

enter image description here

解决方法

您只应使用新方法。旧版本已弃用-请在您的评论中查看问题:Web Reference vs. Service Reference

最后一个答案按原样总结。最后,引用只是创建一个调用服务的客户端库。就是这样。

添加服务引用使您可以创建WCF客户端,如果您使用了适当的绑定,则可以用来与常规Web服务进行通信。添加网络参考将仅允许您创建网络服务(即SOAP)参考。

如果您完全确定您还没有准备好使用WCF(真的不知道为什么),那么您应该创建一个常规的Web服务参考。

(请参阅:https://stackoverflow.com/a/2158137/2416958


因此,要“解决”您的问题:

您需要在文件顶部包含用于创建客户端的namespace

在我的示例中:

using ServiceReference1;

但是您的情况可能有所不同。您可以在生成的文件中找到它,在我的情况下称为ServiceReference1

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace ServiceReference1
{
    
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil","2.0.1")]
    [System.ServiceModel.ServiceContractAttribute(Namespace="webservice.magfa.com",ConfigurationName="ServiceReference1.MagfaSoapServer")]
    public interface MagfaSoapServer
    {

您需要为此创建一个新的

var client = new MagfaSoapServerClient();
client.ClientCredentials = ...