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

标签 c# .net web-services wcf rest

我搜索了一遍又一遍,但我真的不知道我错过了什么。我有 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。

问候,

关于c# - 调用 Restful Wcf 服务时出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20655519/

相关文章:

c# - Revit,如何获取选定墙的位置、长度和高度

c# - 类型转换,Cast<T> 的运行时替代方案?

c# - 新项目的 Linq to SQL

c# - 将 winform .net Framework 3.5 迁移到 4 时出错

c# - 自动部署,Global.asax : Could not load type Namespace. Global

php - PHP:显示来自Web服务的图像

c# - automapper 无法解析方法 formember

c# - 通过方法传入类型时如何使用 'is'?

.net - 在没有 Visual Studio 2010 的情况下安装 MSBuild 4.0

javascript - 数据源查询回调问题(调用顺序、改变全局变量的能力)