How to use HostBuilder for WebJob?












2














With .Net 2.0 was introduced the really usefull HostBuilder for Console App like we have with WebHostBuilder for Web Application.



My concern is now how to implement the HostBuilder with WebJob with a QueueTrigger?



Until now, I was using JobActivator:



        var startup = new Startup();
var serviceProvider = startup.ConfigureServices(new ServiceCollection());
startup.Configure(serviceProvider);

var jobHostConfiguration = new JobHostConfiguration()
{
JobActivator = new JobActivator(serviceProvider),
};

var host = new JobHost(jobHostConfiguration);
host.RunAndBlock();


For a full sample, here is my code:
https://github.com/ranouf/DotNetCore-CosmosDbTrigger-WebJob/tree/master/CosmosDbTriggerWebJob.App



Is there someone who already use HostBuilder for a WebJob with a QueueTrigger? Is it possible?



Thanks










share|improve this question






















  • Yes it is possible, I've posted the code for using a service bus queue trigger below.
    – The Senator
    Oct 17 at 16:31
















2














With .Net 2.0 was introduced the really usefull HostBuilder for Console App like we have with WebHostBuilder for Web Application.



My concern is now how to implement the HostBuilder with WebJob with a QueueTrigger?



Until now, I was using JobActivator:



        var startup = new Startup();
var serviceProvider = startup.ConfigureServices(new ServiceCollection());
startup.Configure(serviceProvider);

var jobHostConfiguration = new JobHostConfiguration()
{
JobActivator = new JobActivator(serviceProvider),
};

var host = new JobHost(jobHostConfiguration);
host.RunAndBlock();


For a full sample, here is my code:
https://github.com/ranouf/DotNetCore-CosmosDbTrigger-WebJob/tree/master/CosmosDbTriggerWebJob.App



Is there someone who already use HostBuilder for a WebJob with a QueueTrigger? Is it possible?



Thanks










share|improve this question






















  • Yes it is possible, I've posted the code for using a service bus queue trigger below.
    – The Senator
    Oct 17 at 16:31














2












2








2







With .Net 2.0 was introduced the really usefull HostBuilder for Console App like we have with WebHostBuilder for Web Application.



My concern is now how to implement the HostBuilder with WebJob with a QueueTrigger?



Until now, I was using JobActivator:



        var startup = new Startup();
var serviceProvider = startup.ConfigureServices(new ServiceCollection());
startup.Configure(serviceProvider);

var jobHostConfiguration = new JobHostConfiguration()
{
JobActivator = new JobActivator(serviceProvider),
};

var host = new JobHost(jobHostConfiguration);
host.RunAndBlock();


For a full sample, here is my code:
https://github.com/ranouf/DotNetCore-CosmosDbTrigger-WebJob/tree/master/CosmosDbTriggerWebJob.App



Is there someone who already use HostBuilder for a WebJob with a QueueTrigger? Is it possible?



Thanks










share|improve this question













With .Net 2.0 was introduced the really usefull HostBuilder for Console App like we have with WebHostBuilder for Web Application.



My concern is now how to implement the HostBuilder with WebJob with a QueueTrigger?



Until now, I was using JobActivator:



        var startup = new Startup();
var serviceProvider = startup.ConfigureServices(new ServiceCollection());
startup.Configure(serviceProvider);

var jobHostConfiguration = new JobHostConfiguration()
{
JobActivator = new JobActivator(serviceProvider),
};

var host = new JobHost(jobHostConfiguration);
host.RunAndBlock();


For a full sample, here is my code:
https://github.com/ranouf/DotNetCore-CosmosDbTrigger-WebJob/tree/master/CosmosDbTriggerWebJob.App



Is there someone who already use HostBuilder for a WebJob with a QueueTrigger? Is it possible?



Thanks







asp.net-core .net-core asp.net-core-2.0 azure-webjobs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 22 at 16:08









Cedric Arnould

326213




326213












  • Yes it is possible, I've posted the code for using a service bus queue trigger below.
    – The Senator
    Oct 17 at 16:31


















  • Yes it is possible, I've posted the code for using a service bus queue trigger below.
    – The Senator
    Oct 17 at 16:31
















