我有一个在本地主机上运行良好的 REST WCF 服务,但在联机部署时返回以下错误:
找不到资源。
注意:我的 WCF 服务位于以下路径下:
https://example.com/api/v1/Service.svc
我很确定问题是路径错误,我尝试了太多解决方案,但没有一个奏效。
以下是我Web.config
:
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="JsonBehavior" listenUri="/">
<identity>
<dns value="" />
</identity>
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding crossDomainScriptAccessEnabled="true" maxBufferSize="2000000000" maxReceivedMessageSize="2000000000" transferMode="Buffered">
<readerQuotas maxDepth="250000000" maxStringContentLength="250000000" maxArrayLength="250000000" maxBytesPerRead="250000000" maxNameTableCharCount="250000000" />
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="JsonBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" minFreeMemoryPercentageToActivateService="0" multipleSiteBindingsEnabled="true">
</serviceHostingEnvironment>
</system.serviceModel>
我尝试过的解决方案:
解决方案 1:
向system.serviceModel > Services > Service
添加基址
<host>
<baseAddresses>
<add baseAddress="https://example.com/" />
</baseAddresses>
</host>
解决方案 2:
向system.serviceModel > serviceHostingEnvironment
添加基址前缀
<baseAddressPrefixFilters>
<add prefix="https://example.com/" />
</baseAddressPrefixFilters>
解决方案 3:
使用端点address
和listenUri
<endpoint address="https://example.com/api/v1/Service.svc" binding="webHttpBinding" contract="IService" behaviorConfiguration="JsonBehavior" listenUri="/">
如何在Web.config
中定义正确的路径?
注意:在另一个项目中,我将.svc
文件放在根目录上,并且使用了与上面发布的相同Web.config
,一切正常。所以这绝对是一个路径问题。
最后,我通过更改<security mode="Transport" />
解决了问题,它奏效了!