在客户端应用程序中用代码创建 WCF 端点配置?
我正在尝试从 .NET 客户端应用程序使用 WCF Web 服务,并且我认为我需要能够以编程方式创建端点,但我不知道如何操作。我认为我需要这样做,因为当我尝试运行该应用程序时,出现以下错误:
找不到默认端点 引用合同的元素 ServiceModel 中的“IEmailService” 客户端配置部分。这 可能是因为没有配置文件 已找到适合您的应用程序的信息,或者 因为没有端点元素匹配 该合同可以在 客户端元素。
在解决此错误时,我创建了一个简单的 Windows 窗体应用程序,在其中尝试使用相同的 Web 服务。通过这个测试应用程序,我可以成功连接到 Web 服务,并且得到有效的响应。但是,我可以通过从应用程序的 app.config 文件中删除 system.serviceModel 节点及其所有子节点来重现测试应用程序中上面引用的确切错误(我可能不必删除该部分的所有内容,我没有把握)。所以,我的第一个想法是我需要将该部分添加到真实应用程序的 app.config 文件中,一切都应该没问题。不幸的是,由于一些荒谬的原因,我不会进入这里,这不是一个选择。因此,我不得不在客户端应用程序内的代码中生成此信息。
我希望这里有人可以帮助我解决这个问题,或者可以为我指出解决此类问题的良好资源。
是否可以在客户端应用程序中用代码创建端点配置?
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:
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,当您执行
添加服务引用
操作时,WCF 运行时将为您生成客户端代理。使用它的最简单方法是使用不带参数的构造函数实例化客户端代理,并仅从
app.config
中获取信息:这要求配置文件具有
< ;client>
条目与您的服务合同 - 如果没有,您将收到错误消息。但是 WCF 运行时生成的客户端代理类还具有其他构造函数 - 例如,一个构造函数需要端点地址和绑定:
使用此设置,根本不需要配置文件 - 您可以在代码中定义所有内容。当然,您还可以在代码中设置绑定和/或端点的任何其他属性。
By default, when you do an
Add Service Reference
operation, the WCF runtime will generate the client-side proxy for you.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
:This requires the config file to have a
<client>
entry with your service contract - if not, you'll get the error you have.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:
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 服务的一种简单方法是使用 System.ServiceModel.ChannelFactory 类。
例如,如果您想使用 BasicHttpBinding:
如果您没有对服务程序集的引用,则可以使用生成的代理类上的重载构造函数之一来指定绑定设置。
An east way to consume a WCF service if you have a reference to the assembly which defines the interface, is using the System.ServiceModel.ChannelFactory class.
For example, if you would like to use BasicHttpBinding:
If you don't have a reference to the service assembly, then you can use one of the overloaded constructors on the generated proxy class to specify binding settings.