Cannot access a disposed context instance - c#

My Application: .Net Core 3.1 Web application using Microservice architecture; Identity for Authorization & Authentication as separate Microservice API.
I have extended the standard AspNetUsers and AspNetRoles table with custom fields.
Getting the following error when I am trying to create a new Role using Identity RoleManager.
Cannot access a disposed context instance. A common cause of this
error is disposing a context instance that was resolved from
dependency injection and then later trying to use the same context
instance elsewhere in your application. This may occur if you are
calling 'Dispose' on the context instance, or wrapping it in a using
statement. If you are using dependency injection, you should let the
dependency injection container take care of disposing context
instances. Object name: 'MembershipDBContext'.
Find my Code below
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
var idenConnectionString = Configuration["DbContextSettings:IdentityConnectionString"];
var userConnectionString = Configuration["DbContextSettings:UserConnectionString"];
var dbPassword = Configuration["DbContextSettings:DbPassword"];
var builder = new NpgsqlConnectionStringBuilder(idenConnectionString)
{
Password = dbPassword
};
var userBuilder = new NpgsqlConnectionStringBuilder(userConnectionString)
{
Password = dbPassword
};
services.AddDbContext<MembershipDBContext>(opts => opts.UseNpgsql(builder.ConnectionString));
services.AddDbContext<UserDBContext>(opts => opts.UseNpgsql(userBuilder.ConnectionString));
services.AddIdentity<MembershipUser, MembershipRole>(options =>
{
options.Password.RequiredLength = 8;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._#+ ";
options.SignIn.RequireConfirmedEmail = false;
}).AddRoles<MembershipRole>().AddEntityFrameworkStores<MembershipDBContext>()
.AddDefaultTokenProviders();
services.AddTransient<IIdentityMSService, IdentityMSService>();//IdentityMS
services.AddTransient<IAdministrationService, AdministrationService>();//IdentityMS
services.AddTransient<IIdentityMSRepository, IdentityMSRepository>();//IdentityMS
services.AddTransient<IAdministrationRepository, AdministrationRepository>();//IdentityMS
services.AddTransient<UserDBContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddMediatR(typeof(Startup));
RegisterServices(services);
}
MembershipDBContext.cs
public class MembershipDBContext : IdentityDbContext<MembershipUser,MembershipRole,string>
{
public MembershipDBContext(DbContextOptions<MembershipDBContext> options) : base(options)
{
}
}
AdministrationController
public class AdministrationController : Controller
{
private readonly IAdministrationMVCService _adminService;
public AdministrationController(IAdministrationMVCService adminService)
{
_adminService = adminService;
}
// GET: AdministrationController/Create
public ActionResult Create()
{
return View();
}
// POST: AdministrationController/Create
[HttpPost]
//[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateAsync(MembershipRole rm)
{
if (ModelState.IsValid)
{
try
{
rm.CompanyId = 1;
await _adminService.AddRoles(rm);
//return RedirectToAction(nameof(Index));
return View();
}
catch
{
return View();
}
}
return View();
}
}
AdministrationApiController
[HttpPost]
public void Post([FromBody] MembershipRole role)
{
_adminMSService.AddRoles(role);
}
AdministrationRepository
public class AdministrationRepository : IAdministrationRepository
{
private readonly RoleManager<MembershipRole> _roleManager;
private readonly UserManager<MembershipUser> _userManager;
public AdministrationRepository(RoleManager<MembershipRole> roleManager, UserManager<MembershipUser> userManager)
{
_roleManager = roleManager;
_userManager = userManager;
}
public async Task AddRolesAsync(MembershipRole rvm)
{
try
{
IdentityResult result = await _roleManager.CreateAsync(rvm);
if (result.Succeeded) {
}
}
catch (Exception ex)
{
throw;
}
}
}
I tried making the services and repositories as singleton but it didn't work either. What am I missing here? Any insights?

Solved the issue! As DavidG pointed out in his comment, I missed the 'await' keyword at certain places like AdministrationApiController.
Posting as an 'Answer' here, as I am unable to mark the comment as 'Answer'.

Related

Catching errors from services in Exception Filter

