简体   繁体   English

WebAPI找不到WCF端点元素

[英]WebAPI cannot find WCF endpoint element

I am attempting to consume a working C# WCF service (RCWindsSvc) via a .Net 4.5 C# MVC4 WebAPI (RCWindsExtSvc). 我正在尝试通过.Net 4.5 C#MVC4 WebAPI(RCWindsExtSvc)消耗可用的C#WCF服务(RCWindsSvc)。 I am receiving the following runtime error when calling the WCF service from the WebAPI: 从WebAPI调用WCF服务时,我收到以下运行时错误:

'Could not find endpoint element with name 'RCWindsSvcEndpoint' and contract RCWindsSvc.IService1' in the ServiceModel client configuration section. 在ServiceModel客户端配置部分中,“找不到名称为'RCWindsSvcEndpoint'的端点元素,并签订了RCWindsSvc.IService1合同”。 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.' 这可能是因为找不到您的应用程序的配置文件,或者是因为在客户端元素中找不到与该名称匹配的端点元素。

The system.serviceModel from the web.config in the WCF Service is: WCF服务中来自web.config的system.serviceModel为:

 <system.serviceModel>
  <services>
    <service name="RCWindsSvc.Service1" behaviorConfiguration="RCWindsSvc.Service1Behavior">
      <endpoint address="http://localhost:15021/RCWinds.svc"
          binding="webHttpBinding"
          contract="RCWindsSvc.IService1"
          behaviorConfiguration="ServiceAspNetAjaxBehavior"
          name="RCWindsSvcEndpoint"/>
      <!-- behaviorConfiguration="WebBehaviour" /> -->
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="RCWindsSvc.Service1Behavior">
        <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
        <serviceMetadata httpGetEnabled="true"/>
        <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
        <serviceDebug includeExceptionDetailInFaults="false"/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <!-- <behavior name="webBehaviour">
        <webHttp/>
      </behavior>-->
      <behavior name="ServiceAspNetAjaxBehavior">
        <enableWebScript/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
</system.serviceModel>

I am using ConfigurationChannelFactory in the WebAPI code to retrieve the WCF configuration data, and the following is the code from the WebAPI Controller: 我在WebAPI代码中使用ConfigurationChannelFactory检索WCF配置数据,以下是来自WebAPI控制器的代码:

public String[] GetStationNames()
        {
            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
            fileMap.ExeConfigFilename = "web.config";
            Configuration newConfiguration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

            ConfigurationChannelFactory<RCWindsSvc.IService1> factory1 = new
                ConfigurationChannelFactory<RCWindsSvc.IService1>("RCWindsSvcEndpoint", newConfiguration, new EndpointAddress("http://localhost:15021/RcWinds.svc"));
            RCWindsSvc.IService1 client1 = factory1.CreateChannel();

            IEnumerable<string> stationNames = client1.GetStationNames();
            return stationNames.ToArray();

        }

This seems to be a somwhat common issue and I have explored a number of suggested solutions to no avail. 这似乎是一个非常普遍的问题,我已经探索了许多建议的解决方案,但无济于事。 Any assistance with this is greatly appreciated. 非常感谢您的协助。

You're missing a client section in your configuration file. 您在配置文件中缺少客户端部分。

<system.serviceModel>
        <bindings>
            ...
        </bindings>
        <client>
           <endpoint address="http://localhost:15021/RCWinds.svc"
               binding="webHttpBinding" 
               contract="RCWindsSvc.IService1" name="RCWindsSvcEndpoint" />
        </client>
    </system.serviceModel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM