Asp.net plug-in implementation

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 implementation methods based on ASP.net plug-ins. I have tried to dynamically load user controls, and I feel a little heavy and have been thinking for a long time, by accident, I thought of my first note. I had to look for it, but it was in the dark. 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 extension points, the plug-in function can be basically implemented! The code implementation logic is as follows:

1 namespace myplug
2 {
3 Public class articlereadingarg: eventargs
4 {
5 Public Article theArticle {Get; set ;}
6}
7 public class articlepage: system. Web. UI. Page
8 {
9 public static event eventhandler <articlereadingarg> onreading;
10 protected override void oninit (eventargs E)
11 {
12 if (onreading! = NULL)
13 {
14 onreading (eventargs. Empty, new articlereadingarg (getarticle (webrequest. getvalue ("ID "))));
15}
16 base. oninit (E );
17}
18}

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 public class counterplug
2 {
3 static counterplug ()
4 {
5 If (this. Enable) articlepage. onreading + = addcount;
6}
7 public static void addcount (Object sender, articlereadingarg E)
8 {
9 E. TheArticle. Hit + = 1;
10}
11}

If the Asp.net model you are using is a website model, it is very convenient to use and manage it in the future by using the dynamic compilation. If not, you can compile it into an independent DLL and 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.

Activator. createinstance <myplug. counterplug> ();
Because an instance is created, static functions are executed and events are subscribed! So the 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

String configfile = httpcontext. Current. server. mappath ("~ /Web. config ");

System. Io. file. setlastaccesstimeutc (configfile, datetime. utcnow );

The restart function can be achieved, which is also poor. Every time the plug-in is uninstalled, it will cause re-compilation. It is really not good, but the system is not always loading and uninstalling the plug-in, the main purpose is to expand without modifying the source code. If you have a better plug-in implementation method, share it with us and look forward to it.

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.