【发布时间】: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 后,我观察到以下行为:
- 我可以打电话给http://localhost:5432/Greet.svc,它说已经创建了一个服务
- 我可以将 wcftestclient 指向http://localhost:5432/Greet.svc?wsdl 以生成可以直接调用 GreetName() 的测试客户端
- 但是,我无法调用 http://localhost:5432/Greet.svc/GreetService/greet/JohnDoe 或 http://localhost:5432/Greet.svc/greet/JohnDoe,尽管我希望能够调用,因为在将其托管在 IIS7 之前,我在相应的 web.config 文件中指定了一个空的相对端点地址。
任何想法为什么 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 或类似