I've created a service that's used throughout my aspnet project that retrieves and validates a header among other things. Issue is that the Exception Filter is not able to catch the errors that are thrown by the service as it's not in the scope of the Exception Filter thus giving the user an ugly internal server error. Is there any way to gracefully return an argument error with description to the user with the use of the services?
The Startup:
services.AddScoped<UserService>();
services
.AddMvc(x =>
{
x.Filters.Add(typeof(Filters.MyExceptionFilter));
})
The Exception Filter:
public class MyExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
if (context.Exception is ArgumentException argumentException)
{
var response = context.HttpContext.Response;
context.ExceptionHandled = true;
response.StatusCode = 400;
context.Result = new ObjectResult(argumentException.Message);
return;
}
}
}
The Service:
public class UserService
{
public readonly string UserId;
public UserService(IHttpContextAccessor context)
{
if (!context.HttpContext.Request.Headers.TryGetValue("x-test", out var user))
{
throw new ArgumentException($"x-test header is required.");
}
UserId = user;
//do other stuff
}
}
Controller Action Method:
public async Task<IActionResult> Delete(string id,
[FromServices] UserService userService)
{
//do stuff
}
A rule-of-thumb in C# is do as little work in the constructor as possible. There's a few good reasons for this (e.g. you can't dispose a class that threw an exception in it's constructor). Another good reason is, as you have found out, construction might happen in a different place (e.g. a DI container) than the place you actually use the class.
The fix should be quite straightforward - just move the logic out of the constructor. You could use a Lazy<T> to do this for example:
public class UserService
{
public readonly Lazy<string> _userId ;
public UserService(IHttpContextAccessor context)
{
_userId = new Lazy<string>(() =>
{
if (!context.HttpContext.Request.Headers.TryGetValue("x-test", out var user))
{
throw new ArgumentException($"x-test header is required.");
}
return user;
});
//do other stuff
}
public string UserId => _userId.Value;
}
Or you could just get the value when you needed it:
public class UserService
{
public readonly IHttpContextAccessor _context;
public UserService(IHttpContextAccessor context)
{
_context = context;
//do other stuff
}
public string UserId
{
get
{
if (_context.HttpContext.Request.Headers.TryGetValue("x-test", out var user))
{
return user;
}
else
{
throw new ArgumentException($"x-test header is required.");
}
}
}
}

How to use ApplicationDbContext outside the controller in Asp .Net 5

I am using Asp .Net 5 to create a WebApi and I am trying to put all database operations in a separate class, the problem is I can't Use ApplicationDbContext by initiating a new object because it takes an argument in the constructor.
my context :
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
controller :
[Route("api/[controller]")]
[ApiController]
public class AttributesController : ControllerBase
{
[HttpPost]
[Route("GetAllAttributes")]
public async Task<AllAttributes> GetAllAttributes()
{
return await new Services.AttributeService().GetAll();
}
}
service :
public class AttributeService
{
private readonly ApplicationDbContext _db ;
public async Task<AllAttributes> GetAll()
{
try
{
var dbAttributes = await _db.Attributes.Where(attr=> (bool)attr.IsActive && !(bool)attr.IsDeleted && !(bool)attr.IsTrashed).ToListAsync();
if (dbAttributes.Count>0)
{
return new AllAttributes
{
Attributes = dbAttributes,
Message = new ResponseMessage
{
Message = "Success",
Code = 200
}
};
}
else
{
return new AllAttributes
{
Message = new ResponseMessage
{
Message = "Empty",
Code = 410
}
};
}
}
catch (Exception ex)
{
return new AllAttributes
{
Message = new ResponseMessage
{
Message = ex.Message,
Code = 500
}
};
}
}}
so when I call it like this I got NullReference Exception.
You will need to add AttributeService to the DI container. You can do this inside ConfigureServices method of Startup.cs:
services.AddScoped<AttributeService>();
Then you can inject the context in the constructor of AttributeService:
public class AttributeService
{
private readonly ApplicationDbContext _db ;
public AttributeService(ApplicationDbContext db)
{
_db = db;
}
...
}

How can acces ClaimsIdentity on Logic Layer

