locked
Using SQL Connection Core 3.1 in Worker Process? RRS feed

  • Question

  • User948188685 posted

    Ive created a .NET Core 3.1 Worker Process Project.  My appsettings.json file has my SQL Connection string defined in it.

    "ConnectionStrings": {
    "myCon": "Server=00.00.000.000;Database=xxxxx;User Id=xxxxx; Password=xxxxxxxxx;Trusted_Connection=True;"
    }

    Lots of sites say its as simple as doing: 

    string myConn = _configuration.GetConnectionString("myConn");

    I know its located in the Microsoft.Extensions.Configuration; namespace. But where/how do I define _configuration? 

    I want the connection string to be accessible to the entire worker process, not just the single Main() method. Does this mean the _connection object has to go into the AsyncStart() method?

    I cant seem to find clear documentation on how to get the Worker process to read/consume the connection string. Most everything I see online covers general .NET Core application projects and not Worker Process project types.

    Help is appreciated.  Trying my best to make that transition from .NET Frameworks to .NET Core 3 

    Monday, July 27, 2020 2:50 PM

All replies

  • User-474980206 posted

    the worker process should not access the configuration directly, as it should not dictate the configuration used. The hosting program should read the configuration and pass to worker constructor via DI like all the examples.

    I typically create a POCO object for the worker settings. Then in startup I read the config and add setting to services.

    Monday, July 27, 2020 2:58 PM
  • User948188685 posted

    If the worker process is running as a Windows Service, I don't see how another application is going to pass the connection object?

    This is a windows service that needs to execute calls to the database every X minutes. The process would have to have direct access to the connection string as far as I know.

    Monday, July 27, 2020 3:09 PM
  • User-474980206 posted

    just look at the command line examples. You use configuration builder 

            private static string GetDbConnection()
            {
                var builder = new ConfigurationBuilder()
                                    .SetBasePath(Directory.GetCurrentDirectory())
                                    .AddJsonFile("Parameters.json", optional: true, reloadOnChange: true);
     
                string strConnection = builder.Build().GetConnectionString("DbConnection");
     
                return strConnection;
            }
     

    Monday, July 27, 2020 3:33 PM
  • User1120430333 posted

    If the worker process is running as a Windows Service, I don't see how another application is going to pass the connection object?

    This is a windows service that needs to execute calls to the database every X minutes. The process would have to have direct access to the connection string as far as I know.

    On Windows service Onstart(), you should be able to get the connectionstring for  DI and inject it into any  class that needs the connectionstring similar to the Windows form Core 3.0, a  3 layers desktop solution,  where the Program.cs executes the constructor of the Startup.cs that in turn gets the connectionstriong form the appsettings.Json file and the connectionstring is used in the DAL in the Model folder. 

     https://github.com/darnold924/PubComanyWinCore

    Look into using the IOption pattern to get the connectionstring to where it is needed by using a class with get/set holdeing the connectionstring and DI the class as an object into any class the needs the connectionstring object.

    By looking at the examples, you should be bale to come up with code to get the connectionstring to where it is needed.

    https://corderoski.wordpress.com/2017/09/18/how-to-read-appsettings-json-from-class-library-in-asp-net-core/

    https://keestalkstech.com/2018/04/dependency-injection-with-ioptions-in-console-apps-in-net-core-2/

    Monday, July 27, 2020 4:32 PM