简体   繁体   English

MVC:在Application启动时运行方法,而不从Application_Start调用它

[英]MVC: Run a method on Application startup without calling it from Application_Start

I have a class with a method that should run on application startup. 我有一个类应该在应用程序启动时运行。 I don't want to call this method directly from Application_Start event. 我不想直接从Application_Start事件中调用此方法。 What's the best way to get this class instantiated and method run on application_start? 将这个类实例化并在application_start上运行方法的最佳方法是什么?

In other words, I want to inject this code into application startup. 换句话说,我想将此代码注入应用程序启动。

I've noticed that some people use WebActivatorEx.PostApplicationStartMethod. 我注意到有些人使用WebActivatorEx.PostApplicationStartMethod。 I've not delved into the details but it is the first place I would look. 我没有仔细研究细节,但这是我看的第一个地方。 Here's an example of a class registered to automatically run when RegisterBundles is called. 这是一个注册为在调用RegisterBundles时自动运行的类的示例。 One of the other hooks may be what you are looking for. 其中一个钩子可能就是你要找的东西。

[assembly: WebActivatorEx.PostApplicationStartMethod(typeof(BootstrapBundleConfig), "RegisterBundles")]

namespace Deloitte.EmploymentMemo.Presentation.App_Start
{
    public class BootstrapBundleConfig
    {
        public static void RegisterBundles()
        {
            // Add @Styles.Render("~/Content/bootstrap") in the <head/> of your _Layout.cshtml view
            // For Bootstrap theme add @Styles.Render("~/Content/bootstrap-theme") in the <head/> of your _Layout.cshtml view
            // Add @Scripts.Render("~/bundles/bootstrap") after jQuery in your _Layout.cshtml view
            // When <compilation debug="true" />, MVC4 will render the full readable version. When set to <compilation debug="false" />, the minified version will be rendered automatically
            BundleTable.Bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include("~/Scripts/bootstrap.js"));
            BundleTable.Bundles.Add(new StyleBundle("~/Content/bootstrap").Include("~/Content/bootstrap.css"));
            BundleTable.Bundles.Add(new StyleBundle("~/Content/bootstrap-theme").Include("~/Content/bootstrap-theme.css"));
        }
    }
}

One of possible solutions to use OWIN startup. 使用OWIN启动的可能解决方案之一。

Install nuget package: install-package Microsoft.Owin.Host.SystemWeb 安装nuget包: install-package Microsoft.Owin.Host.SystemWeb

Add to appsettings startup class: 添加到appsettings启动类:

   <appSettings>
          <add key="owin:appStartup" value="MyProject.Code.Startup" />
   </appSettings>

And by convention you will need class with method called Configuration: 按照惯例,您将需要使用名为Configuration的方法的类:

    public class Startup
    {
            public void Configuration(IAppBuilder app)
            {
                app.Run(context =>
                {
                    string t = DateTime.Now.Millisecond.ToString();
                    return context.Response.WriteAsync(t + " Production OWIN App");
                });
            }
    }

Or do anything you need. 或做任何你需要的事情。

If you interesting in it, check it asp.net: OWIN and Katana project 如果您对它感兴趣,请检查它是否为asp.net:OWIN和Katana项目

Use delegates. 使用代表。 They hold references to methods and that give you possibility to call some methods at one time; 它们包含对方法的引用,并且可以同时调用某些方法; An example of using delegates : 使用委托的示例:

public delegate void myDelegate();

private static void Main()
{
    myDelegate myFunctions = Method1; //initialize delegate with Method1
    myFunctions += Method2;           //Add Method2 to delegate

    myFunctions(); //call all methods added to delegate
}

public static void Method1()
{
    Console.WriteLine("Hello from method1");
}

public static void Method2( )
{
    Console.WriteLine("Hello from method2");
}

This will call both Method1 and Method2 这将调用Method1和Method2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM