在IIS中托管WCF服务时无法访问REST方法 [英] REST methods not accessible when hosting wcf service in IIS

查看:75
本文介绍了在IIS中托管WCF服务时无法访问REST方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF REST服务,该服务公开了GreetService类中的方法:

I have a WCF REST service that exposes a method in class GreetService:

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

我也在Global.asax中注册了一条路线:

Also, I registered a route in Global.asax:

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

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

Now when i run this directly from visual studio, I am able to leverage the UriTemplate and invoke this method using a GET call to http://localhost:5432/GreetService/greet/JohnDoe

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

However, after deploying this to IIS7 by creating a Greet.svc file for it, I am observing the following behavior:

  • I can call http://localhost:5432/Greet.svc and it says that a service has been created
  • I can point wcftestclient to http://localhost:5432/Greet.svc?wsdl to generate a test client which can call GreetName() directly
  • However, I can't call http://localhost:5432/Greet.svc/GreetService/greet/JohnDoe nor http://localhost:5432/Greet.svc/greet/JohnDoe although I expected to be able to since I specified an empty relative endpoint address in the corresponding web.config file prior to hosting it in IIS7.

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

Any ideas why the WebGetAttribute is not working in IIS? Or is there something else I am doing wrong?

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

This is the ServiceModel part of my web.config file which resides in the directory that IIS uses:

<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>

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

EDIT 2: For completeness' sake here is my full web.config file:

<?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部分.

您通常使用基于路由的配置或.svc文件,而不是同时使用这两者,但这与您的问题正交. FWIW,一旦服务正常工作并使用路由,您应该能够杀死.svc文件.

You normally use either the Route-based configuration or a .svc file, not both, but that's orthogonal to your problem. FWIW, you should be able to kill the .svc file once you get the service working and just use the route.

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

Since you're able to generate WSDL and call it, that sounds like you might not have webhttp as an endpoint behavior?

确保您已定义了这样的端点行为(当然可以是diff名称)

Make sure you have an endpoint behavior defined like this (can be a diff name of course)

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

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

and then make sure your service endpoint includes behaviorConfiguration="webHttpBehavior"

这篇关于在IIS中托管WCF服务时无法访问REST方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