简体   繁体   English

如何在OWIN自托管应用中预加载数据

[英]How to pre-load data in an OWIN self-hosted app

I have a OWIN/Katana self-hosted service app. 我有一个OWIN / Katana自托管服务应用程序。 One of its functions is to service some data over WebAPI. 其功能之一是通过WebAPI服务某些数据。 In this app I have a class called dataManager, which is responsible for retrieving the data, and passing it onto the API controller, which asked for it. 在这个应用程序中,我有一个名为dataManager的类,该类负责检索数据,并将其传递给要求它的API控制器。 The data is ultimately served to a mobile platform, so it is very important to cache as much as possible for performance. 数据最终将提供给移动平台,因此,尽可能多地缓存性能对于保持非常重要。 Is there a way to pre-load my DataManager at the application startup, and have it pre-execute it's linq queries? 有没有一种方法可以在应用程序启动时预加载我的DataManager,并使其预执行linq查询?

The Application class looks like this: Application类如下所示:

namespace TaskManager
{
  using System;
  using Microsoft.Owin.Hosting;

  public class TaskManagerApplication
  {
    protected IDisposable WebApplication;

    public void Start()
    {
      WebApplication = WebApp.Start<WebPipeline>("http://*:8080");
    }

    public void Stop()
    {
      WebApplication.Dispose();
    }
  }
}

The Program class looks like this: Program类如下所示:

namespace TaskManager
{
  using Topshelf;

  internal class Program
  {
    private static int Main()
    {
      var exitCode = HostFactory.Run(host =>
      {
        host.Service<TaskManagerApplication>(service =>
        {
          service.ConstructUsing(() => new TaskManagerApplication());
          service.WhenStarted(a => a.Start());
          service.WhenStopped(a => a.Stop());
        });

        host.SetDescription("Task Manager");
        host.SetDisplayName("Task Manager");
        host.SetServiceName("TaskManager");

        host.RunAsNetworkService();
      });

      return (int) exitCode;
    }
  }
}

And the data retrieval statement contained within DataManager class look like this: DataManager类中包含的数据检索语句如下所示:

var rawData = from data in new XPQuery<AccountView3.PipelineData>(uow)
                               where data.Stage.ToLower().Contains("won")
                                   && data.RevenueStartDate.Value.Year == DateTime.Today.Year
                                   && data.WeekOfTheYear >= priorWeekCutoff
                               select data;

What I do is create a public static class in the API library. 我要做的是在API库中创建一个公共静态类。 That's where I modify the HttpConfiguration object. 那是我修改HttpConfiguration对象的地方。 That is also where I define OnStartup() and OnShutdown() methods. 这也是我定义OnStartup()OnShutdown()方法的地方。 I then call these methods in the pipeline class's methods (your WebPipeline class). 然后,我在管道类的方法(您的WebPipeline类)中调用这些方法。

For example (in the MyWebApi library, where my controllers and stuff live): 例如(在MyWebApi库中,其中包含我的控制器和内容):

public class Service
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.EnsureInitialized();
    }

    public static void OnStartup()
    {
        // add any startup logic here, like caching your data
    }

    public static void OnShutdown()
    {
        // add any cleanup logic here
    }
}

Then in the pipeline class: 然后在管道类中:

public class WebPipeline
{
    public static void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();

        MyWebApi.Service.Register(config);
        MyWebApi.Service.OnStartup();

        app.UseWebApi(config);
    }

    public static void Shutdown()
    {
        MyWebApi.Service.OnShutdown();
    }
}

Now your TaskManagerApplication.Start() will result in the API OnStartup() being called. 现在,您的TaskManagerApplication.Start()将导致调用API OnStartup() Then you just have to add a call to WebPipeline.Shutdown() in your TaskManagerApplication.Stop() method. 然后,您只需要在TaskManagerApplication.Stop()方法中添加对WebPipeline.Shutdown()的调用。

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

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