WebJob Using Entity Framework Core: Accessing Site Configuration Information

asp.net-core azure-webjobs c# entity-framework entity-framework-core

Question

I'm building an ASP.NET Core MVC site which uses an EF Core Sql Server database.

In part of the site I need to add the ability to upload files, which then get processed and applied to the database. I thought I would use WebJobs for this.

What I'm not clear on is how I share the database connection string between my website and the webjob. Is there something analogous to the ConfigureServices() method in the website's Startup.cs which I can use in the webjob's program Main()?

Program Main

Per request, here is my current WebJobs code in all its glory:

public static void Main(string[] args)
{
    var config = new JobHostConfiguration();

    if( config.IsDevelopment )
    {
        config.UseDevelopmentSettings();
        config.DashboardConnectionString = "...";
        config.StorageConnectionString = "...";
    }

    JobHost host = new JobHost( config );
    host.RunAndBlock();
}

Not at lot there, but I'm just starting :)

1
0
8/11/2016 11:47:24 PM

Popular Answer

As I know, Azure Web app and WebJob share App setting and connection strings set on Azure portal. So we can define connection string on azure portal, then use ConfigurationManager to get the connection string. I did a small test to verify this for you.

1) Set connection string 'myconnection' with value 'test' enter image description here

2) use web job project to output the connection

 public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log)
        {
            log.WriteLine(message);
            string connectionString = ConfigurationManager.ConnectionStrings["myconnection"].ToString();
            Console.WriteLine("this is my webjob project console write " + connectionString);
        }

3) Then I see the result in Azure web job dashboard

enter image description here

1
8/11/2016 10:04:47 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow