我已经在VS2008/中添加了web服务的代理。NET 3.5解决方案。在构造客户端。net时会抛出以下错误:

在ServiceModel客户端配置部分中找不到引用合约“IMySOAPWebService”的默认端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中没有找到与此契约匹配的端点元素。

搜索此错误告诉我在契约中使用完整的名称空间。这是我的app.config完整的命名空间:

<client>
  <endpoint address="http://192.168.100.87:7001/soap/IMySOAPWebService"
            binding="basicHttpBinding" bindingConfiguration="IMySOAPWebServicebinding"
            contract="Fusion.DataExchange.Workflows.IMySOAPWebService" name="IMySOAPWebServicePort" />
</client>

我正在运行XP本地(我提到这是因为一些谷歌点击提到win2k3) 将app.config复制到app.exe。配置,所以这也不是问题。

有线索吗?


当前回答

当我在配置文件元素中引用没有全局作用域操作符的契约时,出现了这个错误。

i.e.

<endpoint contract="global::MyNamepsace.IMyContract" .../>

有效,但

<endpoint contract="MyNamepsace.IMyContract" .../>

给出“无法找到引用合同的默认端点元素”错误。

包含MyNamepsace的程序集。IMyContract位于与主应用程序不同的程序集中,因此这可能解释了使用全局作用域解析的必要性。

其他回答

对使用服务的非库应用程序进行单元测试可能会导致此问题。

The information that others have entered addresses the root cause of this. If you are trying to write automated test cases and the unit you are testing will actually invoke the service interface, you need to add the service reference to the test project. This is a flavor of the application using library type of error. I did not immediately realize this though because my code that consumes the interface is not in a library. However, when the test actually runs it will be running from the test assembly, not the assembly under test.

向单元测试项目添加服务引用解决了我的问题。

我也有同样的问题。我在类库中使用WCF服务,并从windows应用程序项目调用类库。但我是忘记改变<系统。serviceModel>在windows应用程序项目的配置文件中。类库app.Config文件的serviceModel>。 解决方案:将外部项目的配置与类库的wcf配置相同。

我通过自己创建绑定和端点地址实例解决了这个问题(我认为其他人可能已经建议了)——因为我不想向配置文件添加新的设置(这是对一些广泛使用的现有库代码的替换,以前使用的是旧的Web Service Reference等),所以我希望能够在不添加新的配置设置的情况下将其放入。

var remoteAddress = new System.ServiceModel.EndpointAddress(_webServiceUrl);

using (var productService = new ProductClient(new System.ServiceModel.BasicHttpBinding(), remoteAddress))
{
    //set timeout
    productService.Endpoint.Binding.SendTimeout = new TimeSpan(0,0,0,_webServiceTimeout);

    //call web service method
    productResponse = productService.GetProducts();
} 

Edit

如果你正在使用https,那么你需要使用BasicHttpBinding而不是BasicHttpBinding。

我有一个情况,在单元测试。我将app.config文件复制到单元测试项目中。因此单元测试项目也包含端点信息。

当你在类文件中引用服务时,这里有几个响应可以找到正确的解决方案:将服务配置信息复制到app.config web中。这些答案似乎都没有告诉你应该复制什么。让我们试着纠正一下。

下面是我从我的类库的配置文件复制到我的控制台应用程序的配置文件中,为了解决这个疯狂的错误,我写了一个名为“TranslationServiceOutbound”的服务。

你基本上想要系统内的一切。serviceModel部分:

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_ITranslationServiceOutbound" />
  </basicHttpBinding>
</bindings>
<client>
  <endpoint address="http://MyHostName/TranslationServiceOutbound/TranslationServiceOutbound.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITranslationServiceOutbound"
    contract="TranslationService.ITranslationServiceOutbound" name="BasicHttpBinding_ITranslationServiceOutbound" />
</client>