Welcome to Ocelot.Provider.SqlServer, the SQL Server provider for Ocelot - your gateway to dynamic routing in ASP.NET applications. With Ocelot.Provider.SqlServer, you can effortlessly store your Ocelot routes in a SQL Server database instead of a JSON file, enabling dynamic and scalable routing configurations.
Follow these steps to integrate Ocelot.Provider.SqlServer into your .NET 6.0 project:
Begin by installing the Ocelot.Provider.SqlServer
NuGet package into your .NET 6.0 project using your preferred package manager:
> Install-Package Ocelot.Provider.SqlServer
In your Program.cs
file, configure Ocelot.Provider.SqlServer by adding it to the services collection:
builder.Services.AddOcelot()
.AddSqlServerProvider(options =>
{
options.DbConnectionStrings = builder.Configuration.GetConnectionString("SqlServerDb");
options.MigrationsAssembly = Assembly.GetExecutingAssembly().FullName;
});
In your Startup.cs
, use the Ocelot.Provider.SqlServer middleware to enable dynamic routing:
app.UseOcelotWithSqlServerProvider().Wait();
In your appsettings.json
, define your SQL Server connection string as follows:
"ConnectionStrings": {
"SqlServerDb": "Server=(localdb)\\mssqllocaldb;Database=OcelotDb;Trusted_Connection=True;MultipleActiveResultSets=true"
}
To set up the required database tables, follow these steps:
Set your API Gateway project as the startup project in Visual Studio.
Run the following Entity Framework Core commands in the Package Manager Console:
> Add-Migration InitDb
> Update-Database
After completing the migration, your database will contain two tables:
OcelotRoutes
: Add your routes to this table.OcelotGlobalConfigurations
Explore sample projects using Ocelot.Provider.SqlServer for both the API Gateway and APIs:
We appreciate your feedback, suggestions, and contributions. Please feel free to open issues or reach out with your thoughts. We respond to every issue or message and are eager to improve this package. Thank you for using Ocelot.Provider.SqlServer!