Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello, i'm writing a simple application that uses a plug-in architecture,
means, to simplify, i have a loop, that does the following:
C#
IPlugin plug = //Load with reflection

while (true)
{
    int code = plug.DoPluginCode();
    if (code < 0)
    {
         plug.NotifyError(code);
    }
}


My main application talks with the plugin.
i've checked the plug-in extensions:
http://msdn.microsoft.com/en-us/magazine/cc163476.aspx[^]
and made a test code, and in my plugin, i created a thread, and threw exception:

C#
public int DoPluginCode()
        {
            Thread t = new Thread(new ThreadStart(() => { throw new Exception("bAD"); }));
            t.Start();
            return 1;

        }


but my host application crashed, and i couldn't find any way (although its in another appdomain when using the System.Addin) to know about it and to maybe kill it.
also, memory leaks can affect my application, which may be a problem.

i thought about creating a new proccess, which is stand-alone, running my plug-in, but communication now becomes very hard.

is there any standard way of doing this plug-in architecture? to protect myself from bad 3rd party code?

thanks :)
Posted

1 solution

What are you trying to do here exactly?

Look at your code!!!

C#
IPlugin plug = //Load with reflection

while (true)
{
    int code = plug.DoPluginCode();
    if (code < 0)
    {
         plug.NotifyError(code);  
    }
}


there is a while loop which will run continuously. When the loop executes, each time it will spawn an new thread and it throws exception, the system will be filled with such threads! May be this is the reason why your application crashes.
Try doing this

C#
IPlugin plug = //Load with reflection

    int code = plug.DoPluginCode();
    if (code < 0)
    {
         plug.NotifyError(code);         
    }



Try not to throw exception and try.

Or tell me exactly what you want your plugin to do.
 
Share this answer
 
v3
Comments
arielbeck 5-Jul-12 10:16am    
man, this is just an abstract definition, this is not the code i'm writing.
the main issue, that i have a service, that sometimes calls a plugin function, if this function crashes, no problem, i can "try...catch" around it and handle it myself.

but if the plugin (which is 3rd party, someone else can write it) spawns a new thread that may crash?

if the plugin leaks? then it will affect my entire application (and customers that wrote a bad plugin may raise cases to us, that the service leaks, even though its their plug-in).

if all plugin code would run in an isolated enviornment (which we can kill and start), we won't have those problems, because if it leaks, we can prove it by shutting down the plugin itself, and if it crashes, we can write to log and load the plugin again.

by the way, this can done by spawning a new process and IPC through it, but it seems like an overkill

and last thing, suppose this was my code, the system wouldnt be filled up with threads, because an unhandled thread exception crashes the application.
[no name] 6-Jul-12 2:27am    
Is it a concern or is it some problem that you are facing in your application?.
If its a third party plugin then it must be tested before releasing. You dont have to worry much. If still you have such issues then you cant use it in your application.

Here what you can do is to try out some aspect oriented programming

try this link

http://www.codeproject.com/Articles/16359/MethodLogger-Hook-into-method-calls-in-NET-binarie

here you can find how to inject code into your 3rd party dll.
arielbeck 15-Aug-12 14:58pm    
the thing is, we don't want to rely on the 3rd party , if its not tested well - it shouldn't affect our application. (we are the service itself, and we take plugins)

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