Dependency Injection
The .NET SDK also support Dependency Injection to ease the use of the SurrealDbClient in your application.
Create a new project
Let's start by creating a new ASP.NET Core web app.
Define a Connection String
Open appsettings.Development.json and replace everything in there with the following code.
We have added a new Connection String called SurrealDB with the default configuration.
Register services
Open Program.cs and replace everything in there with the following code.
This code is using the AddSurreal() extension method to inject services automatically.
Notice that all we have to do is one line of code to configure the SurrealDB client with the previously set Connection String.
Note
Note
Consume the SurrealDB client
Open WeatherForecastController.cs and replace everything in there with the following code.
Finally, we can inject the ISurrealDbClient inside our Controller.
Then make sure your SurrealDB server is running on 127.0.0.1:8000 and run your app from the command line with: