【问题标题】:Cannot access Elmah error log when using wcf使用 wcf 时无法访问 Elmah 错误日志
【发布时间】:2014-07-23 17:33:48
【问题描述】:

我一直在尝试将 ELMAH 与 WCF 一起使用,并且已将“正常”ErrorHandlerServiceErrorBehavior 添加到服务中。这些工作,我可以看到,当出现异常时,这些代码运行并向 ELMAH 记录错误。所以应该没问题,但是当我尝试访问错误日志页面 elmah.axd 时,我得到 HTTP 404,WCF 给了我“找不到端点页面”。

这意味着Elmah.ErrorLogPageFactory 未在该网址上使用。所以归结为我的web.config(我认为)。我就是搞不懂,这里是我的web.config 的相关部分:

我的服务端点设置如下:

<system.serviceModel>
   <standardEndpoints>
      <webHttpEndpoint>
         <standardEndpoint name="" helpEnabled="true" 
                           automaticFormatSelectionEnabled="true" />
      </webHttpEndpoint>
   </standardEndpoints>
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
                              multipleSiteBindingsEnabled="true" />
   <services>
       <service name="SaraService.SaraService"
                behaviorConfiguration="SaraSvcBehaviour" >
          <endpoint name="SaraService" 
              address="" 
              binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" 
              behaviorConfiguration="RestEndPointBehaviour"
              contract="SaraService.ISaraService" />
          <endpoint 
              address="soap" 
              behaviorConfiguration="SoapEndPoinstBehaviour" 
              binding="basicHttpBinding" 
              contract="SaraService.ISaraService" />
          <endpoint 
              address="mex" 
              binding="mexHttpBinding" 
              contract="IMetadataExchange" />
      </service>
   </services>
   <behaviors>
      <serviceBehaviors>
         <behavior name="SaraSvcBehaviour">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="true"/>
         </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
         <behavior name="RestEndPointBehaviour">
             <webHttp />
         </behavior>
         <behavior name="SoapEndPoinstBehaviour">
         </behavior>
      </endpointBehaviors>
   </behaviors>
   <bindings>
      <webHttpBinding>
         <binding name="webHttpBindingWithJsonP" 
                  crossDomainScriptAccessEnabled="true">
         </binding>
      </webHttpBinding>
   </bindings>
</system.serviceModel>

所以 / 和 /soap url 中有端点。

我的 ELMAH 配置是这样的:

<system.web>
   <compilation debug="true" targetFramework="4.5" />
   <httpRuntime targetFramework="4.5" />
   <httpModules>
       <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
       <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />
       <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />
   </httpModules>

</system.web>
</location>
<system.webServer>
  <handlers>
    <add name="ELMAH" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
  </handlers>
</system.webServer>

我还在根 url 中运行 AutofacServiceHostFactory。这是我的 global.asax.cs 的相关部分:

public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        var builder = new ContainerBuilder();
        builder.RegisterType<SaraService>();
        builder.RegisterType<MarkkinadataAccess.MarkkinadataEntities>().InstancePerLifetimeScope();
        builder.RegisterType<GenerisDbConnectionSettings>().As<IGenerisDbConnectionSetting>().SingleInstance();
        builder.RegisterType<GenerisApplication>().As<IGenerisApplication>().PropertiesAutowired().InstancePerLifetimeScope();

        builder.RegisterType<CrmProductsQuery>().As<ICrmProductsQuery>();
        builder.RegisterType <ProductsByNameQuery>().As<IProductsByNameQuery>();
        builder.RegisterType<ProductMWQueryByName>().As<IProductMWQueryByName>();
        builder.RegisterType<ProductMWQueryById>().As<IProductMWQueryById>();

        AutofacHostFactory.Container = builder.Build();

        RouteTable.Routes.Add(new ServiceRoute("", new AutofacServiceHostFactory(), typeof(SaraService)));
    }
}

所以我的猜测是,在 root 中运行服务端点或 AutofacServiceHostFactory 会以某种方式阻止我访问 /elmah.axd url 中的 ELMAH 日志页面。

但我只是不明白为什么或如何。

无论如何,我们将不胜感激。

【问题讨论】:

    标签: asp.net web-services wcf elmah


    【解决方案1】:

    好的,我现在得到了这个(至少部分)。罪魁祸首是我 global.asax 中的这一行:

    RouteTable.Routes.Add(new ServiceRoute("", new AutofacServiceHostFactory(), typeof(SaraService)));
    

    所以这将使服务端点成为我想要的根。

    另外,我只是想要一个路由(除 root 之外的任何路由)来访问 ELMAH 错误日志页面。这当然意味着必须有一个带有运行 Elmah.ErrorLogPageFactory 的处理程序的路由。

    现在这应该很简单,但是当我将服务添加到根 url 时,似乎无论如何我都无法使用处理程序 (axd) 访问任何路由。

    这对我来说似乎很奇怪,因为我可以在 www/index.html 中创建一个文件,并且完全不用接触 web.config 就可以很好地提供该文件。但是,对于像 elmah.axd 或 www/elmah.axd 这样的“生成”路由,我在根 url 中添加的服务将始终被执行。给我“无端点”页面,即 WCF 管道 HTTP 404。

    无论如何,如果我将我的服务放到 root 以外的某个 url,我可以访问 ELMAH 错误日志。

    是的,我确实尝试忽略 elmah.axd 网址,但它只是给了我正常的 404 错误。

    所以我想我只需要将我的服务放到 root 以外的其他地方,除非有人能想出更好的答案来做到这一点。

    【讨论】:

    • 感谢您的回答,我能够完全加载 Elmah Log Web 前端。解决方案是将 WCF 服务根路径更改为根以外的其他路径,即“”。我将 WCF 服务的根更改为“root/svc/..”,并且能够通过“root/elmah”访问 Elmah 网络日志
    猜你喜欢
    • 2016-03-10
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 2010-10-28
    • 2020-02-17
    • 1970-01-01
    相关资源
    最近更新 更多