Implementation of ASP. NET plug-in

Source: Internet
Author: User

I still remember that my first blog in the blog Park was the study notes for study delegation and events. In a twinkling of an eye, I have been in the blog Park for more than a year, and I have learned a lot, I would like to thank all of you who wrote blogs in the blog Park for providing us with valuable learning materials and passing on the joy of sharing knowledge and technology.

I have been looking for ASP-based solutions. NET plug-in implementation method, has tried to dynamically load the user control implementation, feel a little heavy, bitter for a long time, accidentally thought of the first note, the original hard to seek, but in the dark, haha. In fact, plug-ins mainly provide extensible points. In fact, the actual requirement of plug-ins also seems to be implemented before or after expansion operations, a bit like AOP, if the program can provide these extended points, ASP. NET plug-in function can be basically achieved! The code implementation logic is as follows:

 
 
  1. namespaceMyPlug  
  2. {  
  3. publicclassArticleReadingArg:EventArgs  
  4. {  
  5. publicArticleTheArticle{get;set;}  
  6. }  
  7. publicclassArticlePage:System.Web.UI.Page  
  8. {  
  9. publicstaticeventEventHandler<ArticleReadingArg>OnReading;  
  10. protectedoverridevoidOnInit(EventArgse)  
  11. {  
  12. if(OnReading!=null)  
  13. {  
  14. OnReading(EventArgs.Empty,newArticleReadingArg(GetArticle(WebRequest.GetValue("ID"))));  
  15. }  
  16. base.OnInit(e);  
  17. }  

The first step is to provide an extension point. If we want to implement the news access statistics plug-in of the news management system, we need to provide an extensible code as follows: This is the base class of a page, he revealed a static event OnReading that provided the developer to subscribe to the events before browsing the Page. Of course, there are also many events in the Page class that can be subscribed, however, it is not publicly subscribed or called to other classes. Now, the basic conditions for this step are mature, because the external class can subscribe to the Onreading event, and the time parameter can be used to access the corresponding news entity, the control is transferred to the subscriber.

 
 
  1. publicclassCounterPlug  
  2. {  
  3. staticCounterPlug()  
  4. {  
  5. if(this.Enable)ArticlePage.OnReading+=AddCount;  
  6. }  
  7. publicstaticvoidAddCount(objectsender,ArticleReadingArge)  
  8. {  
  9. e.TheArticle.Hit+=1;  
  10. }  

If you use ASP. the NET model is a WebSite model, so it is very convenient to use its own dynamic compilation, you can easily implement subsequent use and management, if not, you can compile it into an independent dll, use reflection to create an instance. net static initialization function features-only executed when a static member of the type is accessed for the first time or when the type is instantiated for the first time. For details, see msdn. Here, we use the instance creation method to activate this static function to subscribe to events. We noticed that there are conditions for loading the plug-in. This condition can be obtained based on the configuration file or the database data. The following is the simple code for activation, which is executed after you click Enable.

 
 
  1. Activator.CreateInstance<MyPlug.CounterPlug>(); 

Because an instance is created, static functions are executed and events are subscribed! So the ASP. NET plug-in function will be executed!


The lifecycle of a static variable ends with the end of the application, so it should not disappear immediately. Okay, now we have enabled the plug-in and uninstalled the plug-in. I thought of the concept of the application domain. I think it is still inappropriate. This requires a certain level of trust, finally, I thought that every time I modify webConfig, the application will be restarted. Hey, this is a really annoying feature. It is still necessary here. When I close the plug-in, I need to modify the configuration record and ask Disenable, modify the last modification time of WebConfig

 
 
  1. stringconfigFile=HttpContext.Current.Server.MapPath("~/Web.config");  
  2. System.IO.File.SetLastAccessTimeUtc(configFile,DateTime.UtcNow); 
  1. Introduction to ASP. net mvc Framework
  2. Introduction to MvcAjaxPanel in ASP. NET MVC
  3. ASP. net mvc Framework to save UpdatePanel
  4. Use ASP. net mvc source code to find a solution
  5. ActionInvoker of ASP. net mvc Framework

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.