如何通过DLL在Winform应用程序中使用WCF Web服务

问题描述 投票:0回答:1

我有一个需求,我需要在一个类中使用服务(ASMX或WCF),然后需要在Winforms应用程序中使用该类以从服务中获取响应。

但是问题出在app.config中类的配置(正在使用服务)会被加载到app.config中,如果在应用程序中引用了dll,则不会读取此配置文件。

面对以下错误:

在ServiceModel客户端配置部分中找不到引用合同'MyServices.IService'的默认终结点元素。这可能是因为没有为您的应用程序找到配置文件,或者是在client元素中找不到与该协定匹配的端点元素。

wcf dll app-config
1个回答
0
投票

是,您已经完成了。 Winform应用程序无法识别库项目中的app.config文件。https://docs.microsoft.com/en-us/dotnet/framework/wcf/deploying-a-wcf-library-project有两个解决方案,一个是将库项目中的配置移动到实际项目(Winform应用程序),主要是配置中的system.servicemodel部分。另一种是我们在代码段中对服务配置进行了硬编码,我们使用Channel Factory来调用服务。考虑下面的客户端代码。

   class Program
    {
        static void Main(string[] args)
        {
            BasicHttpBinding binding = new BasicHttpBinding();
            Uri uri = new Uri("http://10.157.13.69:18888");
            ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri));
            IService service = factory.CreateChannel();
            var result = service.GetData();
            Console.WriteLine(result);

        }
    }
    [ServiceContract]
    interface IService
    {
        [OperationContract]
        string GetData();
}

关于ChannelFactoryhttps://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/channel-factoryhttps://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory请随时告诉我是否有什么我可以帮助的。

© www.soinside.com 2019 - 2024. All rights reserved.