I'm fairly new to the whole concept of the microservice architecture, but I have managed to create a simple proof of concept application, which consists of 1 Blazor server-side front-end, 6 microservices, which are all .net 6 WEB APIs and are currently using an InMemory database and they all seed mock data inside of their DBs on startup. I'm using Project Tye to run all projects simultaneously and to easily deploy them to a local kubernetes cluster.
Now I would like to migrate from aforementioned InMemory databases for each microservice to a sqlserver database.
Tye allows to run an image as a dependency in the tye.yaml file and setting environment variables from the configuration, which is then passed through Tye's VS extension for configuration as a ConnectionString.
Here is the snippet from it regarding one sqlserver:
- name: sqlserver-videoteka
image: mcr.microsoft.com/mssql/server:2019-latest
env:
- name: SA_PASSWORD
value: "M!cr0s3rv!ce"
- name: ACCEPT_EULA
value: 'Y'
volumes:
- name: videoteka-storage
target: /var/opt/mssql
bindings:
- port: 9634
connectionString: Server=${host}:${port};Database=VideotekaDB;MultipleActiveResultSets=true;User Id=sa;Password=${env:SA_PASSWORD};
And then in the microservice's Program.cs file I add these 2 lines:
connectionString = configuration.GetConnectionString("sqlserver-videoteka");
builder.Services.AddDbContext<VideotekaDbContext>(options => options.UseSqlServer(connectionString));
So then I attempt to run tye run and I check the logs from the mentioned microservice and this is what I'm met with:
[videoteka-api_ae1ba91d-5]:E:\OceniFilm\Videoteka.API\bin\Debug\net6.0\Videoteka.API.exe
[videoteka-api_ae1ba91d-5]: CONNECTION STRING: Server=localhost:9634;Database=VideotekaDB;MultipleActiveResultSets=true;User Id=sa;Password=M!cr0s3rv!ce;
[videoteka-api_ae1ba91d-5]: info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
[videoteka-api_ae1ba91d-5]: Entity Framework Core 6.0.3 initialized 'VideotekaDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.3' with options: None
[videoteka-api_ae1ba91d-5]: [PrepareDb] Migration unsuccessful: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
[videoteka-api_ae1ba91d-5]: fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
[videoteka-api_ae1ba91d-5]: An error occurred using the connection to database 'VideotekaDB' on server 'localhost:9634'.
[videoteka-api_ae1ba91d-5]: fail: Microsoft.EntityFrameworkCore.Query[10100]
[videoteka-api_ae1ba91d-5]: An exception occurred while iterating over the results of a query for context type 'Videoteka.API.Data.VideotekaDbContext'.
[videoteka-api_ae1ba91d-5]: Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
[videoteka-api_ae1ba91d-5]: ---> System.ComponentModel.Win32Exception (53): The network path was not found.
[videoteka-api_ae1ba91d-5]: at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
[videoteka-api_ae1ba91d-5]: at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
As I understood Tye's configuration, it would create the image of the sql server and use my variables for the credentials, no?
Would a separate sqlserver container need to be running on my machine? But then why would it pull the image and create a container?
Any help is much appreciated. Thank you.
EDIT: CER solved it for me regarding the tye run, but when running tye deploy, the sqlserver gets skipped?
this happens right at the end of deployment.
CodePudding user response:
I think there might be 2 issues:
- Your
bindings
section should includecontainerPort:1433
to map to9643
. See tye schema. - Re:
connectionstring
. SQL Server delimiter between host and port is not a colon (:
), it's a comma (,
). e.g.localhost,9643
.