Skip to content

Commit

Permalink
Fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
MahdiyarGHD committed Oct 7, 2023
1 parent a980373 commit b84db98
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace EasyMicroservices.AuthenticationsMicroservice.Migrations
{
/// <inheritdoc />
public partial class SyncDatabaseForProduction : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
UserName = table.Column<string>(type: "nvarchar(450)", nullable: true),
Password = table.Column<string>(type: "nvarchar(max)", nullable: true),
UniqueIdentity = table.Column<string>(type: "nvarchar(450)", nullable: true, collation: "SQL_Latin1_General_CP1_CS_AS"),
CreationDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
ModificationDateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
DeletedDateTime = table.Column<DateTime>(type: "datetime2", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});

migrationBuilder.CreateIndex(
name: "IX_Users_CreationDateTime",
table: "Users",
column: "CreationDateTime");

migrationBuilder.CreateIndex(
name: "IX_Users_DeletedDateTime",
table: "Users",
column: "DeletedDateTime");

migrationBuilder.CreateIndex(
name: "IX_Users_IsDeleted",
table: "Users",
column: "IsDeleted");

migrationBuilder.CreateIndex(
name: "IX_Users_ModificationDateTime",
table: "Users",
column: "ModificationDateTime");

migrationBuilder.CreateIndex(
name: "IX_Users_UniqueIdentity",
table: "Users",
column: "UniqueIdentity");

migrationBuilder.CreateIndex(
name: "IX_Users_UserName",
table: "Users",
column: "UserName",
unique: true,
filter: "[UserName] IS NOT NULL");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// <auto-generated />
using System;
using EasyMicroservices.AuthenticationsMicroservice.Database.Contexts;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

#nullable disable

namespace EasyMicroservices.AuthenticationsMicroservice.Migrations
{
[DbContext(typeof(AuthenticationsContext))]
partial class AuthenticationsContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

modelBuilder.Entity("EasyMicroservices.AuthenticationsMicroservice.Database.Entities.UserEntity", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<long>("Id"));
b.Property<DateTime>("CreationDateTime")
.HasColumnType("datetime2");
b.Property<DateTime?>("DeletedDateTime")
.HasColumnType("datetime2");
b.Property<bool>("IsDeleted")
.HasColumnType("bit");
b.Property<DateTime?>("ModificationDateTime")
.HasColumnType("datetime2");
b.Property<string>("Password")
.HasColumnType("nvarchar(max)");
b.Property<string>("UniqueIdentity")
.HasColumnType("nvarchar(450)")
.UseCollation("SQL_Latin1_General_CP1_CS_AS");
b.Property<string>("UserName")
.HasColumnType("nvarchar(450)");
b.HasKey("Id");
b.HasIndex("CreationDateTime");
b.HasIndex("DeletedDateTime");
b.HasIndex("IsDeleted");
b.HasIndex("ModificationDateTime");
b.HasIndex("UniqueIdentity");
b.HasIndex("UserName")
.IsUnique()
.HasFilter("[UserName] IS NOT NULL");
b.ToTable("Users");
});
#pragma warning restore 612, 618
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ namespace EasyMicroservices.AuthenticationsMicroservice
{
public class DatabaseBuilder : IDatabaseBuilder
{
readonly IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
IConfiguration _configuration;
public DatabaseBuilder(IConfiguration configuration)
{
_configuration = configuration;
}

public void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseInMemoryDatabase("AuthDb");
//optionsBuilder.UseSqlServer(config.GetConnectionString("local"));
optionsBuilder.UseSqlServer(_configuration.GetConnectionString("local"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using EasyMicroservices.AuthenticationsMicroservice.Database.Contexts;
using System;
using System.Linq;
using Microsoft.Extensions.Configuration;

namespace EasyMicroservices.AuthenticationsMicroservice
{
Expand All @@ -32,7 +33,9 @@ public virtual IContractLogic<TEntity, TCreateRequestContract, TUpdateRequestCon

public virtual IDatabase GetDatabase()
{
return new EntityFrameworkCoreDatabaseProvider(new AuthenticationsContext(new DatabaseBuilder()));
var _config = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();

return new EntityFrameworkCoreDatabaseProvider(new AuthenticationsContext(new DatabaseBuilder(_config)));
}

public static string DefaultUniqueIdentity { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static async Task Main(string[] args)

builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<IDependencyManager>(service => new DependencyManager());
builder.Services.AddScoped<IDatabaseBuilder>(serviceProvider => new DatabaseBuilder());
builder.Services.AddScoped<IDatabaseBuilder>(serviceProvider => new DatabaseBuilder(_config));
builder.Services.AddScoped(service => new WhiteLabelManager(service, service.GetService<IDependencyManager>()));
builder.Services.AddScoped<IJWTManager, JWTManager>();
builder.Services.AddScoped((serviceProvider) => new DependencyManager().GetContractLogic<UserEntity, AddUserRequestContract, UserContract, UserContract>());
Expand Down Expand Up @@ -145,7 +145,9 @@ public static async Task Main(string[] args)

static void CreateDatabase()
{
using (var context = new AuthenticationsContext(new DatabaseBuilder()))
var _config = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).Build();

using (var context = new AuthenticationsContext(new DatabaseBuilder(_config)))
{
if (context.Database.EnsureCreated())
{
Expand Down

0 comments on commit b84db98

Please sign in to comment.