0

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.

richard
  • 12,263
  • 23
  • 95
  • 151
  • what kind of trigger are you using ?? – Thomas Aug 17 '17 at 02:52
  • azure queue trigger – richard Aug 17 '17 at 06:47
  • 1
    queue or servicebus queue ? have a look at this answer https://stackoverflow.com/questions/35186456/azure-triggered-webjobs-scope-for-dependency-injection and let me know :-) – Thomas Aug 17 '17 at 06:56
  • azure storage queue. – richard Aug 17 '17 at 07:27
  • Haha yeah that's the best answer I've found so far. Overriding the IQueueProcessorFactory. We are working on trying that now. Is that a hack or is that the intended use? Is that intended to be overridden? – richard Aug 17 '17 at 07:29
  • So the IjobActivator allow you to inject dependencies and the IQueueProcessorFactory allows you to perform some message specific logic such as create a new scope – Thomas Aug 17 '17 at 21:09

1 Answers1

0

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.

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65