【问题标题】:How to Consume WCF Web Service in a Winform application from a DLL如何从 DLL 在 Winform 应用程序中使用 WCF Web 服务
【发布时间】:2020-02-19 16:18:01
【问题描述】:

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

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

面临以下错误:

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

【问题讨论】:

  • 那么你的问题是什么?消费应用程序需要有配置,而不是 DLL。
  • 欢迎来到 Stack Overflow!分享您的代码,请查看how-to-ask 以帮助您提出一个好问题,从而得到一个好的答案。

标签: wcf dll app-config


【解决方案1】:

是的,正如你所做的那样。 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();
}

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

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多