Solve the Problem of online and offline status of MVC users

Source: Internet
Author: User

In the past, there was a Weibo function in the work project, which also caused online and offline user problems.

However, the first program uses the Session _ end event. The Session _ end event does not need to be explained. It is the event triggered when the seesion expires, but there is a BUG! Because there is a process recycle mechanism in iis, the Session will be lost if the system is busy. Of course, Microsoft solved the problem with an external seesion. At that time, I was wondering why, but I still had little experience.

 

Today, I suddenly saw some online status problems. I was wondering if I could not use Seesion _ end to implement the online status. Baidu hasn't gotten any gains for a long time. I have to write it on my own!

 

The first thought is to take advantage of the static object features (the object is created before the program is executed and will not be released before the program ends .) A few days ago, I showed that the process and thread execution were adjusted and executed by a scheduler maintained by the operating system. Therefore, you may want to maintain a static collection object in the pipeline event Application_Start () and use the timer to implement the session_end event.

 

Add an event to the Globel. asax. cs File

   
StatusMy object class is a key issue. The code should not be very understandable.

Single Instance IStatusMy ?? (IStatusMy = List <MyUser> thisLock = System. threading. timer (ToMyContent, ToMyContent (listMy = | listMy. count <= MyDbContext. getCurrentEFContext (); I = (MyUser myContent = TimeSpan (,); = DateTime. now. subtract (myContent. UTime); (ts <; User u = User {ID = myContent. ID, Status =, UTime = myContent. UTime, Name = <User> entry = db. entry <User >=>. status ). isModified = ++ (I = (u = && U. ID <= MyUser {ID = u. ID, Name = u. name, UTime = DateTime. now, Status = (listMy = List <MyUser> = listMy. firstOrDefault (c => c. ID = (m! = & M. ID! =

 

The only ef context of the MyDbContext thread, you know.

    = CallContext.GetData()  (dbContext=== = 

 

The global filter of MVC can be operated no matter the user accesses the Server Page. Aspect-Oriented Programming?

 

Add a filter class to the FilterConfig class

    

Implementation of LoginValidateAttribute custom filter

(FilterContext. RouteData. Values []. ToString ()! = 1. Verify that the user logs on (Session & Cookie )(! = FilterContext. Result = RedirectResult (

 

ToHelp class implementation

Ihelp ?? (Ihelp = 0.1 Http context and related attributes 2.1 Current user object + MODEL. ou_UserInfo Usr Session [] = (Session [] = (Request. cookies [] = strUser = Request. cookies [userid = db. set <OnlineStatus. models. user> (). firstOrDefault (c => c. ID = (use = & use. ID <= Login (= db. set <OnlineStatus. models. user> (). firstOrDefault (c => c. name = (use = & use. ID> =
 
I tried it. OK.

I 've been studying it by myself. It's just a hope to communicate with the gods. Please give me some advice.

Don't like, don't spray.
I hope someone can give me some advice.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.