Azure Webjob - Per Request Lifetime?

China☆狼群 提交于 2019-12-11 17:30:18

问题


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.

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.


回答1:


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.

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?

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:

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.



来源:https://stackoverflow.com/questions/45710450/azure-webjob-per-request-lifetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!