Azure Webjob-每个请求生命周期? [英] Azure Webjob - Per Request Lifetime?

查看:133
本文介绍了Azure Webjob-每个请求生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序,我正在使用它来进行天蓝色的webjob.我需要针对每个Azure网络作业请求进行唯一的休眠会话.我正在使用自动事实来管理DI.

I have a console app that I'm using for an azure webjob. I need to have a unique nhibernate session per azure webjob request. I'm using autofact to manage DI.

我如何才能在Azure Webjobs中按请求获得终身实例化?控制台应用程序本来就没有此功能.我需要更改项目类型吗?

How can I get Per Request Lifetime instancing in azure webjobs? Inherently a console app doesn't have this. Do I need to change project types?

我已经看到有关如何执行类似操作的几个答案此处.但是,它们基本上可以归结为将容器作为函数的参数传入.并不是每个请求都是实例.

I've seen several answers on how to do something similar here and here. But they basically boil down to passing in a container as a parameter to functions. That's not really instance per request.

推荐答案

据我所知,webjob没有请求.它只是在App Service Web Apps上将程序作为后台进程运行.无法收到请求.

As far as I know, the webjob doesn't have the request. It just run programs as background processes on App Service Web Apps. It couldn't get the request.

我认为,按请求生命周期"实例化可用于Web应用程序(例如ASP.NET Web表单)和MVC应用程序而不是webjobs.

In my opinion, the Per Request Lifetime instancing is used in web application like ASP.NET web forms and MVC applications not webjobs.

您对请求的意思是什么?

What do you mean of the request?

通常,我们将使用AutofacJobActivator在webjobs中使用每个依赖实例".

Normally, we will use the Instance Per Dependency in the webjobs by using AutofacJobActivator.

触发该功能后,它将自动创建新实例.

It will auto create new instance when the function is triggered.

这是一个网络作业示例:

Here is a webjob example:

class Program
{
    // Please set the following connection strings in app.config for this WebJob to run:
    // AzureWebJobsDashboard and AzureWebJobsStorage
    static void Main()
    {
        var builder = new ContainerBuilder();
         builder.Register(c =>
        {
            var model = new DeltaResponse();
            return model;
        })
     .As<IDropboxApi>()
     .SingleInstance();
     builder.RegisterType<Functions>().InstancePerDependency();
        var Container = builder.Build();
        var config = new JobHostConfiguration()
        {
            JobActivator = new AutofacJobActivator(Container)
        };

        var host = new JobHost(config);
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
}

public class AutofacJobActivator : IJobActivator
{
    private readonly IContainer _container;

    public AutofacJobActivator(IContainer container)
    {
        _container = container;
    }

    public T CreateInstance<T>()
    {
        return _container.Resolve<T>();
    }
}

public interface IDropboxApi
{
    void  GetDelta();
}

public class DeltaResponse : IDropboxApi
{
    public Guid id { get; set; }

    public DeltaResponse()
    {
        id = Guid.NewGuid();
    }
    void IDropboxApi.GetDelta()
    {
        Console.WriteLine(id);
        //throw new NotImplementedException();
    }
}

Functions.cs:

Functions.cs:

public class Functions
{
    // This function will get triggered/executed when a new message is written 
    // on an Azure Queue called queue.

    private readonly IDropboxApi _dropboxApi;

    public Functions(IDropboxApi dropboxApi)
    {
        _dropboxApi = dropboxApi;
    }


    public void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
    {
        log.WriteLine("started");

        // Define request parameters.
        _dropboxApi.GetDelta();
    }
}

函数触发后,它将自动创建新实例.

When the function triggered, it will auto create new instance.

这篇关于Azure Webjob-每个请求生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