错误404调用Restful Wcf服务时

本文关键字:服务 Wcf Restful 调用 错误 | 更新日期: 2024-10-24 20:57:56

我一次又一次地搜索,但我真的不知道我遗漏了什么。我有WCF方法,调用后,服务返回错误404,找不到资源。

url如下:localhost:3522/AccountService.svc/accountbalance/1

我使用IIS 8学习版从Visual运行它。

类似于的WCF方法

[ServiceContract]
public interface IAccountService
{
    [OperationContract]
    [WebGet(UriTemplate = "accountbalance/{accountId}")]
    decimal GetAccountBalance(string accountId);
}

这是我的web.config

<bindings>
  </basicHttpBinding>
  <webHttpBinding>
    <binding name="WebHttpBinding">
      <security mode="Transport">
        <transport clientCredentialType="Windows" proxyCredentialType="Windows" />
      </security>
    </binding>
  </webHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="ProfileServiceDefaultBehavior"
    name="MyWcfService.AccountService">
    <endpoint behaviorConfiguration="RestFulBehavior" binding="webHttpBinding"
      bindingConfiguration="WebHttpBinding" name="AccountServiceEndpoint"
      bindingName="AccountServiceBinding" contract="MyWcfService.IAccountService" />
    <endpoint address="mex" behaviorConfiguration="" binding="mexHttpBinding"
      contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="RestFulBehavior">
      <webHttp automaticFormatSelectionEnabled="true" />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="ProfileServiceDefaultBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

错误404调用Restful Wcf服务时

根据简短的回顾,配置文件的服务部分似乎缺少baseAddresses元素。

    <host>
      <baseAddresses>
        <add baseAddress="https://localhost:3522/AccountService.svc" />
      </baseAddresses>
    </host>

此外,您可能需要考虑启用WCF跟踪和日志记录,以便查看WCF服务启动事件中是否存在任何错误。以下链接提供了一个很好的概述:

http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

最后注意:由于您的服务绑定具有[securitymode="Transport"],请确保您的服务器端证书配置正确以支持ssl。

问候,

我通过两种方式解决了这个问题:

  1. 对于Transport安全模式,必须启用SSL,并且必须在SSL通道中调用服务
  2. 对于TransportCredentialOnly安全模式,Restful服务与http配合良好

@adkSerenity:感谢SSL的建议和跟踪。

谨致问候,Vu