Web应用程序中的WCF服务无法通过RESTCLIENT工作?

时间:2013-12-17 06:04:47

标签: c# asp.net wcf restful-url rest-client

我在Web应用程序中添加了一个RESTful WCF服务(Righclicked解决方案并添加了WCF服务),并且在运行它时将URL公开为svcutil.exe http://localhost:62783/Service1.svc?wsdl,但我尝试从像{{{{}}这样的RESTCLIENT调用该服务UriTemplate。它会抛出像

这样的错误

http://localhost:62783/AuthenticateUser

但是,如果我创建一个单独的RESTful WCF服务并从RESTCLIENT调用工作正常。这是我的代码

HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

 [OperationContract]
        string AuthenticateUser1();

和配置

 [WebInvoke(UriTemplate = "/AuthenticateUser", Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
        public string AuthenticateUser1()
        {

            return string.Format("Token {0}", new Guid().ToString());
        }

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

根据您发布的配置,您有一个SOAP basicHttpBinding的默认端点,它(默认情况下)映射到http方案。我在REST方面做得很少,但我相信您需要使用webHttpBinding添加端点来执行REST,并且很可能URL需要http://localhost:62783/Service1.svc/AuthenticateUser(请注意包含服务)文件),虽然我不是100%肯定那个。

要添加REST端点,请在服务的配置文件中执行以下操作:

<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior name="">
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
    <!-- Added for REST -->
    <endpointBehaviors>
      <behavior name="REST">
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <!-- REST endpoint -->
  <endpoint address="" binding="webHttpBinding"
            contract="<contract name with namespace>"
            behaviorConfiguration="REST"> 
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

同样,REST不是我的强项,但至少应该让你指向正确的方向。