簡體   English   中英

umbraco網站的自定義application_start代碼

[英]custom application_start code for umbraco website

我讀到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)。

我在Visual Studio中創建了一個空的MVC解決方案,並添加了Umbraco 6.x. 編輯global.asax.cs並遇到與您相同的問題:函數未觸發。

解決方案似乎很簡單,但有點棘手:

在global.asax文件中(如果雙擊它,visual studio打開global.asax.cs,按“F7”或右鍵單擊global.asax並選擇“View Markup”),您必須更改“Inherits”屬性。 從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#" %>

也可以看看:

來自umbraco的原始自定義控制器文檔

類似的問題(針對umbraco 6): stackoverflow中的解決方案

實現自己的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以強制重新啟動應用程序,同時還要逐步調試調試器以確保您的代碼由於其他原因而無法訪問。

我很欣賞這是一個非常古老的問題,但這是我在搜索類似問題時發現的少數選項之一。

我已經解決的解決方案與本主題中提到的解決方案不同,所以我認為我也會提供它。

我繼續編寫了一個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"/>

如果您希望代碼在應用程序啟動時運行,那么您通常采用的方式是創建一個繼承自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
        }
    }
}

我會使用WebActivator包。 然后,您可以在不使用global.asax和覆蓋Umbraco方法的情況下運行代碼。

請參閱Bart Wullems的這篇博客文章,以便快速入門

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM