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

查看:23
本文介绍了在客户端应用程序中创建 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?

推荐答案

默认情况下,当您执行 添加服务引用 操作时,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();

这要求配置文件在您的服务合同中有一个 条目 - 如果没有,您将收到您遇到的错误.

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天全站免登陆