Yes it is possible, I've posted the code for using a service bus queue trigger below.
– The Senator
Oct 17 at 16:31




Yes it is possible, I've posted the code for using a service bus queue trigger below.
– The Senator
Oct 17 at 16:31












1 Answer
1






active

oldest

votes


















1














Right then, I've figured this out. Firstly ensure that you've upgraded your packages to use the latest v3 versions of the appropriate WebJob packages.



I found that I needed the following:




  1. Microsoft.Azure.WebJobs

  2. Microsoft.Azure.WebJobs.Core

  3. Microsoft.Azure.WebJobs.Extensions.ServiceBus


Then you can use the builder in your Main method for the Console project as follows:



 static async Task Main()
{
var builder = new HostBuilder();
builder.ConfigureAppConfiguration(cb =>
{
cb.AddJsonFile("appsettings.json");
});
builder.ConfigureWebJobs(b =>
{
b.AddServiceBus();
});
await builder.RunConsoleAsync();
}


Note that I've enabled the language feature 7.1 to support async Main methods. You can use 'Wait()' instead if you prefer. Then I added an appsettings.json file to my project as follows:



{
"ConnectionStrings": {
"AzureWebJobsServiceBus": "Endpoint=sb://..."
}
}


(EDIT: and ensured the file is copied always to the output folder)
And finally and most importantly I modified the trigger function to include the name of the Connection as follows:



    public static void ProcessQueueMessage([ServiceBusTrigger("[Your Queue Name]", Connection = "AzureWebJobsServiceBus")] string message, TextWriter log)
{
log.WriteLine(message);
}


