Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added references to ngpsql, appsettings values. #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions XPlatformChat/XPlatformChat.WebApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using Npgsql;
using System.Text;
using XPlatformChat.Lib.Models;
using XPlatformChat.WebApi.Data;
Expand All @@ -27,10 +28,8 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
SetEFDataProvider(services);

string connStr = Configuration.GetConnectionString("IdentityStores");
services.AddDbContext<ChatDbContext>(options => options.UseSqlServer(connStr));

services.AddIdentity<ChatUser, IdentityRole>()
.AddEntityFrameworkStores<ChatDbContext>()
.AddDefaultTokenProviders();
Expand Down Expand Up @@ -61,6 +60,25 @@ private void ConfigureJwtBearer(JwtBearerOptions options)
};
}

private void SetEFDataProvider(IServiceCollection services)
{
string environment = Configuration.GetValue<string>("Environment");

if (environment == "Production")
{
NpgsqlConnectionStringBuilder builder = new(Configuration.GetConnectionString("PostgresIdentityStores"));
builder.Password = Configuration["Password"];
string connStr = builder.ConnectionString;

services.AddDbContext<ChatDbContext>(options => options.UseNpgsql(connStr));
}
else
{
string connStr = Configuration.GetConnectionString("IdentityStores");
services.AddDbContext<ChatDbContext>(options => options.UseSqlServer(connStr));
}
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<UserSecretsId>c0eb9080-8be4-4f93-914c-c271ede6d2ae</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,6 +13,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.0" />
</ItemGroup>

Expand Down
4 changes: 3 additions & 1 deletion XPlatformChat/XPlatformChat.WebApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"Environment": "Production", //production or local
"Logging": {
"LogLevel": {
"Default": "Information",
Expand All @@ -13,6 +14,7 @@
},
"AllowedHosts": "*",
"ConnectionStrings": {
"IdentityStores": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=XPlatformChatDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
"IdentityStores": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=XPlatformChatDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
"PostgresIdentityStores": "Host=127.0.0.1;Port=5432;Database=xplatformchatdb;Username=c2gserviceaccount"
}
}