【问题标题】:How add WSDL web reference to.NET Standard library如何将 WSDL Web 引用添加到 .NET Standard 库
【发布时间】:2020-05-07 12:31:00
【问题描述】:

我需要使用这个 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? 或者是Schenker服务的问题,只兼容旧的.NET Framework?

为什么会出现问题,因为我需要托管目标应用程序 od linux 机器。

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

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

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

【问题讨论】:

  • 我用 .Net 堆栈尝试了那个 wsdl,但它也不起作用,因为它需要身份验证。我建议您将 WSDL 下载为文件以及所有导入的模式,然后从文件而不是直接从 url 生成。
  • 我试过没有结果,仍然是同样的错误。这是一个 WSDL 文件pastebin.com/ynT2ApAD
  • 但您还需要下载testapi2.schenker.pl/services/…testapi2.schenker.pl/services/…,如果这些xsd 有schemaLocations,那么您也需要下载它们。然后你必须编辑 wsdl 和 xsds 以指向磁盘上的文件而不是那个网络服务器。
  • 我修复了 WSD 文件并添加了 XML 模式(将路径更改为本地),但仍然出现相同的错误。您可以查看主要主题,我在那里添加了超链接。

标签: c# .net .net-core wsdl visual-studio-2019


【解决方案1】:

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

选项 1 - 尝试使用类似于服务模型元数据的 WSDL,使用 dotnet-svcutil。这是为 .NET Core 和 .NET Standard 生成 Web 服务引用的命令行工具。 Reference for 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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-05
    • 2018-05-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 2020-02-04
    相关资源
    最近更新 更多