我需要使用此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添加到.NET Standard作为Web参考?还是仅与旧的.NET Framework兼容的Schenker服务的问题?
为什么会出现问题,因为我需要在Linux机器上托管目标应用程序。
WSDL文件:https://pastebin.com/p0nrLiwe
TransportOrderds.xsdhttps://pastebin.com/w6Edzdjz
StandardTypes.xsdhttps://pastebin.com/0pu6YJuA
您可以在此处尝试两个选项,但连接的服务除外。
选项1-尝试使用dotnet-svcutil消耗与服务模型元数据类似的WSDL。这是一个命令行工具,可为.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();
}
}
}
要成功使用通道工厂方法,需要从块包中安装ServiceModel。