将 WCF Rest 服务发布到 IIS 时出现问题。这一切都适用于 VS,但是当通过 IIS 运行时,我不断收到错误请求。
一些示例代码,我的服务就这么简单:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Admin
{
[WebGet(UriTemplate = "Field/GetAll")]
public IList<EntityFields> GetFields() ...
我已将路线添加到我的 global.asax:
RouteTable.Routes.Add(new ServiceRoute("Admin", new WebServiceHostFactory(), typeof(Admin)));
我的 web.config 看起来像这样。我希望这是我做错了什么。请注意,这已经从阅读这里和 MS 论坛等多次改变和变形。
<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>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
如果它们有用,请注意:
- IIS 7.5
- 视窗服务器 2008 R2
- 我通过 AJAX 调用使用 JSONP 调用了很多方法。
- 因为我正在使用 JSONP,所以我设法让它工作的唯一方法是将所有必需的方法转换为 GET 并使用流来调用回调
- 当我通过 Visual Studio 启动时,一切正常。当我在 IIS 中托管它时,我的问题就出现了。
非常欢迎任何想法。