C# Plugin using MarshalByRefObject creating multiple copies - c#

I am putting together a plugin framework with these requirements:
load/unload plugins at will
call methods in loaded plugins
raise callback events from plugin to the owner
To do this I am creating a new AppDomain, and loading the Plugin assemblies into this.
The implementation I have so far is working to a degree, but I believe I am creating an instance of the plugin in the local appDomain and also the new AppDomain.
When I load first, I get duplicate callback messages. When I load/unload multiple times I get multiple callback messages being added to the list. This indicates to me that I am loading up the plugin assembly not only remotely but also locally, and thus my "unload" mechanism is not operating as I would like. I would be grateful if anyone can tell me where I am going wrong.
I also understand I need to take the "life time" of the plugin into account but not sure where to implement this.
Thanks.
(1) I have a plugin interface
public interface IPlugin
{
string Name();
string Version();
string RunProcess();
// custom event handler to be implemented, event arguments defined in child class
event EventHandler<PluginEventArgs> CallbackEvent;
//event EventHandler<EventArgs> CallbackEvent;
void OnProcessStart(PluginEventArgs data);
void OnProcessEnd(PluginEventArgs data);
}
(2) custom event args
[Serializable]
public class PluginEventArgs : EventArgs
{
public string ResultMessage;
public string executingDomain;
public PluginEventArgs(string resultMessage = "")
{
// default empty values allows us to send back default event response
this.ResultMessage = resultMessage;
this.executingDomain = AppDomain.CurrentDomain.FriendlyName;
}
}
(3) example plugin class implementation
[Serializable]
public class Plugin_1 : IPlugin
{
System.Timers.Timer counter;
int TimerInterval;
string PluginName = "My plugin";
public string Name()
{
return "CMD";
}
public bool Start()
{
OnStart(new PluginEventArgs());
RunProcess();
return true;
}
// OnTimer event, process start raised, sleep to simulate doing some work, then process end raised
public void OnCounterElapsed(Object sender, EventArgs e)
{
OnProcessStart(new PluginEventArgs());
OnProcessEnd(new PluginEventArgs());
Stop();
}
public bool Stop()
{
// simulate waiting for process to finish whatever its doing....
if (counter != null)
{
counter.Stop();
OnStop(new PluginEventArgs());
}
return true;
}
public string RunProcess()
{
TimerInterval = 2000;
if (counter == null){
counter = new System.Timers.Timer(TimerInterval);
}
else {
counter.Stop();
counter.Interval = TimerInterval;
}
counter.Elapsed += OnCounterElapsed;
counter.Start();
return "";
}
public event EventHandler<PluginEventArgs> CallbackEvent;
void OnCallback(PluginEventArgs e)
{
if (CallbackEvent != null)
{
CallbackEvent(this, e);
}
}
public void OnProcessStart(PluginEventArgs Data)
{
OnCallback(new PluginEventArgs(Data.executingDomain + " - " + PluginName + " started"));
}
public void OnProcessEnd(PluginEventArgs Data)
{
OnCallback(new PluginEventArgs(Data.executingDomain + " - " + PluginName + " ended"));
}
(4) I have a plugin manager that loads/unloads
public bool LoadPlugin()
{
try
{
Domain_Command = AppDomain.CreateDomain("Second_domain");
command_loader = (ProxyLoader)Domain_Command.CreateInstanceAndUnwrap("PluginMgr", "PluginMgr.Method");
Plugins.AddPlugin(command_loader.LoadAndExecute("APluginName", Plugins.ProxyLoader_RaiseCallbackEvent), SomePluginType, false);
return true;
}
catch (Exception ex)
{
string message = ex.Message;
return false;
}
}
(5) my "ProxyLoader" to load the plugin into separate AppDomain
public class ProxyLoader : MarshalByRefObject
{
public AssemblyInstanceInfo LoadAndExecute(string assemblyName, EventHandler<PluginContract.PluginEventArgs> proxyLoader_RaiseCallbackEvent)
{
AssemblyInstanceInfo AInfo = new AssemblyInstanceInfo();
//nb: this AppDomain.CurrentDomain is in its own context / different from the caller app domain?
Assembly pluginAssembly = AppDomain.CurrentDomain.Load(assemblyName);
foreach (Type type in pluginAssembly.GetTypes())
{
if (type.GetInterface("IPlugin") != null)
{
object instance = Activator.CreateInstance(type, null, null);
AInfo.ObjectInstance = instance;
string s = ((PluginContract.IPlugin)instance).RunProcess(); // main procedure
AInfo.ASM = pluginAssembly;
((PluginContract.IPlugin)instance).CallbackEvent += proxyLoader_RaiseCallbackEvent;
((PluginContract.IPlugin)instance).Start();
instance = null;
}
}
return AInfo;
}
}
(6) I have a callback this plugs into
public event EventHandler<PluginContract.PluginEventArgs> Callback;
void OnCallback(PluginContract.PluginEventArgs e)
{
if (Callback != null)
{
Callback(this, e);
}
}
(7) called by (referenced in ProxyLoader when load the assembly)
public void ProxyLoader_RaiseCallbackEvent(object source, PluginContract.PluginEventArgs e)
{
OnCallback(new PluginContract.PluginEventArgs(str));
}

Related

How can I communicate between plugins?

I have a plugin system where I use MarshalByRefObject to create isolated domains per plugin, so users can reload their new versions, as they see fit without having to turn off the main application.
Now I have the need to allow a plugin to view which plugins are currently running and perhaps start/stop a specific plugin.
I know how to issue commands from the wrapper, in the below code for example:
using System;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
namespace Wrapper
{
public class RemoteLoader : MarshalByRefObject
{
private Assembly _pluginAassembly;
private object _instance;
private string _name;
public RemoteLoader(string assemblyName)
{
_name = assemblyName;
if (_pluginAassembly == null)
{
_pluginAassembly = AppDomain.CurrentDomain.Load(assemblyName);
}
// Required to identify the types when obfuscated
Type[] types;
try
{
types = _pluginAassembly.GetTypes();
}
catch (ReflectionTypeLoadException e)
{
types = e.Types.Where(t => t != null).ToArray();
}
var type = types.FirstOrDefault(type => type.GetInterface("IPlugin") != null);
if (type != null && _instance == null)
{
_instance = Activator.CreateInstance(type, null, null);
}
}
public void Start()
{
if (_instance == null)
{
return;
}
((IPlugin)_instance).OnStart();
}
public void Stop()
{
if (_instance == null)
{
return;
}
((IPlugin)_instance).OnStop(close);
}
}
}
So then I could, for example:
var domain = AppDomain.CreateDomain(Name, null, AppSetup);
var assemblyPath = Assembly.GetExecutingAssembly().Location;
var loader = (RemoteLoader)Domain.CreateInstanceFromAndUnwrap(assemblyPath, typeof(RemoteLoader).FullName);
loader.Start();
Of course the above is just a resumed sample...
Then on my wrapper I have methods like:
bool Start(string name);
bool Stop(string name);
Which basically is a wrapper to issue the Start/Stop of a specific plugin from the list and a list to keep track of running plugins:
List<Plugin> Plugins
Plugin is just a simple class that holds Domain, RemoteLoader information, etc.
What I don't understand is, how to achieve the below, from inside a plugin. Be able to:
View the list of running plugins
Execute the Start or Stop for a specific plugin
Or if this is even possible with MarshalByRefObject given the plugins are isolated or I would have to open a different communication route to achieve this?
For the bounty I am looking for a working verifiable example of the above described...
First let's define couple of interfaces:
// this is your host
public interface IHostController {
// names of all loaded plugins
string[] Plugins { get; }
void StartPlugin(string name);
void StopPlugin(string name);
}
public interface IPlugin {
// with this method you will pass plugin a reference to host
void Init(IHostController host);
void Start();
void Stop();
}
// helper class to combine app domain and loader together
public class PluginInfo {
public AppDomain Domain { get; set; }
public RemoteLoader Loader { get; set; }
}
Now a bit rewritten RemoteLoader (did not work for me as it was):
public class RemoteLoader : MarshalByRefObject {
private Assembly _pluginAassembly;
private IPlugin _instance;
private string _name;
public void Init(IHostController host, string assemblyPath) {
// note that you pass reference to controller here
_name = Path.GetFileNameWithoutExtension(assemblyPath);
if (_pluginAassembly == null) {
_pluginAassembly = AppDomain.CurrentDomain.Load(File.ReadAllBytes(assemblyPath));
}
// Required to identify the types when obfuscated
Type[] types;
try {
types = _pluginAassembly.GetTypes();
}
catch (ReflectionTypeLoadException e) {
types = e.Types.Where(t => t != null).ToArray();
}
var type = types.FirstOrDefault(t => t.GetInterface("IPlugin") != null);
if (type != null && _instance == null) {
_instance = (IPlugin) Activator.CreateInstance(type, null, null);
// propagate reference to controller futher
_instance.Init(host);
}
}
public string Name => _name;
public bool IsStarted { get; private set; }
public void Start() {
if (_instance == null) {
return;
}
_instance.Start();
IsStarted = true;
}
public void Stop() {
if (_instance == null) {
return;
}
_instance.Stop();
IsStarted = false;
}
}
And a host:
// note : inherits from MarshalByRefObject and implements interface
public class HostController : MarshalByRefObject, IHostController {
private readonly Dictionary<string, PluginInfo> _plugins = new Dictionary<string, PluginInfo>();
public void ScanAssemblies(params string[] paths) {
foreach (var path in paths) {
var setup = new AppDomainSetup();
var domain = AppDomain.CreateDomain(Path.GetFileNameWithoutExtension(path), null, setup);
var assemblyPath = Assembly.GetExecutingAssembly().Location;
var loader = (RemoteLoader) domain.CreateInstanceFromAndUnwrap(assemblyPath, typeof (RemoteLoader).FullName);
// you are passing "this" (which is IHostController) to your plugin here
loader.Init(this, path);
_plugins.Add(loader.Name, new PluginInfo {
Domain = domain,
Loader = loader
});
}
}
public string[] Plugins => _plugins.Keys.ToArray();
public void StartPlugin(string name) {
if (_plugins.ContainsKey(name)) {
var p = _plugins[name].Loader;
if (!p.IsStarted) {
p.Start();
}
}
}
public void StopPlugin(string name) {
if (_plugins.ContainsKey(name)) {
var p = _plugins[name].Loader;
if (p.IsStarted) {
p.Stop();
}
}
}
}
Now let's create two different assemblies. Each of them needs only to reference interfaces IPlugin and IHostController. In first assembly define plugin:
public class FirstPlugin : IPlugin {
const string Name = "First Plugin";
public void Init(IHostController host) {
Console.WriteLine(Name + " initialized");
}
public void Start() {
Console.WriteLine(Name + " started");
}
public void Stop() {
Console.WriteLine(Name + " stopped");
}
}
In second assembly define another plugin:
public class FirstPlugin : IPlugin {
const string Name = "Second Plugin";
private Timer _timer;
private IHostController _host;
public void Init(IHostController host) {
Console.WriteLine(Name + " initialized");
_host = host;
}
public void Start() {
Console.WriteLine(Name + " started");
Console.WriteLine("Will try to restart first plugin every 5 seconds");
_timer = new Timer(RestartFirst, null, 5000, 5000);
}
int _iteration = 0;
private void RestartFirst(object state) {
// here we talk with a host and request list of all plugins
foreach (var plugin in _host.Plugins) {
Console.WriteLine("Found plugin " + plugin);
}
if (_iteration%2 == 0) {
Console.WriteLine("Trying to start first plugin");
// start another plugin from inside this one
_host.StartPlugin("Plugin1");
}
else {
Console.WriteLine("Trying to stop first plugin");
// stop another plugin from inside this one
_host.StopPlugin("Plugin1");
}
_iteration++;
}
public void Stop() {
Console.WriteLine(Name + " stopped");
_timer?.Dispose();
_timer = null;
}
}
Now in your main .exe which hosts all plugins:
static void Main(string[] args) {
var host = new HostController();
host.ScanAssemblies(#"path to your first Plugin1.dll", #"path to your second Plugin2.dll");
host.StartPlugin("Plugin2");
Console.ReadKey();
}
And the output is:
First Plugin initialized
Second Plugin initialized
Second Plugin started
Will try to restart first plugin every 5 seconds
Found plugin Plugin1
Found plugin Plugin2
Trying to start first plugin
First Plugin started
Found plugin Plugin1
Found plugin Plugin1
Found plugin Plugin2
Trying to stop first plugin
Found plugin Plugin2
Trying to stop first plugin
First Plugin stopped
First Plugin stopped
Found plugin Plugin1
Found plugin Plugin2
Trying to stop first plugin
You can make a plugin ask it's host to perform these actions. You can pass to the RemoteLoader an instance of a MarshalByRefObject derived class that is created by the host. The RemoteLoader can then use that instance to perform any action.
You also can make the plugins communicate with each other by passing a suitable MarshalByRefObject from the host to each plugin. I'd recommend routing all actions through the host, though, because it's a simpler architecture.

event - object referance is not set to an instanse of an object windows forms C#

I am trying to raise an event in a DLL file refrenced to a windows forms project.
I have the following message when I run the program "Object is not set to an instace of an object":
namespace Server
{
public delegate void messageHnadler();
public class ClassServer
{
public event messageHnadler messageForChat
public string Message { get; set; }
public Socket listenerSocket;
public BinaryFormatter transBinary;
public Thread threadingServer;
public TcpListener listenerServer;
private List<TcpClient> connectedClients = new List<TcpClient>();
public bool OpenServer(string ipAddress, int PortNumber)
{
try
{
listenerServer = new TcpListener(IPAddress.Parse(ipAddress), PortNumber);//creating listener for clients to connect
listenerServer.Start();
threadingServer = new Thread(LoopThroughClients);
threadingServer.Start();
threadingServer = new Thread(GetMessage);
threadingServer.Start();
return true;
}
catch (Exception)
{
return false;
}
}
public void LoopThroughClients()
{
listenerSocket = listenerServer.AcceptSocket();
}
public void GetMessage()
{
while (true)
{
if (listenerSocket != null)
{
NetworkStream streamWithClient = new NetworkStream(listenerSocket);
transBinary = new BinaryFormatter();
string stringFromClient = (string)transBinary.Deserialize(streamWithClient);
if (stringFromClient != null)
{
Message = stringFromClient;
messageForChat();
}
streamWithClient = new NetworkStream(listenerSocket);
BinaryFormatter tranBinary = new BinaryFormatter();
tranBinary.Serialize(streamWithClient, stringFromClient);
stringFromClient = null;
}
}
}
In the windows forms project I signed the event to a function:
namespace Chat_Project_Server_UI
{
public partial class SeverUI : Form
{
OpenServerForm openServer = new OpenServerForm();
ClassServer serverForEvent = new ClassServer();
public SeverUI()
{
InitializeComponent();
openServer.ShowDialog();
serverForEvent.messageForChat += new messageHnadler(serverForEvent_messageForChat);
OpenningServer();
}
public void OpenningServer()
{
if(openServer.IsConnected)
{
ChatTextBox.AppendText("SERVER OPEN!\n");
}
else
{
ChatTextBox.AppendText("Faild to open server...\n");
}
}
private void test_Click(object sender, EventArgs e)
{
ChatTextBox.AppendText("aaaaa");
}
public void EventHolder()
{
}
void serverForEvent_messageForChat()
{
ChatTextBox.AppendText(serverForEvent.Message);
}
}
Always check if a handler has been assigned first as follows:
var handler = messageForChat;
if (handler != null)
handler()
Standard Way to handle Events inside the Class that defines it Is to Create A Method Named OnXxx and Always Check whether the Event is Assigned Handler Or Not
in your case define new function as following:
protected void OnMessageForChat(){
//protected modifier allows subclasses to raise the event by calling this method
if (messageForChat!=null)messageForChat();
}
And WhenEver you want to raise the event Just Call this Function

Correct Event handling in C#

this is basically a follow up to a previous question (Triggering an event in c# from c++ and declaring LPCWSTR). I've revised my code based on the answers and comments I have received and I solved the initial issue, which was passing the event to the GpioSetupInterruptPin from a gpio api. I don't have a lot of documentation on the api but what i'm trying to achieve is: have a form with a white label; after pressing a switch, the label turns yellow.
The problem i'm having now is the event seems to trigger as soon as it's created (the "execute" message is passed to the debug dialog and the label turns yellow) but it doesn't do anything when i toggle the switch. I was told in the last question to use WaitForSingleObject but i'm not really sure where to call it and this article only added to my confusion.
public partial class Form1 : Form
{
// P/Invoke CreateEvent and WaitForSingleObject
private void GPIO_Open() //get handle for gpio
private void GPIO_Output() //output pin declaration
private void button1_Click(object sender, EventArgs e)
{
Interrupt_Setup();
}
private void Interrupt_Setup()
{
hGPIO = GPIOapi.GpioOpenHandle(); //returns a handle to the gpio
GIPO_ON = true;
Debug.WriteLine("Driver open \n" + hGPIO);
GPIO_Output(); //set output pins
GPIO_Interrupt(Trigger); //configure interrupt
}
private void GPIO_Interrupt(string trigger)
{
bool ok;
_Main();
//INTERRUPT DECALRATION
ok = GPIOapi.GpioSetupInterruptPin(hGPIO, port6, 4, GPIOapi.INT_TRIGGER_MODE.TRIGGER_MODE_EDGE,
GPIOapi.INT_TRIGGER_POLARITY.TRIGGER_POL_HIGH_RISING, trigger, true);
Thread waitThread=new Thread(WaitForTrigger);
waitThread.Start();
if (!ok)
Debug.WriteLine("NO interrupt");
else
Debug.WriteLine("Interrupt set for:" + port6 + "04" + " at " + hGPIO);
}
public static string Trigger = "InputProcessUpdateHandler";
public static IntPtr handle = CreateEvent(IntPtr.Zero, false, false, Trigger); //used P/Invoke
private static InputProcessor inputProcessor = null;
public Color[] color =
{
Color.Orchid, Color.DarkOrchid, Color.GreenYellow, Color.CornflowerBlue, Color.SteelBlue,Color.Crimson
};
public int i = 0;
public void WaitForTrigger()
{
while(true)
{try
{
if (WaitForSingleObject(handle, 0xFFFFFFFF) == false)
{
BeginInvoke(((System.Action)(() =>label2.BackColor = color[i])));
i++;
if (i > 4)
i = 0;
}
Thread.Sleep(300);
}
catch (Exception e)
{ Debug.WriteLine("exception: " + e); }}
}
}
private void _Main()
{
inputProcessor = new InputProcessor();
ShowToggle showToggle = new ShowToggle(inputProcessor);
inputProcessor.Process(label1);
}
public class ShowToggle
{
private InputProcessor _inputProcessor = null;
public ShowToggle(InputProcessor inputProcessor)
{
_inputProcessor = inputProcessor;
_inputProcessor.updateHandledBy += InputProcessUpdateHandler;
}
private void InputProcessUpdateHandler(Label label)
{
label.BackColor = Color.Yellow;
Debug.Write("execute");
}
}
public class InputProcessor
{
public delegate void InputProcessUpdateHandler(Label label);
public event InputProcessUpdateHandler updateHandledBy = null;
public void Process(Label label)
{
if (updateHandledBy != null)
updateHandledBy(label);
}
}
If anyone could help me with this, I would be very grateful.
*** I got it working but it looks a right mess. Could anyone help me straighten it out?
You code is really confusing to me. I think what you want is something like this. Bear in mind I'm typing this into the SO text editor, so don't expect it to compile and just work - it's a guide. Consider it a step above pseudocode.
public class DeviceInterrupt
{
IntPtr m_gpio;
string m_eventName;
public event EventHandler OnInterrupt;
public DeviceInterrupt(int port)
{
// get a driver handle
m_gpio = GPIO_Open();
// generate some unique event name
m_eventName = "GPIO_evt_" + port;
// wire up the interrupt
GpioSetupInterruptPin(m_gpio, port, m_eventName, ...);
// start a listener
new Thread(EventListenerProc)
{
IsBackground = true,
Name = "gpio listener"
}
.Start();
}
public void Dispose()
{
// TODO: release the handle
}
private void EventListenerProc()
{
// create the event with the name we sent to the driver
var wh = new WaitHandle(false, m_eventName);
while (true)
{
// wait for it to get set by the driver
if (wh.WaitOne(1000))
{
// we have an interrupt
OnInterrupt.Fire(this, EventArgs.Empty);
}
}
}
}
Usage would then be something like this:
var intr = new DeviceInterrupt(4);
intr.OnInterrupt += MyHandler;
....
void MyHandler(object sender, EventArgs a)
{
Debug.WriteLine("Interrupt occurred!");
}
Note
The Compact Framework doesn't support actual named system events, so the named WaitHandle I use in my code above is not a CF-supplied WaitHandle. Instead I'm using the one from the Smart Device Framework. You could also P/Invoke to CreateEvent and WaitForSingleObject yourself.

IObserver and IObservable in C# for Observer vs Delegates, Events

All I am trying to do is implementing the observer pattern.
So, I came up with this solution:
We have a PoliceHeadQuarters whose primary job is to send notifications to all those who are subscribed to it. Consider that the DSP, Inspector and SubInspector classes are subscribed to PoliceHeadQuarters.
Using Events and Delegates I wrote
public class HeadQuarters
{
public delegate void NewDelegate(object sender, EventArgs e);
public event EventHandler NewEvent;
public void RaiseANotification()
{
var handler = this.NewEvent;
if (handler != null)
{
handler(this, new EventArgs());
}
}
}
public class SubInspector
{
public void Listen(object sender, EventArgs e)
{
MessageBox.Show(string.Format("Event Notification received by sender = {0} with eventArguments = {1}", sender, e.ToString()));
}
}
public class Inspector
{
public void Listen(object sender, EventArgs e)
{
MessageBox.Show(string.Format("Event Notification received by sender = {0} with eventArguments = {1}", sender, e.ToString()));
}
}
and this is how I invoked it
var headQuarters = new HeadQuarters();
var SubInspector = new SubInspector();
var Inspector = new Inspector();
headQuarters.NewEvent += Inspector.Listen;
headQuarters.NewEvent += SubInspector.Listen;
headQuarters.RaiseANotification();
so, both Inspector and SubInspector classes get notification whenever there the function RaiseANotification() is invoked.
It seems that the DotNet Framework 4, 4.5 supports a new way called IObserver and IObservable.
Can anyone give me a super simple example using IObservable and IObserver pattern for the above scenario? I googled only to find the available examples in the internet too bloated and difficult to understand.
My hinch: (probably i think it's wrong)
class DSP : IObserver //since it observes the headquarters ?
class PoliceHeadQuarters: IObservable // since here's where we send the notifications ?
Thanks in advance.
EDIT: Somebody also said that the MSDN documentation is also incorrect for IObservable #
IObservable vs Plain Events or Why Should I use IObservable?.
Here's a modification of MSDN example to fit your framework:
public struct Message
{
string text;
public Message(string newText)
{
this.text = newText;
}
public string Text
{
get
{
return this.text;
}
}
}
public class Headquarters : IObservable<Message>
{
public Headquarters()
{
observers = new List<IObserver<Message>>();
}
private List<IObserver<Message>> observers;
public IDisposable Subscribe(IObserver<Message> observer)
{
if (!observers.Contains(observer))
observers.Add(observer);
return new Unsubscriber(observers, observer);
}
private class Unsubscriber : IDisposable
{
private List<IObserver<Message>> _observers;
private IObserver<Message> _observer;
public Unsubscriber(List<IObserver<Message>> observers, IObserver<Message> observer)
{
this._observers = observers;
this._observer = observer;
}
public void Dispose()
{
if (_observer != null && _observers.Contains(_observer))
_observers.Remove(_observer);
}
}
public void SendMessage(Nullable<Message> loc)
{
foreach (var observer in observers)
{
if (!loc.HasValue)
observer.OnError(new MessageUnknownException());
else
observer.OnNext(loc.Value);
}
}
public void EndTransmission()
{
foreach (var observer in observers.ToArray())
if (observers.Contains(observer))
observer.OnCompleted();
observers.Clear();
}
}
public class MessageUnknownException : Exception
{
internal MessageUnknownException()
{
}
}
public class Inspector : IObserver<Message>
{
private IDisposable unsubscriber;
private string instName;
public Inspector(string name)
{
this.instName = name;
}
public string Name
{
get
{
return this.instName;
}
}
public virtual void Subscribe(IObservable<Message> provider)
{
if (provider != null)
unsubscriber = provider.Subscribe(this);
}
public virtual void OnCompleted()
{
Console.WriteLine("The headquarters has completed transmitting data to {0}.", this.Name);
this.Unsubscribe();
}
public virtual void OnError(Exception e)
{
Console.WriteLine("{0}: Cannot get message from headquarters.", this.Name);
}
public virtual void OnNext(Message value)
{
Console.WriteLine("{1}: Message I got from headquarters: {0}", value.Text, this.Name);
}
public virtual void Unsubscribe()
{
unsubscriber.Dispose();
}
}
public class Program
{
public static void Main(string[] args)
{
Inspector inspector1 = new Inspector("Greg Lestrade");
Inspector inspector2 = new Inspector("Sherlock Holmes");
Headquarters headquarters = new Headquarters();
inspector1.Subscribe(headquarters);
inspector2.Subscribe(headquarters);
headquarters.SendMessage(new Message("Catch Moriarty!"));
headquarters.EndTransmission();
Console.ReadKey();
}
}
Another suggestion - you probably want to consider leveraging the reactive extensions library for any code using IObservable. The nuget package is Rx-Main and the homepage for it is here: http://msdn.microsoft.com/en-us/data/gg577609.aspx
Update: ReactiveX has been translated to many platforms and languages and is now managed as an open source project. Here is the landing page.
This will save you a lot of boilerplate code. Here's a super simple example:
var hq = new Subject<string>();
var inspectorSubscription = hq.Subscribe(
m => Console.WriteLine("Inspector received: " + m));
var subInspectorSubscription = hq.Subscribe(
m => Console.WriteLine("Sub Inspector received: " + m));
hq.OnNext("Catch Moriarty!");
It will output:
Inspector received: Catch Moriarty!
Sub Inspector received: Catch Moriarty!
Reactive Extensions is a big subject, and a very powerful library - worth investigating. I recommend the hands-on lab from the link above.
You would probably want to embed those subscriptions within your Inspector, SubInspector immplementatinos to more closely reflect your code. But hopefully this gives you an insight into what you can do with Rx.

Windows service based on plugin architecture

I am building a windows service based on plugin architecture and I run into some problem.
The problem is I want plugin to fire event on the main application. Here is some code.
These are delegates
namespace eTreasury.SchedulerInterface
{
public enum Severity
{
Message,
Warning,
Error
}
public delegate void ErrorHandler(string message, Severity errorSeverity);
public delegate void CompletedHandler(string message);
public delegate void ProgressReportHandler(string message, int percentCompleted);
}
This is interface
public interface IPluginInterface : IDisposable
{
string Identifier{ get; }
void Run();
void Dispose();
event ErrorHandler OnError;
event CompletedHandler OnCompleted;
event ProgressReportHandler OnProgress;
}
This is base class I want all plugins to inherit from
public abstract class BasePlugin : MarshalByRefObject, IPluginInterface
{
protected string _identifier;
public string Identifier
{
get { return _identifier; }
}
public abstract void Run();
protected void ReportError(string message, Severity errorSeverity)
{
if (OnError != null)
OnError(message, errorSeverity);
}
protected void ReportProgress(string message, int percentCompleted)
{
if (OnProgress != null)
OnProgress(message, percentCompleted);
}
protected void ReportProgress(string message)
{
ReportProgress(message, 0);
}
protected void ReportCompleted(string message)
{
if (OnCompleted != null)
OnCompleted(message);
}
public void Dispose()
{
OnError = null;
OnCompleted = null;
OnProgress = null;
_identifier = null;
}
public event ErrorHandler OnError;
public event CompletedHandler OnCompleted;
public event ProgressReportHandler OnProgress;
}
This is plugin
public class CurrencyRatesPlugin : BasePlugin
{
public CurrencyRatesPlugin()
{
_identifier = "CurrencyRatesPlugin";
}
public override void Run()
{
try
{
ReportProgress("bla", 0);
}
catch (Exception e)
{
ReportError("bla");
}
}
}
And this is my windows service code
public partial class CurrencyRatesPluginService : ServiceBase
{
AppDomain appDomain;
IPluginInterface pluginInterface;
System.Timers.Timer Timer = null;
public CurrencyRatesPluginService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
this.appDomain = CreateAppDomain();
this.pluginInterface = (IPluginInterface)appDomain.CreateInstanceFrom("C:\\eTreasuryScheduler\\Plugins\\eTreasury.CurrencyRatesPlugin.dll", "eTreasury.Plugins.CurrencyRatesPlugin.CurrencyRatesPlugin").Unwrap();
PluginSection section = (PluginSection)ConfigurationManager.GetSection("PluginSectionGroup/PluginSection");
if (section == null)
{
EventLogManager.LogError("bla");
}
else
{
Timer = new System.Timers.Timer();
Timer.Enabled = false;
Timer.Interval = section.PluginItems[0].Interval;
Timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
Timer.Start();
}
}
catch(Exception ex)
{
EventLogManager.LogError(String.Format("{0}...{1}", ex.Message, ex.InnerException == null ? string.Empty : ex.InnerException.Message));
}
}
protected override void OnStop()
{
Timer.Stop();
this.pluginInterface.Dispose();
AppDomain.Unload(this.appDomain);
}
void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Process();
}
void Process()
{
try
{
this.pluginInterface.OnProgress += ProcessProgressReportHandler;
this.pluginInterface.OnCompleted += ProcessCompletedHandler;
pluginInterface.Run();
this.pluginInterface.OnProgress -= ProcessProgressReportHandler;
this.pluginInterface.OnCompleted -= ProcessCompletedHandler;
}
catch (Exception ex)
{
EventLogManager.LogError(String.Format("{0}...{1}", ex.Message, ex.InnerException == null ? string.Empty : ex.InnerException.Message));
}
}
private void ProcessProgressReportHandler(string message, int percentCompleted)
{
EventLogManager.LogInformation(message);
}
private void ProcessCompletedHandler(string message)
{
EventLogManager.LogInformation(message);
}
AppDomain CreateAppDomain()
{
...
}
}
And everythig is working fine except the events.
The error occurs here
this.pluginInterface.OnProgress += ProcessProgressReportHandler;
And the error message is
Exception has been thrown by the target of an invocation....Could not load file or assembly 'eTreasury.SchedulerService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
I`ve added IHost interface and Initialize methods in plugin interface. When loading plugin in host application I call Initialize method of that plugin and pass Host to it. After this I have ability to call host methods from plugin.

Categories

Resources