繁体   English   中英

WebAPI找不到WCF端点元素

[英]WebAPI cannot find WCF endpoint element

我正在尝试通过.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();

        }

这似乎是一个非常普遍的问题,我已经探索了许多建议的解决方案,但无济于事。 非常感谢您的协助。

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

<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