首页 > 解决方案 > 如何将 WSDL Web 引用添加到 .NET Standard 库

问题描述

我需要使用这个 WSDL: https://testapi2.schenker.pl/services/TransportOrders?wsdl

但是我在添加对 .NET Standard 库的引用时遇到了问题。

对于 .NET Framework 库,我可以选择在下面的屏幕上添加 Web 引用。

但对于 .NET Standard 或 .NET Framework,我有完全不同的选择。

我也收到警告:

Cannot import wsdl:port
Detail: There was an error importing a wsdl:binding that the wsdl:port is dependent on.
XPath to wsdl:binding: //wsdl:definitions[@targetNamespace='http://api.schenker.pl/TransportOrders/']/wsdl:binding[@name='TransportOrdersBinding']
XPath to Error Source: //wsdl:definitions[@targetNamespace='http://api.schenker.pl/TransportOrders/']/wsdl:service[@name='TransportOrdersService']/wsdl:port[@name='TransportOrdersPort']
Cannot import wsdl:binding
Detail: The given key was not present in the dictionary.
XPath to Error Source: //wsdl:definitions[@targetNamespace='http://api.schenker.pl/TransportOrders/']/wsdl:binding[@name='TransportOrdersBinding']

有没有可能我可以将该 WSDL 作为 Web 参考添加到 .NET Standard?还是仅与旧的 .NET Framework 兼容的 Schenker 服务的问题?

为什么这是一个问题,因为我需要托管目标应用程序 od linux 机器。

WSDL 文件: https://pastebin.com/p0nrLiwe

TransportOrderds.xsd https://pastebin.com/w6Edzdjz

标准类型.xsd https://pastebin.com/0pu6YJuA

标签: c#.net.net-corewsdlvisual-studio-2019

解决方案


您可以在此处尝试两个选项,除了 Connected Services。

选项 1 - 尝试使用类似于服务模型元数据的 WSDL,使用 dotnet-svcutil。这是为 .NET Core 和 .NET Standard 生成 Web 服务引用的命令行工具。 dotnet-svcutil 的参考

选项 2 - 使用通道工厂来使用 WCF 服务。此选项的缺点是您应该了解要使用的 WCF 服务的合同定义。我创建了一个小型 WCF 服务并尝试了它,这是一种不太麻烦的方法。

我在ASP.NET Core MVC项目中定义了已知的WCF契约,如下图,

using System.ServiceModel;

namespace AspNet_Core_Wcf_Client.Contracts
{
    [ServiceContract]
    public interface IOrderService
    {
        [OperationContract]
        int GetOrdersCount();
    }
}

这个合同也在我的 WCF 中定义。

然后从控制器使用此服务,如下所示。

using AspNet_Core_Wcf_Client.Contracts;
using Microsoft.AspNetCore.Mvc;
using System.ServiceModel;

namespace AspNet_Core_Wcf_Client.Controllers
{
    public class OrderController : Controller
    {
        public IActionResult Index()
        {
            // create binding object
            BasicHttpBinding binding = new BasicHttpBinding();

            // create endpoint object
            EndpointAddress endpoint = new EndpointAddress("http://localhost:64307/OrderService.svc");

            // create channel object with contract
            ChannelFactory<IOrderService> channelFactory = new ChannelFactory<IOrderService>(binding, endpoint);

            // Create a channel
            IOrderService client = channelFactory.CreateChannel();
            int result = client.GetOrdersCount();

            ViewBag.Count = result;

            return View();
        }
    }
}

要成功使用通道工厂方法,需要从 nugget 包中安装 ServiceModel。


推荐阅读