Hi Divyang,
I appreciate your help so far. Unfortunatly it didn't help and the context is still disposed. Maybe it helps if I show you what I've got so far.
So first of all I implemented an ActionFilter and the service like this:
builder.RegisterType<LicenceCheck>().As<IFilterProvider>();
builder.RegisterType<TestService>().As<ITestService>()
.InstancePerLifetimeScope();
The ActionFilter itself looks like this:
public partial class LicenceCheck : ActionFilterAttribute, IFilterProvider
{
private ITestService _testService;
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
return new List<Filter>() { new Filter(this, FilterScope.Action, 0) };
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext == null || filterContext.HttpContext == null)
return;
HttpRequestBase request = filterContext.HttpContext.Request;
if (request == null)
return;
string actionName = filterContext.ActionDescriptor.ActionName;
if (String.IsNullOrEmpty(actionName))
return;
string controllerName = filterContext.Controller.ToString();
if (String.IsNullOrEmpty(controllerName))
return;
if (filterContext.IsChildAction)
return;
if (_testService == null)
_testService = EngineContext.Current.ContainerManager.Resolve<ITestService>();
if (_testService.GetLicenceLevel() == 3)
{
var noLogonUrl = new UrlHelper(filterContext.RequestContext).RouteUrl("TestLogin");
filterContext.Result = new RedirectResult(noLogonUrl);
}
return;
}
}
Inside the TestService I've got my SettingService, which is automatically created via constructor. The method then just gets an encrypted setting from the database, decrypts it and returns the value.
private readonly ISettingService _settingService;
public int GetLicenceLevel()
{
string licencelevel = string.Empty;
var cipherText = _settingService.GetSetting("licence.file");
This works for every first run, but after that the GetSetting method encounters the exception of DBContext has been disposed.
Hope this will get you better insight!
Greetings Andreas!