繁体   English   中英

来自 appsetting.json (.NET Core) 的 WCF 服务客户端配置

[英]WCF service client configuration from appsetting.json (.NET Core)

有没有办法从配置文件配置 WCF 服务引用? 我想使用 SecurityMode、Address、ReaderQuotas 等设置配置 WCF 服务引用。我还希望能够在 WsHttpBinding、BasicHttpBinding、BasicHttpsBinging 等之间进行选择(如 .NET Framework 中 app.config 提供的正常配置)。

有没有办法在 .NET Core/.NET Standard 中实现这一点?

谢谢,巴特克

某些绑定,例如WshttpbindingNetnamedbinding与 DotNet Core 框架不兼容。 因此,我们无法配置它。 但是,这并不代表我们不能配置BasichttpbindingNettcpbinding
目前,不使用第三方库,无法使用DotNet Core创建WCF服务。 此外,基于 DotNet Core 的 WCF 客户端只是一种兼容的解决方法。
https://github.com/dotnet/wcf
与 DotNet Framework 项目一样,Microsoft Corporation 提供了 Microsoft WCF Web Service Reference Provider 工具来生成客户端代理。
https://docs.microsoft.com/en-us/dotnet/core/additional-tools/wcf-web-service-reference-guide
添加连接服务后,它会生成一个包含客户端代理类的新命名空间。 大多数客户端配置位于Reference.cs
此外,我们可以手动编写代码来调用 WCF 服务。

class Program
    {
        static void Main(string[] args)
        {
            //using the automatically generated client proxy lcoated in the Reference.cs file to call the service.
            //ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
            //var result = client.TestAsync();
            //Console.WriteLine(result.Result);

            //using the Channel Factory to call the service.
            Uri uri = new Uri("http://10.157.13.69:21012");
            BasicHttpBinding binding = new BasicHttpBinding();
            ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
            IService service = factory.CreateChannel();
            var result = service.Test();
            Console.WriteLine(result);
        }
    }
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string Test();

    }

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
如果有什么我可以帮忙的,请随时告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM