WebAPI找不到WCF端点元素

时间:2014-08-22 14:04:30

标签: c# wcf asp.net-mvc-4 asp.net-web-api endpoint

我试图通过.Net 4.5 C#MVC4 WebAPI(RCWindsExtSvc)使用有效的C#WCF服务(RCWindsSvc)。从WebAPI调用WCF服务时收到以下运行时错误:

'无法在ServiceModel客户端配置部分找到名称为“RCWindsSvcEndpoint”且收缩RCWindsSvc.IService1的端点元素。 这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此名称匹配的端点元素。'

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>

我在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();

        }

这似乎是一个很常见的问题,我已经探索了一些建议的解决方案无济于事。非常感谢您对此的任何帮助。

1 个答案:

答案 0 :(得分:0)

您在配置文件中缺少客户端部分。

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