【问题标题】:REST methods not accessible when hosting wcf service in IIS在 IIS 中托管 wcf 服务时无法访问 REST 方法
【发布时间】:2010-07-12 18:19:21
【问题描述】:

我有一个 WCF REST 服务,它在类 GreetService 中公开一个方法:

[ServiceContract]
public class GreetService
{
    [WebGet(UriTemplate = "greet/{name}")]
    public String GreetName(string name)
    {
        return "Hello " + name;
    }
}

另外,我在 Global.asax 中注册了一条路线:

RouteTable.Routes.Add(new ServiceRoute("GreetService", new WebServiceHostFactory(), typeof(GreetService)));

现在,当我直接从 Visual Studio 运行它时,我可以利用 UriTemplate 并使用 GET 调用来调用此方法 http://localhost:5432/GreetService/greet/JohnDoe

但是,通过为其创建 Greet.svc 文件将其部署到 IIS7 后,我观察到以下行为:

任何想法为什么 WebGetAttribute 在 IIS 中不起作用?还是我做错了什么?

编辑: 这是我的 web.config 文件的 ServiceModel 部分,它位于 IIS 使用的目录中:

<system.serviceModel>
    <!-- <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> -->
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
</system.serviceModel>

编辑 2:为了完整起见,这里是我的完整 web.config 文件:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule"
           type="System.Web.Routing.UrlRoutingModule,
           System.Web, Version=4.0.0.0,
           Culture=neutral,
           PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <handlers>
      <add name="UrlRoutingHandler"
         preCondition="integratedMode"
         verb="*" path="UrlRouting.axd"
         type="System.Web.HttpForbiddenHandler, 
         System.Web, Version=4.0.0.0, Culture=neutral, 
         PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers>
  </system.webServer>

  <system.serviceModel>
     <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>--> 
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

</configuration>

【问题讨论】:

  • 请发布 web.config,至少 system.servicemodel 部分:)
  • 感谢您的努力,我一开始就应该这样做。我正在使用 StandardEndpoint,不确定这对行为意味着什么。
  • 您是如何专门部署到 IIS 中的?您是否正在更改项目的“Web”选项卡以进入 IIS vdir?你真的有 IIS 在端口 5432 上侦听吗? (基于您在部署到 IIS 后尝试的事情)。通常,在 Web 选项卡中切换到本地 IIS 后,您会点击 localhost/MyProject/GreetService/greet/JohnDoe 或类似

标签: wcf rest iis-7 webget


【解决方案1】:

如果您将路线定义为:

new ServiceRoute("GreetService", .....

那么您应该可以拨打您的服务电话

http://localhost:5432/YourVirtualDirectory/GreetService/greet/JohnDoe

如果您的 Web 应用程序部署到您的 IIS 根目录(不在虚拟目录中),则为:

http://localhost:5432/GreetService/greet/JohnDoe

在定义 ServiceRoute 时,这样做是为了摆脱必须指定 Greet.svc 文件,真的 - ServiceRoute 条目已经包含 IIS 实例化您的服务并调用它所需的所有信息 - 不需要您的 URL 中涉及的 *.svc 文件(svc 文件基本上包含与您的 ServiceRoute 条目相同的信息)。

【讨论】:

  • 我将它部署到它自己的 IIS 网站中,而不是虚拟目录中。我还尝试省略 .svc 文件并构建您最后提到的 URL,但它不起作用。但是感谢您确认这是要走的路,我一定犯了一些愚蠢的错误。
【解决方案2】:

global.asax.cs 中的行更改为:

RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(GreetService)));

并将以下内容放入您的 web.config&lt;configuration&gt; 节点下:

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule"
           type="System.Web.Routing.UrlRoutingModule, 
          System.Web.Routing, Version=4.0.0.0, 
          Culture=neutral, 
          PublicKeyToken=31BF3856AD364E35" />

    </modules>
    <handlers>
      <add name="UrlRoutingHandler"
         preCondition="integratedMode"
         verb="*" path="UrlRouting.axd"
         type="System.Web.HttpForbiddenHandler, 
         System.Web, Version=4.0.0.0, Culture=neutral, 
         PublicKeyToken=b03f5f7f11d50a3a" />
    </handlers>
  </system.webServer>

(请确保您使用的是正确的 .NET 版本)并看看它对您有什么作用。

【讨论】:

  • 没什么,很遗憾 - 我无法让它工作。不过,我可以访问位于我的应用程序目录中的静态文件 - 但似乎所有路由都丢失了。例如,首选方法是将其部署为直接在默认网站下的应用程序,对吗?
  • 不,这也没有帮助,但我认为这个信息很重要,因为我也在其他地方找到了它,以及将 放入 部分
【解决方案3】:

注意:请发布 web.config,至少是 system.servicemodel 部分。

您通常使用基于路由的配置或 .svc 文件,而不是两者,但这与您的问题正交。 FWIW,您应该能够在服务正常工作后终止 .svc 文件并使用路由。

既然您能够生成 WSDL 并调用它,那听起来您可能没有 webhttp 作为端点行为?

确保您有这样定义的端点行为(当然可以是差异名称)

    <endpointBehaviors> 
        <behavior name="webHttpBehavior"> 
            <webHttp /> 
        </behavior> 
    </endpointBehaviors> 

然后确保您的服务端点包含 behaviorConfiguration="webHttpBehavior"

【讨论】:

【解决方案4】:

问题是机器配置缺少以下部分

<configSections>
    <sectionGroup name="system.serviceModel" type="System.ServiceModel.Configuration.ServiceModelSectionGroup, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <section name="standardEndpoints" type="System.ServiceModel.Configuration.StandardEndpointsSection, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </sectionGroup>
</configSections>

将其添加到 web.config 顶部(在 &lt;configuration&gt; 的开始标记之后)应该可以解决此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多