Dapper changing connection string at runtime - asp.net-core

I have three identical database servers (DDL wise) that my application connects to.
I want let application users decide which database they want to connect to.
This is a ASP.NET Core 2.1 API project, which implements both service and repository patterns (database queries are called using Dapper, but this can be changed).
I could simply put server name as a parameter in my Domain project, but I don't want this to know anything about database servers we have, neither I want my Repository to know that there could are multiple servers.
So it's just application side that can decide which database server to connect to at run-time.
I was able to find samples and suggestions for EF Core, but not much for anything else, particularly Dapper.
So my question is. How would I let users change database server at run time while keeping domain and repository unaware that multiple servers are there?

So in this case I would suggest having your connection strings either stored in environment variables or appsettings.json files. Lets say the user wants to access "server1" , this would come in from your api and you could then get out the relevant connection string in your repository layer like so
var connectionString = Environment.GetEnvironmentVariable(serverToUse);
Where serverToUse is "server1" passed in from the api. I prefer storing my connection strings in environment variables as its stored directly on the host machine, instead of the application. You could also do this via appsettings
"Server1" : "Your connection string here" in your appsettings.json
You could also do this in your service layer and have the service layer pass the connection string to the repository . Pick your flavor. In this case the repositories are not concerned with what connection you are using as long as its present in either environment variable or appsettings.

Related

Interview task, stuck on local database connection, need alternative

I'm required to create a bit of software for a company, to illustrate my code use. I'm using .NET CORE Web App MVC and I believe it requires me to use a database but I would need to upload my code on GitHub for them to inspect and run but obviously wouldn't read the database from my machine. What are the alternatives? Can a fake DB be created within the project for instance? Or is there something else I could do that doesn't involve Azure?
I tried scaffolding a DBContext from a controller but it requires a connection of a database
Have you considered mocking your data connection? It is the same thing you would do if you were unit testing your application. You would not want to connect directly to your database; instead, you would create a mock connection and return the data yourself.
You have multiple choices here. You can use a Mock framework like Moq, FakeItEasy, JustMock, and NSubstitute. Otherwise, you can roll your own.

Azure SQL Primary replica read/write connection redirection

I have an Azure Asp.Net Core Web App running with Azure SQL. The SQL server has a replica target in a different region.
This particular server is a Hangfire "task" server. It should participate in task execution along with the rest of the servers in the (logical) Hangfire cluster during normal operation (it should point toward MyPrimaryLiveSql.database.com).
In the event of a service outage in the primary region, the sql replica at BCDRSql.database.com goes from Read Only to Read/Write. I would now like the Task Server to automatically start using bcdrsql.database.com as its connection string for all connections going forward.
Is there a way in either Azure SQL or Asp.Net or Azure Web App Configuration or Entity Framework Core to dynamically choose/redirect the SQL server connection string depending on if the Database is in read only or read/write mode?
I can get the database read/write status via SELECT DATABASEPROPERTYEX(DB_NAME(), 'Updateability');, but I'm not sure how to get my Data Context to use the BCDR connection string based on the result, as my DB Context is only initialized once during the startup sequence.
You can likely just us a failover group to solve this problem. Please read through this link: auto failover groups. Note that this works for the automatic DR path - there is also the other active geo-replication solution and you may need control over the failover yourself (Microsoft does it in the "auto" case). So please be aware of the level of control you need in each solution. (Of course, if you are choosing to failover you can likely get your app to do what you want then too since you are making a change yourself.

Use appsettings.json for inmemory database aspnet.core

Is there a way to specify an "InMemory" database "connection string" (for lack of better terms) in the applicationsettings.json file?
I want to be able to use InMemory for development and then transform the connection for deployed (test, QA, prod) environments without having to change the code.
I recognize I can use a conditional statement to check and then go from there like -
var builder = new DbContextOptionsBuilder();
if(env.IsDevelopment())
{
builder.UseInMemoryDatabase("DevelopmentContext");
}
I was just curious if there is more of a convention or api based way. I have not been able to find anything.
There is no way to do what you're asking. It can't be controlled by connection string alone; you must either use the in-memory provider or the SQL Server provider, for example, which means you must branch in code. Now, you could potentially make the provider a configuration value, and then use the config value to determine whether to use the in-memory provider vs your production provider, but that doesn't buy you anything that simply using IHostingEnvironment.IsDevelopment() doesn't.
That said, the in-memory provider should not actually be used even in development. That's not what it's for, and won't serve you as you imagine. The in-memory provider exists for testing, where volatility is not an issue as you'd want to tear down and rebuild the database after every test anyways. Using it in development wouldn't allow you reliably persist data from one request to the next, so you'd have problems with all sorts of things.
If you want a more simplistic data provider for development, you can use something like SQLite.

Connecting to specific Azure Redis database in asp .NET core

I want to connect to a specific database in our new Azure Redis cache but can't seem to figure out how to do it.
I've tried adding the database id to connection string in various forms as well as looking for GetDatabase(dbid) on the IDistributedcache object (which doesn't seem to exist).
FYI, I want to use the same cache for our testing and production without having to pay for an additional redis cache so I'm open to alternative approaches.
You could also use the ConnectionMultiplexer object directly and access the Database via the GetDatabase method.

Is it possible to store session in local db (DBF file) in asp .net MVC?

Could you help me please? I would like to store session in a local database(dbf) file.
I was searching on the internet, but I could not find anything, about how should this be done. You can use an sql server instance to store session with use of aspnet_regsql.exe, however I did not manage to use with a dbf file. Please help.
First of all, we don't normally use Local Db in Production. Instead, you want to consider using real SQL Server.
Second, you do not need to store Session State in database, unless you plan to host in Web Farm or Web Garden environment. To improve the performance, you can store View State inside Session State which then stores in SQL Server, but I don't think it is your intention.
If you plan to host in Web Farm or Web Garden environment, you might want to look at StackExchange.Redis used in Stack Overflow and Azure.