c# - Subscribe to an event raised by a Controller in other project -


i have 2 projects in same solution. 1 asp.net mvc application (a) , other console application (c). want c subscribe event raised controller in a. using singleton class raises event , can take subscriptions event.

i subscribed event , checked (using debugger) subscription indeed stored , can confirm event gets raised @ right time.

the problem happens when start using projects - though c subscribes event, when raises subscription not in eventhandler listener list event not cought in c.

it singleton goes out of scope or same instance not used between projects. kinda filling have 2 instances of singleton.

my goal receive event controller in project. singleton class can avoided if not necessary.

any appreciated.

controller (in project a)

[httppost] public void processcommandmessage(message message) {     messagespy.instance.onmessagereceived(message); } 

messagespy (in project a)

{     private static readonly messagespy _instance = new messagespy();     public static messagespy instance { { return _instance; } }     static messagespy() { }     private messagespy() { }      public event eventhandler<message> messagereceived;        public virtual void onmessagereceived(message message)     {         if (messagereceived != null)         {             messagereceived(this, message);         }     }      } 

subscriber (in project c)

private void run() {     slackmessagespy.instance.messagereceived += instance_messagereceived; }  static void instance_messagereceived(object sender, outgoinghookmessage e) {     trace.writeline(e.text); } 

you added project reference web app console app assume? not going work. have stated correctly: have 2 instances. both running in own separated process , should communicate between them using messaging.

you have tight coupling between web app , console app using slackmessagespy. should decouple these events instead using messaging component signalr (https://www.asp.net/signalr) or webhooks (https://docs.asp.net/projects/webhooks/en/latest/overview.html) example. controller sends signalr message or calls webhook , console app subscribes message.

it might simple have controller writing small files information location console app has access , have console app read files.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -