【问题标题】:Simplest REST service not working最简单的 REST 服务不起作用
【发布时间】:2011-07-25 11:12:42
【问题描述】:

谁能告诉我为什么这个 REST 服务不起作用?

namespace WCFRestExample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class HelloWorld : IHelloWorld
    {
        [WebGet(UriTemplate="/", ResponseFormat=WebMessageFormat.Xml)]
        public string GetData()
        {
            return "HIIII";
        }
    }
}

namespace WCFRestExample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IHelloWorld
    {
        [OperationContract]
        string GetData();
    }
}

这是我的 web.config:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

我能够浏览服务 .svc 并且我可以正确获取页面,但是当我浏览时:

http://localhost:60503/HelloWorld.svc/GetData

我得到一个 404 页面。谁能告诉我发生了什么,我在哪里可以找到 WCF REST 的教程?这是可以创建的最简单的服务,但对我也不起作用。

提前致谢:)

【问题讨论】:

  • 是否有用于 REST 的 wsdl?不,至少在默认情况下,据我所知。这显然不是高度定制的解决方案:)
  • @Tom Squires:WSDL 用于 SOAP 服务而不是基于 Http 的 REST 服务。看来你也是新手;)
  • 显示 svc 文件内容,可能是 ServiceFactory 配置错误。
  • @Int3:
  • 这还不够,还需要提供servicefactory

标签: c# wcf rest .net-4.0


【解决方案1】:

您没有在任何地方定义这应该是 REST 服务 - 您的 web.config 中也没有使用 webHttpBinding 的端点,也没有在您的 *.svc 文件中指定使用 @987654324 @。

最简单的修复方法是将 svc 文件修复为:

<%@ ServiceHost Language="C#" Debug="true" 
      Service="WCFRestExample.HelloWorld" CodeBehind="HelloWorld.svc.cs"
       Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

有了这个,你的 svc 文件现在定义它想使用WebServiceHost(理解 REST 的主机)来托管你的服务...

查看An Introduction To RESTful Services With WCF,了解使用 WCF 的 REST 服务的精彩介绍。

【讨论】:

    【解决方案2】:

    尝试将您的 URI 模板更改为:

    [WebGet(UriTemplate="/GetData", ResponseFormat=WebMessageFormat.Xml)]
    

    更新

    uri模板中的位可以是任何东西,所以可以是:

    /GetMySuperComplicatedData 
    

    并且仍然被映射到GetData()

    更多信息请阅读:http://msdn.microsoft.com/en-us/library/bb675245.aspx

    【讨论】:

    • 尝试在 getData 末尾添加斜杠吗?所以 /GetData/ 就在请求和 uri 模板上
    【解决方案3】:

    试试http://localhost:60503/HelloWorld,然后告诉我们你的Global.asax

    【讨论】:

    • 我试过了。仍然收到 404 错误。项目中没有 global.asax。我应该加一个吗?我应该在里面写什么?
    • 您在创建项目时是否使用了 WCF 4.0 项目模板?它不是标准的,但你可以下载它(File -&gt; New Project -&gt; Online Templates -&gt; WCF -&gt; WCF REST Service Template 40(CS))。它将创建一个为您完成许多常见配置的项目,包括global.asax。看看那里,很清楚。
    【解决方案4】:

    这可能是您没有正确设置 ServiceHostFactory 的原因之一

    <%@ ServiceHost Language="C#"       
                    Service="WCFRestExample.HelloWorld"  
                    CodeBehind="HelloWorld.svc.cs"    
                    Factory="System.ServiceModel.Activation.WebServiceHostFactory"
    %>
    

    【讨论】:

      猜你喜欢
      • 2016-11-25
      • 2015-10-24
      • 2017-02-09
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      相关资源
      最近更新 更多