16

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

当我单击按钮时,我收到错误:

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

所以我尝试将以下内容添加到 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 服务,我是否还需要向他们提供配置文件?

4

7 回答 7

22

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

<client>
  <endpoint 
      name="CompanyWcfService_webhttpBinding"
      address="http://merlin.com/CompanyServices/CompanyWcfService.svc" 
      binding="webHttpBinding" 
      contract="CompanyWcfServices.ICompanyWcfService" 
      behaviorConfiguration="webHttpEndpointBehavior"
  />
</client>
于 2013-07-11T02:02:48.727 回答
14

如果我们有分层架构,请确保

1) add app.config in "all projects"
2) add service config details in all app.config
3) run the project
于 2014-02-24T12:34:11.043 回答
7

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

于 2014-06-16T16:07:24.267 回答
5

就我而言,我有一个 WPF 项目引用了一个具有服务引用的外部 UserControl。我还必须将服务引用添加到主项目。

于 2016-01-21T01:31:32.580 回答
3

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

于 2015-06-08T14:27:26.517 回答
1

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

SvcUtil.exe

于 2014-02-25T15:00:34.917 回答
0

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

因此,如果您不幸不得不从 VB6 程序中调用 WCF DLL(例如,使用 COM-interop .NET 模块时可能会出现这种情况),并且您需要调试 VB6 程序,那么您需要在 VB6.exe 所在目录下创建一个 VB6.exe.config 文件。

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

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

于 2018-04-18T14:52:18.797 回答