在客户端应用程序中以代码创建WCF端点配置? [英] Create WCF endpoint configurations in the client app, in code?

查看:75
本文介绍了在客户端应用程序中以代码创建WCF端点配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从.NET客户端应用程序使用WCF Web服务,我认为我需要能够以编程方式创建端点,但是我不知道如何.我认为我需要这样做,因为当我尝试运行该应用程序时,出现以下错误:

I am trying to consume a WCF web service from a .NET client application, and I think I need to be able to programmatically create endpoints, but I don't know how. I think I need to do this because, when I try to run the application, I am getting the following error:

找不到默认端点 引用合同的元素 ServiceModel中的"IEmailService" 客户端配置部分.这 可能是因为没有配置文件 已找到适合您的应用程序,或 因为没有端点元素匹配 该合同可以在 客户元素.

Could not find default endpoint element that references contract 'IEmailService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

在排除此错误的同时,我创建了一个简单的Windows窗体应用程序,在其中尝试使用相同的Web服务.使用该测试应用程序,我可以成功连接到Web服务,并且得到有效的响应.但是,我可以通过从应用程序的app.config文件中删除system.serviceModel节点及其所有子节点,来在测试应用程序中重现上面引用的确切错误(我可能不必删除该部分的所有内容,没有把握).因此,我首先想到的是,我需要将该部分添加到真实应用程序的app.config文件中,并且一切都应该很好.不幸的是,出于荒谬的原因,我不会在这里讨论,这不是一种选择.因此,我不得不在客户端应用程序内通过代码生成此信息.

While troubleshooting this error, I created a simple windows forms application, in which I try to consume the same web service. With this test application I can connect to the web service successfully, and I get a valid response. But, I can reproduce the exact error cited above within in my test app by removing the system.serviceModel node and all of its child nodes from the application's app.config file (I might not have to remove ALL of that section, I'm not sure). So, my first thought was that I need to add that section to the app.config file for the real app, and everything should be fine. Unfortunately, for ridiculous reasons that I won't get into here, that is not an option. So, I am left with having to generate this information in code, inside the client app.

我希望这里的人可以帮助我解决这个问题,或者可以为解决此类问题的人提供一个好的资源.

I am hoping someone here can help me work through this, or can point me toward a good resource for this sort of problem.

是否可以在客户端应用中以代码创建端点配置?

Is it possible to create endpoint configurations in the client app, in code?

推荐答案

默认情况下,当您执行Add Service Reference操作时,WCF运行时将为您生成客户端代理.

By default, when you do an Add Service Reference operation, the WCF runtime will generate the client-side proxy for you.

最简单的使用方法是使用不带参数的构造函数实例化客户端代理,然后从app.config中获取信息:

The simplest way to use it is to instantiate the client proxy with a constructor that takes no parameters, and just grab the info from the app.config:

YourServiceClient proxy = new YourServiceClient();

这要求配置文件在您的服务合同中包含一个<client>条目-否则,您会得到您所遇到的错误.

This requires the config file to have a <client> entry with your service contract - if not, you'll get the error you have.

但是WCF运行时生成的客户端代理类还具有其他构造函数-例如,一个具有端点地址和绑定的构造函数:

But the client side proxy class generated by the WCF runtime also has additional constructors - one takes an endpoint address and a binding, for instance:

BasicHttpBinding binding = new BasicHttpBinding(SecurityMode.None);
EndpointAddress epa = new EndpointAddress("http://localhost:8282/basic");

YourServiceClient proxy = new YourServiceClient(binding, epa);

使用此设置,根本不需要配置文件-您可以在代码中定义所有内容.当然,您也可以在此处在代码中设置绑定和/或终结点的几乎所有其他属性.

With this setup, no config file at all is needed - you're defining everything in code. Of course, you can also set just about any other properties of your binding and/or endpoint here in code.

这篇关于在客户端应用程序中以代码创建WCF端点配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