C# 找不到具有名称和合同的端点元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14369406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:37:26  来源:igfitidea点击:

Could not find endpoint element with name and contract

c#wcfbindingwcf-clientwcf-configuration

提问by ipoh

I have added reference to a WCF service that has two end points. On adding the service the following get added to the Config file:

我添加了对具有两个端点的 WCF 服务的引用。添加服务时,将以下内容添加到配置文件中:

<client>
  <endpoint name="ABCServiceV1" address="http://staging.ABCwebservices.com/ABC/Service.svc"
    binding="basicHttpBinding" bindingConfiguration="ABCServiceV1"
    contract="ABCService.IService"  />
  <endpoint name="ABCServiceV2" address="http://staging.ABCwebservices.com/ABC/Service.svc/20"
    binding="basicHttpBinding" bindingConfiguration="ABCServiceV2"
    contract="ABCService.IService1"  />
</client>

The code to create the client is as as below:

创建客户端的代码如下:

ABCService.ServiceClient ABCClient = new ServiceClient("ABCServiceV2");

However, I am getting an runtime error - "Could not find endpoint element with name 'ABCServiceV2' and contract 'ABCService.IService' 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 name could be found in the client element."

但是,我收到一个运行时错误 - “在 ServiceModel 客户端配置部分中找不到名称为 'ABCServiceV2' 和合同为 'ABCService.IService' 的端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为没有可以在客户端元素中找到与此名称匹配的端点元素。”

if i used ABCService.ServiceClient ABCClient = new ServiceClient("ABCServiceV1");then everything works fine. But when using ABCServiceV2 it is trying to look for Contract - ABCService.IService - when it should be looking for - ABCService.IService1.

如果我使用,ABCService.ServiceClient ABCClient = new ServiceClient("ABCServiceV1");那么一切正常。但是当使用 ABCServiceV2 时,它试图寻找合同 - ABCService.IService - 当它应该寻找 - ABCService.IService1。

How do i make it look for the correct contract?

我如何让它寻找正确的合同?

采纳答案by chrisn

If you added a second reference to a different service (ABCServiceV2) then I believe this will have generated a second service class for ABCServiceV2. The two classes will implement separate contracts (ABCService.IService and ABCService.IService1) so you won't be able to interchange the endpoints.

如果您添加了对不同服务 (ABCServiceV2) 的第二个引用,那么我相信这将为 ABCServiceV2 生成第二个服务类。这两个类将实现单独的契约(ABCService.IService 和 ABCService.IService1),因此您将无法交换端点。

I believe you should be able to initialise your two service endpoints like so:

我相信您应该能够像这样初始化您的两个服务端点:

ABCService.ServiceClient ABCClient = new ServiceClient("ABCServiceV1");
ABCService.Service1Client ABCClient1 = new Service1Client("ABCServiceV2");

回答by Overlord

Even though this post is old and answered, the answer didn't help in my case. My problem was I generated the service client with the svcutil.exetool, but didn't specify any namespace at all; and so the contract namespace name was generated as the target namespace of the schema document by default, yes total mess.

尽管这篇文章很旧并且得到了回答,但答案对我的情况没有帮助。我的问题是我使用svcutil.exe工具生成了服务客户端,但根本没有指定任何命名空间;因此默认情况下合约命名空间名称是作为模式文档的目标命名空间生成的,是的,一团糟。

On the other hand I was trying to use the config file generated when a service reference is added to the project. The contract namespace in this file is ServiceReference1 (by default) or any other name you want, perfect storm! But all I had to do was to remove the namespace part from the FQN from the <endpoint>'s contract attribute, and the contract became visible to the CLR.

另一方面,我试图使用在将服务引用添加到项目时生成的配置文件。此文件中的合约命名空间是 ServiceReference1(默认情况下)或您想要的任何其他名称,完美风暴!但是我所要做的就是从 FQN<endpoint>的合同属性中删除名称空间部分,并且合同对 CLR 可见。

Hope this help others

希望这对其他人有帮助