Skip to main content
 首页 » 编程设计

c#之调用 Restful Wcf 服务时出现 404 错误

2024年02月13日23虾米哥

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

像这样的 url:localhost:3522/AccountService.svc/accountbalance/1

我使用 IIS 8 Express 从 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" /> 

请您参考如下方法:

根据简短的审查,配置文件的服务部分似乎缺少 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

最后说明:由于您的服务绑定(bind)具有 [security mode="Transport"],请确保您的服务器端证书配置正确以支持 ssl。

问候,