wcf - 找不到引用契约(Contract)的默认端点元素

标签 wcf

我知道这已经被打死了,但我无法让它正常工作。
我有一个带有多个契约(Contract)的 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");
}

当我单击按钮时,出现错误:

"Could not find default endpoint element that references contract 'ServiceReference1.ICompanyWcfService' 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."



所以我尝试在 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 服务,我是否还需要向他们提供配置文件?

最佳答案

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

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

关于wcf - 找不到引用契约(Contract)的默认端点元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17583220/

相关文章:

c# - IIS 中的 WCF NetTcpBinding

wcf - 遵循 WCF 绑定(bind)配置命名约定

c# - 无法在同一解决方案中为 WCF 项目的客户端类添加服务引用

wcf - 在 Silverlight 中向 WCF 添加压缩的最简单方法是什么?

c# - log4net 是否会杀死我的 WCF 单元测试?

WCF 服务 404 错误

wcf - 在 WCF 服务中发布 DataAnnotation 属性,并在使用者代理类中重新生成它们?

wcf - 为什么我的客户发送请求如此缓慢?

wcf - 错误和 Restful WCF

WCF 多重契约(Contract)