首页 > 解决方案 > 带有 .NET 标准 2.0 的 WCF Web 服务参考

问题描述

reference.cs在 .NET Standard 2.0 项目中生成 WCF Web 服务引用,然后从 .NET 4.7.1 项目中使用它,但我得到了"Could not establish secure channel for SSL/TLS with authority"

如果我使用Add Service ReferenceVisual Studio 2015 之外的工具生成它。我得到一个类似的 reference.cs 类,如果我将它复制到我的 .NET 标准 2.0 项目中,它会起作用,但我会丢失 WCF Web 服务的异步调用实现。有什么办法可以使这项工作?

标签: c#web-serviceswcf.net-standard-2.0

解决方案


确保配置绑定。它们位于 .net 框架项目的 app.config 中,但应封装在BasicHttpBinding.NET Standard/.NET Core 中。

这样您就可以根据需要指定安全设置:

    binding.Security.Mode = BasicHttpSecurityMode.Transport;
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
    binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;

来自 Nuget 包 System.ServiceModel.Http:

namespace System.ServiceModel
{
    public class BasicHttpBinding : HttpBindingBase
    {
        public BasicHttpBinding();
        public BasicHttpBinding(BasicHttpSecurityMode securityMode);

        public BasicHttpSecurity Security { get; set; }

        public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingParameterCollection parameters);
        public override BindingElementCollection CreateBindingElements();
    }
}

推荐阅读