I want to move this service to logic for using on everywhere, but i can't successful because it was coming from the controller.
I have two services. There read caches and I use them in the controller layer when authenticating.
my first logic is reading companyId in cache
public virtual int GetCompanyIdFromCache(int id)
{
_memCache.TryGetValue(id, out int companyId);
return companyId;
}
My second service is also on the controller. (helps me find the user's id)
[HttpGet]
[Route("GetCompanyId")]
public int GetCompanyPublicId()
{
if (User.Identity is ClaimsIdentity claimsIdentity)
{
var userId = claimsIdentity.FindFirst(ClaimTypes.Name)?.Value;
var companyId = _userService.GetCompanyIdFromCache(Convert.ToInt32(userId));
return companyId;
}
throw new ArgumentException("Can't be found Company");
}
I want to use this method everywhere, so i want to move the second service completely to logic layer but User field comes from ControllerBase (on HttpContext i guess) and I can't move it to logic
if (User.Identity is ClaimsIdentity claimsIdentity)
What should I do to refactor the logic layer?
As far as I know, the User.Identity is ClaimsIdentity which is a property in the controllerbase. We couldn't directly use it in the other methods.
If you want to access the User.Identity in other service method, I suggest you could try to inject the httpaccessor service and get the ClaimsIdentity from it.
More details, you could refer to below codes:
Create myclass:
public class Myclass
{
public IHttpContextAccessor _accessor { get; set; }
public Myclass(IHttpContextAccessor accessor)
{
_accessor = accessor;
var re = accessor.HttpContext.User.Identity as ClaimsIdentity;
int i = 0;
}
public string GetName() {
var re = _accessor.HttpContext.User.Identity as ClaimsIdentity;
string name = re.Claims.First(x => x.Type == "name").Value;
return name;
}
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
services.AddHttpContextAccessor();
services.AddScoped(typeof(Myclass));
}
Usage:
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;
public Myclass test { get; set; }
public HomeController(ILogger<HomeController> logger, Myclass _test)
{
_logger = logger;
test = _test;
}
public async Task<IActionResult> IndexAsync()
{
var claimsIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
claimsIdentity.AddClaim(new Claim("name", "aaaa"));
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity)
);
return View();
}
public async Task<IActionResult> PrivacyAsync()
{
var re= test.GetName();
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
Result:

How to use ApplicationDbContext in a Class / Separate Thread ASP.Net Core

I am attempting to have a separate thread start a looping process to collect and import data from an api call and insert it into a local db. I attempt to create the database connection the same way I do in my controllers but it keeps giving me various errors.
I have tried multiple ways. For the setup below it gives me this error:
System.ObjectDisposedException: 'Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.'
The setup is for testing purposes.
public class HomeController : Controller
{
public ApplicationDbContext db;
public HomeController(ApplicationDbContext context)
{
db = context;
}
public IActionResult Index()
{
// This executes the code and inserts the test row no problem.
BinanceData binanceData = new BinanceData();
binanceData.Symbol = "Test";
binanceData.RealTime = DateTime.Now;
db.BinanceData.Add(binanceData);
db.SaveChanges();
// End
// Where I create the new thread and start the process
Thread doThis = new Thread(delegate ()
{
DataCollection dataCollection = new DataCollection(db);
dataCollection.InsertData(DateTime.Now);
});
doThis.Start();
// End
return View();
}
}
This is the class where I attempt to create a connection (or pass existing connection) and to begin the loop and collect/insert data.
public class DataCollection
{
public ApplicationDbContext db;
public DataCollection(ApplicationDbContext context)
{
db = context;
}
public void InsertData(DateTime nextTime)
{
List<string> tokens = new List<string> { "ETHBTC", "LTCBTC", "BNBBTC", "NEOBTC", "GASBTC", "BTCUSDT", "MCOBTC", "WTCBTC", "LRCBTC", "QTUMBTC", "YOYOBTC", "OMGBTC", "ZRXBTC", "STRATBTC", "SNGLSBTC", "BQXBTC", "KNCBTC", "FUNBTC", "SNMBTC", "IOTABTC", "LINKBTC", "XVGBTC", "SALTBTC", "MDABTC", "MTLBTC", "SUBBTC", "EOSBTC", "SNTBTC", "ETCBTC", "MTHBTC", "ENGBTC", "DNTBTC", "ZECBTC", "BNTBTC", "ASTBTC", "DASHBTC", "OAXBTC", "BTGBTC", "EVXBTC", "REQBTC", "VIBBTC", "TRXBTC", "POWRBTC", "ARKBTC", "XRPBTC", "MODBTC", "ENJBTC", "STORJBTC", "KMDBTC", "RCNBTC", "NULSBTC", "RDNBTC", "XMRBTC", "DLTBTC", "AMBBTC", "BATBTC", "BCPTBTC", "ARNBTC", "GVTBTC", "CDTBTC", "GXSBTC", "POEBTC", "QSPBTC", "BTSBTC", "XZCBTC", "LSKBTC", "TNTBTC", "FUELBTC", "MANABTC", "BCDBTC", "DGDBTC", "ADXBTC", "ADABTC", "PPTBTC", "CMTBTC", "XLMBTC", "CNDBTC", "LENDBTC", "WABIBTC", "TNBBTC", "WAVESBTC", "GTOBTC", "ICXBTC", "OSTBTC", "ELFBTC", "AIONBTC", "NEBLBTC", "BRDBTC", "EDOBTC", "WINGSBTC", "NAVBTC", "LUNBTC", "APPCBTC", "VIBEBTC", "RLCBTC", "INSBTC", "PIVXBTC", "IOSTBTC", "STEEMBTC", "NANOBTC", "VIABTC", "BLZBTC", "AEBTC", "NCASHBTC", "POABTC", "ZILBTC", "ONTBTC", "STORMBTC", "XEMBTC", "WANBTC", "WPRBTC", "QLCBTC", "SYSBTC", "GRSBTC", "CLOAKBTC", "GNTBTC", "LOOMBTC", "REPBTC", "TUSDBTC", "ZENBTC", "SKYBTC", "CVCBTC", "THETABTC", "IOTXBTC", "QKCBTC", "AGIBTC", "NXSBTC", "DATABTC", "SCBTC", "NPXSBTC", "KEYBTC", "NASBTC", "MFTBTC", "DENTBTC", "ARDRBTC", "HOTBTC", "VETBTC", "DOCKBTC", "POLYBTC", "PHXBTC", "HCBTC", "GOBTC", "PAXBTC", "RVNBTC", "DCRBTC", "USDCBTC", "MITHBTC", "BCHABCBTC" };
foreach (string token in tokens)
{
BinanceData binance = new BinanceData();
using (var client = new BinanceClient())
{
var data = client.GetKlines(token, Binance.Net.Objects.KlineInterval.OneMinute, null, null, 2);
var kline = data.Data[0];
binance.Symbol = token;
binance.Close = kline.Close;
binance.CloseTime = kline.CloseTime;
binance.High = kline.High;
binance.Low = kline.Low;
binance.Open = kline.Open;
binance.OpenTime = kline.OpenTime;
binance.QuoteAssetVolume = kline.QuoteAssetVolume;
binance.TakerBuyBaseAssetVolume = kline.TakerBuyBaseAssetVolume;
binance.TakerBuyQuoteAssetVolume = kline.TakerBuyQuoteAssetVolume;
binance.TradeCount = kline.TradeCount;
binance.Volume = kline.Volume;
binance.RealTime = DateTime.Now;
}
db.BinanceData.Add(binance);
db.SaveChanges();
}
CountUntilNextMin(nextTime);
}
public void CountUntilNextMin(DateTime nextTime)
{
while (DateTime.Now < nextTime)
{
System.Threading.Thread.Sleep(10000);
}
DateTime passTime = DateTime.Now.AddMinutes(1);
InsertData(passTime);
}
}
Here is my ApplicationDbContext Class
public class ApplicationDbContext : IdentityDbContext
{
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<BinanceData> BinanceData { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public ApplicationDbContext()
: base()
{
}
}
And my StartUp.cs ConfigureServices Method
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
By default your DI container is scoping the DbContext to the request. If you want to use one in a long-running background process, just create it directly. EG:
Thread doThis = new Thread(delegate ()
{
using (var db = new ApplicationDbContext())
{
DataCollection dataCollection = new DataCollection();
dataCollection.InsertData(DateTime.Now);
}
});
The DbContext will need to be configured, and the simplest way to do that is to just override OnConfiguring to connect to your database. Minimally, like this:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=YourServer;Database=YourDatabase;Trusted_Connection=True;MultipleActiveResultSets=true");
}
Although you'll probably want to read the connection string from your configuration.
i using this by calling CreateScope try it
public static async Task InitializeDatabaseAsync(IServiceProvider serviceProvider, IHostingEnvironment env)
{
var result = false;
using (var scope1 = serviceProvider.CreateScope())
using (var db1 = scope1.ServiceProvider.GetService<MainContext>())
{
result = await db1.Database.EnsureCreatedAsync();
if (result)
{
InsertTestData(serviceProvider, env);
}
}
}
then
using (var scope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
using (var db = scope.ServiceProvider.GetService<MainContext>())
{
existingData = db.Set<TEntity>().ToList();
}

Asp.net core session unit test

I hava a problem with unit testing asp.net core mvc controller!
the problem is that in my controller, i use sessions:
[HttpPost]
public IActionResult OpretLobby(LobbyViewModel lobby)
{
try
{
//find brugeren der har lavet lobby
var currentUser = HttpContext.Session.GetObjectFromJson<User>("user");
//save as a lobby
ILobby nyLobby = new Lobby(currentUser.Username)
{
Id = lobby.Id
};
SessionExtension.SetObjectAsJson(HttpContext.Session, lobby.Id, nyLobby);
//add to the list
_lobbyList.Add(nyLobby);
return RedirectToAction("Lobby","Lobby",lobby);
}
this all works perfectly well online on the server, nothing wrong here.
BUT when it comes to the demand of unit testing this whole thing, its not so perfect anymore.
basicly the problem is, that i cant get access to my session from a test.. i have tryed in many ways to create mocks and what not, but most of the solutions work for .net framework, and not for .net core for some reason! please help im in pain!
note:
i used a dummy version of a test to isolate this problem:
[Test]
public void TestIsWorking()
{
SessionExtension.SetObjectAsJson(uut.HttpContext.Session, "user", _savedUser);
//ViewResult result = uut.OpretLobby(lobbyViewModel) as ViewResult;
//Assert.AreEqual("OpretLobby", result.ViewName);
Assert.AreEqual(1,1);
}
goes wrong also here trying to set the session for a user :/
It seems that GetObjectFromJson is an extension method. If so, we could not mock static method easily.
I normally create an abstraction for that kind of scenario. Then register it in DI container, and inject the dependency to the controller.
Startup.cs
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<IUserSession, UserSession>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
...
}
...
}
Abstraction
public class User
{
public string Username { get; set; }
}
public interface IUserSession
{
User User { get; }
}
public class UserSession : IUserSession
{
private readonly IHttpContextAccessor _httpContextAccessor;
public UserSession(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public User User => _httpContextAccessor.HttpContext.Session.GetObjectFromJson<User>("user");
}
public static class SessionExtensions
{
public static User GetObjectFromJson<User>(
this ISession sesson, string json) where User : new()
{
return new User(); // Dummy extension method just to test OP's code
}
}
Controller
public class HomeController : Controller
{
private readonly IUserSession _userSession;
public HomeController(IUserSession userSession)
{
_userSession = userSession;
}
public IActionResult OpretLobby()
{
var currentUser = _userSession.User;
return View(currentUser);
}
}
}
Unit Tests
using AspNetCore.Controllers;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;
namespace XUnitTestProject1
{
public class HomeControllerTests
{
private readonly User _user;
public HomeControllerTests()
{
_user = new User {Username = "johndoe"};
}
[Fact]
public void OpretLobby_Test()
{
// Arrange
var mockUserSession = new Mock<IUserSession>();
mockUserSession.Setup(x => x.User).Returns(_user);
var sut = new HomeController(mockUserSession.Object);
// Act
var result = sut.OpretLobby();
// Assert
var viewResult = Assert.IsType<ViewResult>(result);
var user = Assert.IsType<User>(viewResult.Model);
Assert.Equal(_user.Username, user.Username);
}
}
}
#nicholas-ladefoged
I would rather recommend you use integration testing when you need to get some content from session
asp.net core has an awesome TestHost nuget package which can help you validate logic using integration way for testing.
Try add a below code snippet
TestServer server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>()
.ConfigureTestServices(services =>
{
services.AddHttpContextAccessor();
}));
var client = server.CreateClient();
var response = await client.GetAsync(""); // I'm using GetAsync just for sample
here you go! You will have a real session that you can test
BTW, Moq library hasn't ability to mock a static methods, so there a lot of issues. I think that will be more easier to use an integration test in your situation
Win solved it! Here is the final test code if anyone is wondering.
// Arrange
var mockUserSession = new Mock<IUserSession>();
mockUserSession.Setup(x => x.User).Returns(_savedUser);
var sut = new LobbyController(FakeSwagCommunication,mockUserSession.Object);
// Act
var result = sut.OpretLobby(_lobbyViewModel);
// Assert
Assert.IsInstanceOf<RedirectToActionResult>(result);

Categories