简体   繁体   中英

WebJob Using Entity Framework Core: Accessing Site Configuration Information

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 :)

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' 在此处输入图片说明

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM