Error 404 When calling Restful Wcf Service

2019-07-29 00:44发布

I did the search again and again but I don't really know what am I missing. I have WCF method and after calling, the service return with error 404, resource can not be found.

The url like this: localhost:3522/AccountService.svc/accountbalance/1

I run it from Visual with IIS 8 Express.

The WCF method like this

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

And this is my 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" />

2条回答
Evening l夕情丶
2楼-- · 2019-07-29 01:29

I resolved the issue by 2 ways:

  1. For Transport security mode, SSL must be enabled and the service must be call in SSL channed.
  2. For TransportCredentialOnly security mode, the Restful service works fine with http.

@adkSerenity: thanks for the SSL suggestion, and tracing.

Best Regards, Vu

查看更多
等我变得足够好
3楼-- · 2019-07-29 01:33

Based on a brief review, the service section of the config file seems to be missing the baseAddresses element.

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

In addition, you may want to consider enabling WCF Tracing and Logging, so you can review the WCF service startup events for any errors. The following link provides a good overview:

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

Final note: since your service binding has [security mode="Transport"], make sure your server side certificate is configured correctly to support ssl.

Regards,

查看更多
登录 后发表回答