I will show how I use in my project which is also done through the Repository Pattern
and works perfectly.
Startup.Cs
Within the class Startup.cs
create a property of type IConfigurationRoot
, if there is no:
public IConfigurationRoot Configuration { get; }
That done, we go to the class builder Startup.cs
. Here your file should look like the code below.
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
if (env.IsDevelopment())
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
Configuration = builder.Build();
Environment = env;
}
Understand the .AddJsonFile
passing the file "appsettings.json"
which is where you usually put the connection string
. I believe that your project is also like this.
Also note the addition of this line Configuration = builder.Build();
.
Now to the method ConfigureServices
also in class Startup.cs
. Within the method, just below services.AddMvc()
, put the following code:
services.AddSingleton<IConfiguration>(Configuration);
Once this is done, we have successfully configured the DI for the IConfigurationRoot
.
Seurepositorio.Cs
Inside the class you want to access to connection string
, create a variable of type string
to guard his connection string
:
private readonly string _connectionString;
Once this is done, create your class constructor as follows, informing IConfiguration
as a parameter (it will be created through Dependency Injection):
public SuaClasseRepository(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("DefaultConnection");
}
Notice I used the configuration.GetConnectionString("DefaultConnection");
to get the connection string
of the archive appsettings.json
(defined in the Startup.Cs class) and that has the name DefaultConnection
.
Below follows as is my file appsettings.json
and it must follow with the same object names to avoid future problems:
"ConnectionStrings": {
"DefaultConnection": "myDatabaseConnectionString"
}
Ready. Now you have access to your connection string
in all the classes I created as shown above.
So by injecting the dependency into the API, I can see the Connection string that is in the
appsettings.json
of the API in theClass Library
? In fact, ASP.NET Core 2.0 no longer configures the file by defaultappsettings.json
?– Vinícius Avansini
Yes to both questions. But the appsettings.json file you can modify if you wish, add more than one Connection string, etc. I put the example of mine to you to see what it looks like and if it is with its string Connection inside.
– perozzo
Now I saw the structure of your project. It was not necessary to create another project only for Libraries. Since you did so, put an appsettings.json file in your tb Class Library and try to do what I reported above. I’ve never tested it that way to see if it works.
– perozzo
Thank you very much!! As you said, I wanted to separate things a little, even though I didn’t need to. But finally, injecting in the class
Startup
I can access the Library, just like you said.– Vinícius Avansini
Perfect! Good to know that in separate projects also works. I use the Repository Pattern, but all within the same project! Hug!
– perozzo
I see people commenting on separating, but unfortunately it takes a little work or not, I depend on your knowledge! Hug and thanks for the help again!
– Vinícius Avansini