Even though the name of the Connection is the default I still seemed to have to define it in my function attributes. I tried the 'Values' approach too but I couldn't make that work. I then started receiving messages from the service bus.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51970969%2fhow-to-use-hostbuilder-for-webjob%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Right then, I've figured this out. Firstly ensure that you've upgraded your packages to use the latest v3 versions of the appropriate WebJob packages.



    I found that I needed the following:




    1. Microsoft.Azure.WebJobs

    2. Microsoft.Azure.WebJobs.Core

    3. Microsoft.Azure.WebJobs.Extensions.ServiceBus


    Then you can use the builder in your Main method for the Console project as follows:



     static async Task Main()
    {
    var builder = new HostBuilder();
    builder.ConfigureAppConfiguration(cb =>
    {
    cb.AddJsonFile("appsettings.json");
    });
    builder.ConfigureWebJobs(b =>
    {
    b.AddServiceBus();
    });
    await builder.RunConsoleAsync();
    }


    Note that I've enabled the language feature 7.1 to support async Main methods. You can use 'Wait()' instead if you prefer. Then I added an appsettings.json file to my project as follows:



    {
    "ConnectionStrings": {
    "AzureWebJobsServiceBus": "Endpoint=sb://..."
    }
    }


    (EDIT: and ensured the file is copied always to the output folder)
    And finally and most importantly I modified the trigger function to include the name of the Connection as follows:



        public static void ProcessQueueMessage([ServiceBusTrigger("[Your Queue Name]", Connection = "AzureWebJobsServiceBus")] string message, TextWriter log)
    {
    log.WriteLine(message);
    }


    Even though the name of the Connection is the default I still seemed to have to define it in my function attributes. I tried the 'Values' approach too but I couldn't make that work. I then started receiving messages from the service bus.






    share|improve this answer




























      1














      Right then, I've figured this out. Firstly ensure that you've upgraded your packages to use the latest v3 versions of the appropriate WebJob packages.



      I found that I needed the following:




      1. Microsoft.Azure.WebJobs

      2. Microsoft.Azure.WebJobs.Core

      3. Microsoft.Azure.WebJobs.Extensions.ServiceBus


      Then you can use the builder in your Main method for the Console project as follows:



       static async Task Main()
      {
      var builder = new HostBuilder();
      builder.ConfigureAppConfiguration(cb =>
      {
      cb.AddJsonFile("appsettings.json");
      });
      builder.ConfigureWebJobs(b =>
      {
      b.AddServiceBus();
      });
      await builder.RunConsoleAsync();
      }


      Note that I've enabled the language feature 7.1 to support async Main methods. You can use 'Wait()' instead if you prefer. Then I added an appsettings.json file to my project as follows:



      {
      "ConnectionStrings": {
      "AzureWebJobsServiceBus": "Endpoint=sb://..."
      }
      }


      (EDIT: and ensured the file is copied always to the output folder)
      And finally and most importantly I modified the trigger function to include the name of the Connection as follows:



          public static void ProcessQueueMessage([ServiceBusTrigger("[Your Queue Name]", Connection = "AzureWebJobsServiceBus")] string message, TextWriter log)
      {
      log.WriteLine(message);
      }


      Even though the name of the Connection is the default I still seemed to have to define it in my function attributes. I tried the 'Values' approach too but I couldn't make that work. I then started receiving messages from the service bus.






      share|improve this answer


























        1












        1








        1






        Right then, I've figured this out. Firstly ensure that you've upgraded your packages to use the latest v3 versions of the appropriate WebJob packages.



        I found that I needed the following:




        1. Microsoft.Azure.WebJobs

        2. Microsoft.Azure.WebJobs.Core

        3. Microsoft.Azure.WebJobs.Extensions.ServiceBus


        Then you can use the builder in your Main method for the Console project as follows:



         static async Task Main()
        {
        var builder = new HostBuilder();
        builder.ConfigureAppConfiguration(cb =>
        {
        cb.AddJsonFile("appsettings.json");
        });
        builder.ConfigureWebJobs(b =>
        {
        b.AddServiceBus();
        });
        await builder.RunConsoleAsync();
        }


        Note that I've enabled the language feature 7.1 to support async Main methods. You can use 'Wait()' instead if you prefer. Then I added an appsettings.json file to my project as follows:



        {
        "ConnectionStrings": {
        "AzureWebJobsServiceBus": "Endpoint=sb://..."
        }
        }


        (EDIT: and ensured the file is copied always to the output folder)
        And finally and most importantly I modified the trigger function to include the name of the Connection as follows:



            public static void ProcessQueueMessage([ServiceBusTrigger("[Your Queue Name]", Connection = "AzureWebJobsServiceBus")] string message, TextWriter log)
        {
        log.WriteLine(message);
        }


        Even though the name of the Connection is the default I still seemed to have to define it in my function attributes. I tried the 'Values' approach too but I couldn't make that work. I then started receiving messages from the service bus.






        share|improve this answer














        Right then, I've figured this out. Firstly ensure that you've upgraded your packages to use the latest v3 versions of the appropriate WebJob packages.



        I found that I needed the following:




        1. Microsoft.Azure.WebJobs

        2. Microsoft.Azure.WebJobs.Core

        3. Microsoft.Azure.WebJobs.Extensions.ServiceBus


        Then you can use the builder in your Main method for the Console project as follows:



         static async Task Main()
        {
        var builder = new HostBuilder();
        builder.ConfigureAppConfiguration(cb =>
        {
        cb.AddJsonFile("appsettings.json");
        });
        builder.ConfigureWebJobs(b =>
        {
        b.AddServiceBus();
        });
        await builder.RunConsoleAsync();
        }


        Note that I've enabled the language feature 7.1 to support async Main methods. You can use 'Wait()' instead if you prefer. Then I added an appsettings.json file to my project as follows:



        {
        "ConnectionStrings": {
        "AzureWebJobsServiceBus": "Endpoint=sb://..."
        }
        }


        (EDIT: and ensured the file is copied always to the output folder)
        And finally and most importantly I modified the trigger function to include the name of the Connection as follows:



            public static void ProcessQueueMessage([ServiceBusTrigger("[Your Queue Name]", Connection = "AzureWebJobsServiceBus")] string message, TextWriter log)
        {
        log.WriteLine(message);
        }


        Even though the name of the Connection is the default I still seemed to have to define it in my function attributes. I tried the 'Values' approach too but I couldn't make that work. I then started receiving messages from the service bus.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 22:07









        Kevin R.

        2,58411525




        2,58411525










        answered Oct 17 at 16:16









        The Senator

        3,72022335




        3,72022335






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51970969%2fhow-to-use-hostbuilder-for-webjob%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word