EASYNETQ Help Class
public class Mqhelper {//<summary>////</summary> public static Voi D Publish (Message msg) {////Creates a message bus IBus bus = Busbuilder.createmessagebus (); try {Bus. Publish (msg, x = X.withtopic (msg). Messagerouter)); } catch (Easynetqexception ex) {System.Console.WriteLine ("Send message:" + ex.) Message); Handle connection message Server exception} bus. Dispose ();//Similar to database connection, remember to destroy bus objects after use}///<summary>///Receive messages//</summary> <param name= "MSG" ></param> public static void Subscribe (Message msg, iprocessmessage ipro) {////Create message bus IBus bus = Busbuilder.createmessagebus (); try {Bus. Subscribe<message> (Msg. Messagerouter, message = ipro. PROCESSMSG (message), x = X.withtopic (Msg. Messagerouter)); System.Console.WriteLine ("Subscription message success"); } catch (Easynetqexception ex) {System.Console.WriteLine ("subscription message failed:" + ex. Message); }//Similar to database connection, remember to destroy bus objects after use}}
public class Busbuilder {public static IBus Createmessagebus () { //Message server connection string var connectionString = configurationmanager.connectionstrings["RabbitMQ"]; if (connectionString = = NULL | | connectionstring.connectionstring = = string. Empty) { throw new Exception ("Messageserver connection string is missing or empty"); } Return Rabbithutch.createbus (connectionstring.connectionstring); } }
Public interface iprocessmessage { void processmsg (Message msg); void Notice (); }
public class Message {public string MessageID {get; set;} public string MessageTitle {get; set;} public string MessageBody {get; set;} public string Messagerouter {get; set;} }
RabbitMQ EASYNETQ Usage