This site uses cookies to deliver our services. By using this site, you acknowledge that you have read and understand our Cookie and Privacy policy. Your use of Kontext website is subject to this policy. Allow Cookies and Dismiss

Instantiate a Service in ConfigureServices Method in .NET Core

29 views 0 comments last modified about 3 months ago Raymond Tang

dotnet core lite-log

.NET Core is built in with dependency injection. Usually method ConfigureServices in Startup class is used to register services in the container.

The signature of the method looks like the following:

public void ConfigureServices(IServiceCollection services)
{

}

Sometimes it may be necessary to instantiate a service in this function. To do that, you can use the following method:

var sp = services.BuildServiceProvider();
var dbConfig = sp.GetService<IOptions<DatabaseConfig>>().Value;

In the above example, the BuildServiceProvider function is invoked to create a service provider object which then is used to get service.

Related pages

Set AttachDbFilename as Relative Path in .NET Core

135 views   0 comments last modified about 3 months ago

.NET Framework, you can use |DataDirectory| to configure connection string when connecting to SQL Server database file via attach mode: AttachDbFilename=|DataDirectory|\dbname.mdf In .NET Core, you cannot directly set SQL Server Express connec...

View detail

Set AttachDbFilename as Relative Path in .NET Core

135 views   0 comments last modified about 3 months ago

.NET Framework, you can use |DataDirectory| to configure connection string when connecting to SQL Server database file via attach mode: AttachDbFilename=|DataDirectory|\dbname.mdf In .NET Core, you cannot directly set SQL Server Express connec...

View detail

ASP.NET Core 2.1 Error - 'Cyrillic' is not a supported encoding name

141 views   0 comments last modified about 3 months ago

After upgrading to ASP.NET Core 2.1 (.NET Core SDK 2.1.301), you may encounter the following error about encoding: System.ArgumentException &nbsp;&nbsp; HResult=0x80070057 &nbsp;&nbsp; Message='Cyrillic' is not a supported encoding name. For information on defining a custo...

View detail

SQLite in .NET Core with Entity Framework Core

83 views   0 comments last modified about 3 months ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

View detail

SQLite in .NET Core with Entity Framework Core

83 views   0 comments last modified about 3 months ago

SQLite is a self-contained and embedded SQL database engine. In .NET Core, Entity Framework Core provides APIs to work with SQLite. This page provides sample code to create a SQLite database using package Microsoft.EntityFrameworkCore.Sqlite . Create sample project ...

View detail

Graphics Programming and Image Processing in .NET Core 2.x

47 views   0 comments last modified about 3 months ago

In .NET Core 2.x, Windows Forms or WPF are not implemented since they are based on GDI+ and DirectX respectively in Windows. In .NET Core 3.0, there is plan to add Desktop Packs which includes UWP. WPF and Windows Forms. However, they will still be Windows-only. In .NET Core applications, you may...

View detail

Add comment

Please login first to add comments.  Log in New user?  Register

Comments (0)

No comments yet.