【问题标题】:custom application_start code for umbraco websiteumbraco 网站的自定义 application_start 代码
【发布时间】:2012-02-28 23:12:51
【问题描述】:

我读到要让 umbraco 在应用程序启动时运行我的代码,我需要从 umbraco.Global 继承并覆盖 Application_Start。我已经使用以下简单代码完成了此操作,该代码位于 umbraco 网站项目引用的它自己的程序集中,以及它的 bin 文件夹中。

public class AtomicF1Global : umbraco.Global
{        
    protected override void Application_Start(object sender, EventArgs e)
    {
        base.Application_Start(sender, e);

        new WindsorStarter().Start();

        throw new Exception("Reached Custom Global");
    }
}

那里的异常纯粹是为了向我证明它没有被调用。

据我所知,我需要做的就是我已经完成的事情。我不需要在任何地方更新 umbraco 表(就像对 umbraco 进行许多不同修改时的情况一样)。

但是,我的代码从未被调用,我也无法找出原因。我需要在某处注册吗?

我还检查以确保 bin 目录中不存在“App_Global.asax.dll”。

我还尝试在 umbraco 站点项目中创建一个 Global.asax,如下所示:

<%@ Application Language="C#" Inherits="umbraco.Global" %>
<%@ Import Namespace="atomicf1.domain" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Call Global base class first
        base.Application_Start(sender, e);


        // Code that runs on application startup
        new WindsorStarter().Start();

        throw new Exception("Reached Custom Global");

    }             
</script>

umbraco 的版本是 4.7.1 (.NET 4.0)。

【问题讨论】:

  • 您找到解决方案了吗?

标签: c# .net umbraco


【解决方案1】:

我在 Visual Studio 中创建了一个空的 MVC 解决方案并添加了 Umbraco 6.x。编辑 global.asax.cs 并遇到与您相同的问题:函数未触发。

解决方案看似简单,但有点棘手:

在 global.asax 文件中(如果你双击它,Visual Studio 打开 global.asax.cs,按“F7”或右键单击 global.asax 并选择“查看标记”)你必须更改“继承” “ 属性。查看 umbraco 的原始 global.asax:

<% Application Codebehind="Global.asax.cs" Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %>

改成这样:

<%@ Application Codebehind="Global.asax.cs" Inherits="YourGlobalClassName" Language="C#" %>

注意:如果 YourGlobalClassName 在命名空间内,请确保也包含命名空间,如下所示:

<%@ Application Codebehind="Global.asax.cs" Inherits="YourNameSpace.YourGlobalClassName" Language="C#" %>

另见:

custom controller documentation来自umbraco。

类似问题(针对 umbraco 6):solution in stackoverflow

【讨论】:

    【解决方案2】:

    实现您自己的 global.asax 是正确的方法,因为它将在应用程序启动时运行。

    我以与您类似的方式实现了我的 global.asax,但是我将代码放在单独项目中的单独 CS 文件中 - 但那只是我的方式。

    public class Global : umbraco.Global
    {
        protected override void Application_Start(object sender, System.EventArgs e)
        {
            base.Application_Start(sender, e);
    
            XmlConfigurator.Configure();
            ILog log = LogManager.GetLogger(typeof (Global));
            log.Debug("Application started");
        }
    
    }
    

    查看差异,您需要将protected override 添加到您的方法中。

    就我个人而言,我总是将日志记录(使用 log4net)添加到我的 global.cs 文件中,这样我就可以确保我的日志记录已启动并正在运行并且应用程序已重新启动。

    如果您的代码没有被拾取,您可能需要尝试修改 web.config 以强制重新启动应用程序,但也可以使用调试器逐步检查以确保您的代码不会被其他代码访问原因。

    【讨论】:

    • 谢谢。我在我的 AtomicF1Global 类中完全使用了你的方法,但它并没有被解雇。我试过触摸 web.config 并重新启动应用程序池等。我不知道为什么,因为我读过的所有内容都指向我最初尝试的(以及你的解决方案)作为要走的路。跨度>
    【解决方案3】:

    如果您希望代码在应用程序启动时运行,通常的做法是创建一个继承自 ApplicationBase 的类。当 Umbraco 应用程序启动时,从它继承的公共类被调用。以下是我们在 Umbraco 网站之一上使用的启动事件之一的示例代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.web;
    using umbraco.BasePages;
    
    namespace MySite.Events
    {
        public class EventHandlers : ApplicationBase
        {
            public EventHandlers()
            {
                //put your code to run on app startup here
            }
        }
    }
    

    【讨论】:

    • 我已将其标记下来,因为这不是添加代码以在启动时运行的正确方法。这就是您扩展 Umbraco 事件的方式,例如移动或复制节点。如果向它添加日志记录,您将看到它实际上可能在应用程序生命周期内被实例化多次。
    • FWIW 我也尝试过这种方法,在阅读了 Matt Brailsford 的博文之后。
    • @Digbyswift 但是如果你需要运行依赖于 umbraco 核心的东西,你可以使用它。在 Application_Start 中运行它不会让您访问缓存的 xml 文档中的节点。所以这对于在引入 IApplicationStartupHandler 之前的站点来说是一个很好的方法
    【解决方案4】:

    我会使用WebActivator 包。然后,您可以让您的代码运行,而无需使用 global.asax 和覆盖 Umbraco 方法。

    请参阅this blog post by Bart Wullems 了解快速入门。

    【讨论】:

    【解决方案5】:

    我很欣赏这是一个非常古老的问题,但这是我在搜索类似问题时发现的为数不多的选项之一。

    我确定的解决方案与此线程中提到的不同,所以我想我也会提供它。

    我继续编写了一个 CustomWebModule,它可以位于 Web 请求和任何传入 umbraco 的请求之间。

    第一步是编写一个自定义的IHttpModule 实现

    namespace My.Application.Namespace
    {
        public class CustomWebModule : IHttpModule
        {
            static void AuthenticateRequest(object sender, EventArgs e){
                // DO ALL FANCY STUFF in my app and then make calls to the 
                // KEY umbraco functions from umbraco.core.security.AuthenticationExtensions 
                // "AuthenticateCurrentRequest" function.
                // 
                // specifically: setting the GenericPrincipal and related entities.
            }
    
            public void Dispose()
            {
                //throw new NotImplementedException();
                // make sure you don't write a memory leak!
                // dispose of everything that needs it!
            }
    
            public void Init(HttpApplication app)
            {
                app.AuthenticateRequest += AuthenticateRequest; 
            }
        }
    }
    

    请注意,“花哨的东西”部分所需的调用列在 Umbraco 核心代码库中:

    https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Core/Security/AuthenticationExtensions.cs

    然后将密钥添加到 web.config

    <system.web>
        <httpModules>
            <add name="CustomWebModule" type="My.Application.Namespace.CustomWebModule"/>
    

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
                <remove name="CustomWebModule"/>
                <add name="CustomWebModule" type="My.Application.Namespace.CustomWebModule"/>
    

    【讨论】:

    • 有点苛刻,但我喜欢它
    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    相关资源
    最近更新 更多