[英]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.