Click here to Skip to main content
15,891,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am working in asp.net mvc4 web application here we have different modules. View, Controller, Service and Store.
View - cshtml page having normal html5 elementsand jquery functions
Controller - MVC Controller to access view and Service(here we done our business logic)
Service - Business Logic done here. we are using this service for reducing controller work load. Also we have interfaces, this classes are inherited from interfaces.
Store - Inherited from interface. Having logic to access database

In netshell, MVC -
M - (Interface, Service and Store)
V - Normal View
C - Normal Controller
based on this you can understand my project. Now i want to log user activities and store those in database. I have saved logs for User page navigation, login and Normal Logoff, But i am not able to save session_end time in database. My web.config code here
<sessionState mode="InProc" timeout="1"></sessionState> and my global.asax.cs page session end code here
C#
public void Session_onEnd(object sender, EventArgs e)
{
    var userLoggingActivityLog = new UserLoggingActivityLog
    {
        UserName = Session["username"].ToString(),
        LogoffType = "Abnormal",
        UserId = Convert.ToInt32(Session["userid"])
    };
    var userActivityLogService =         DependencyResolver.Current.GetService<IUserActivityLogService>();
    userActivityLogService.CreateUserLoggingActivity(userLoggingActivityLog, false);
    Session.Clear();
    Session.Abandon();
}


When sesseion_end method gets fire, In this particular line "var userActivityLogService = DependencyResolver.Current.GetService<IUserActivityLogService>();"null exception underhanded by user code error get produced. Can anyone help me how to solve this error and successfully call my interface to save session end time in database.
Posted
Updated 15-Oct-15 22:37pm
v2
Comments
F-ES Sitecore 16-Oct-15 4:38am    
The Session end event doesn't have access to an http context so it's likely something in your code needs an http context. Either the dependency injection framework or maybe something to do with the way the service has been registered (some DI frameworks allow you to register per-request services which won't work on session end as there is no request), or maybe something in the constructor of the concrete service class. Given the information you've provided it's hard to be more specific. You could break your line into smaller chunks to see if it is the DespendencyResovler.Current that is null, or if the issue is with the GetService call.
sankarisiva 16-Oct-15 4:59am    
Hi,
As per you advice i checked DespendencyResovler.Current is null or not.
var testDependencResolver = DependencyResolver.Current; Here the testDependencResolver gets error "Object reference not set to an instance of an object".
F-ES Sitecore 16-Oct-15 5:17am    
It might be an issue with how you register the dependency resolver, maybe the code that registers it doesn't run for session end. It might not be the best solution, but you might have to bypass the DI and just create the concrete instance of UserActivityLogService in your session_end event.
sankarisiva 16-Oct-15 5:56am    
Hi,
I added the below code in my Session_End function but the same null exception error is produced for dependency resover.
var container = new UnityContainer();
container.RegisterType<iuseractivitylogservice, useractivitylogservice="">();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900