1

将 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 中托管它时,我的问题就出现了。

非常欢迎任何想法。

4

1 回答 1

0

我的问题最终与 REST 和我的配置无关。我通过将以下内容添加到配置来启用 WCF Web 服务:

<system.diagnostics>
  <sources>
    <source name="System.ServiceModel"
            switchValue="Information, ActivityTracing"
            propagateActivity="true" >
      <listeners>
        <add name="xml"/>
      </listeners>
    </source>
    <source name="System.ServiceModel.MessageLogging">
      <listeners>
        <add name="xml"/>
      </listeners>
    </source>
    <source name="myUserTraceSource"
            switchValue="Information, ActivityTracing">
      <listeners>
        <add name="xml"/>
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add name="xml"
         type="System.Diagnostics.XmlWriterTraceListener"
         initializeData="C:\logs\Error.svclog" />
  </sharedListeners>
</system.diagnostics>

再次点击我的 Web 服务以获取错误,并在“SvcTraceViewer.exe”中弹出打开跟踪日志。这突出了我在数据库上使用集成安全性的事实。但是因为我通过浏览器和 IIS 托管站点访问该服务,所以我访问了应用程序池标识下的数据库。这突出显示了 WCF 跟踪中的此错误:

System.Data.SqlClient.SqlException: Cannot open database "XYZ" requested by the login. The login failed...

所以,长话短说,我是个傻瓜……

于 2013-01-29T01:19:43.093 回答