找不到引用合同的默认端点元素

时间:2013-07-11 00:36:39

标签: wcf

我知道这已经被打死了,但是我不能让它按原样运作。 我有几个合同的WCF服务。 直接调用它们时它们都工作正常,例如http://merlin.com/CompanyServices/CompanyWcfService.svc/Get_Document_Dates_Received/223278 我已经在InfoPath Forms和Nintex Workflows上成功使用了这个WCF服务。 现在我创建一个简单的ASP.Net应用程序,例如在http://www.webcodeexpert.com/2013/04/how-to-create-and-consume-wcf-services.html中完成的。 我能够添加文章中描述的服务引用。 我在表单中添加了一个按钮,并在Button1_Click事件中添加了以下代码:

protected void Button1_Click(object sender, EventArgs e)
{
    ServiceReference1.CompanyWcfServiceClient x = new ServiceReference1.CompanyWcfServiceClient();
    var result = x.Get_Document_Dates_Received("223278");
}

当我点击按钮时出现错误:

  

"找不到引用合同的默认端点元素' ServiceReference1.ICompanyWcfService'在ServiceModel客户端配置部分中。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。"

所以我尝试将以下内容添加到web.config中:(直接从CompanyWcfService的web.config文件中复制。

<system.serviceModel>
<services>
  <service name="CompanyWcfServices.CompanyWcfService" behaviorConfiguration="ServiceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="CompanyWcfServices.ICompanyWcfService" behaviorConfiguration="webHttpEndpointBehavior" >
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">                                                                
    </endpoint>
  </service>
</services>
<bindings>
  <webHttpBinding>
    <binding>
      <security mode="None">
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name ="webHttpEndpointBehavior">
      <webHttp helpEnabled ="true" faultExceptionEnabled="true" automaticFormatSelectionEnabled="true"/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

我得到了同样的错误,还有其他事情要发生。

我终于放弃了这样的服务:

HttpWebRequest request = WebRequest.Create(@"http://merlin/Companyservices/CompanyWcfService.svc/Get_Document_Dates_Received/223278") as HttpWebRequest;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = null;

var result = "";
try
{
    response = request.GetResponse() as HttpWebResponse;
    if (response.StatusCode == HttpStatusCode.OK)
    {
        using (Stream stream = response.GetResponseStream())
        {
            StreamReader reader = new StreamReader(stream, Encoding.UTF8);
            result = reader.ReadToEnd();
        }
    }
}
catch (Exception ex)
{
    result = "";
}

我花了几个小时阅读帖子,其中大多数建议将配置信息复制到web.config文件中。这对我来说似乎有问题(除了它似乎不起作用的事实)。如果我需要使用第三方WCF服务怎么办?我是否必须向第三方请求配置信息?而Visa Versa,如果我创建一个旨在供第三方使用的WCF服务,我是否也需要为他们提供配置文件?

7 个答案:

答案 0 :(得分:16)

该错误表示您没有在客户端配置部分中定义端点。当您将服务引用添加到项目时,它应该为您创建客户端部分。如果没有,那么在system.serviceModel部分的应用程序的web.config中添加以下内容

<client>
  <endpoint 
      name="CompanyWcfService_webhttpBinding"
      address="http://merlin.com/CompanyServices/CompanyWcfService.svc" 
      binding="webHttpBinding" 
      contract="CompanyWcfServices.ICompanyWcfService" 
      behaviorConfiguration="webHttpEndpointBehavior"
  />
</client>

答案 1 :(得分:11)

如果我们有分层架构,请务必

1) add app.config in "all projects"
2) add service config details in all app.config
3) run the project

答案 2 :(得分:7)

如果您的项目引用了库并尝试使用该库函数中的WCF函数,那么您可以尝试将客户端端点从项目配置文件复制到dll的配置文件。有些事情发生在我之前,因为我在项目中引用的库不会使用项目配置文件(其中客户端点已配置,因为服务在那里被引用)但是它自己的结果是系统找不到端点配置。

答案 3 :(得分:4)

在我的情况下,我有一个WPF项目引用一个具有服务引用的外部UserControl。我还必须将服务引用添加到主项目中。

答案 4 :(得分:2)

将app.config中的绑定和客户端值添加到默认的web.config解决了我的问题。

答案 5 :(得分:1)

实际上,这个的诀窍是使用svcutil.exe来创建代理。我一直在尝试通过Visual Studio“添加服务”向导创建代理。一旦我这样做,配置就变得轻而易举。

SvcUtil.exe

答案 6 :(得分:0)

当涉及到WCF时,通常需要在调用它的可执行文件的配置文件中定义配置。

因此,如果您不幸在VB6程序中调用WCF DLL(例如,使用COM-interop .NET模块时可能就是这种情况),则需要调试VB6程序,然后您需要在VB6.exe所在的目录中创建一个VB6.exe.config文件。

如果不能执行上述操作,可能会导致“无法找到引用合同的默认端点元素”。

作为一种解决方法,可以在运行时加载dll的配置文件,然后使用Binding和EndpointAddress作为参数调用使用过的Service的构造函数(从dll的配置中获取)。