locked
Sending messages between application directories RRS feed

  • Question

  • hello,

    I'm trying to create a senario where i could sort of pass a message from one application directory to another.  Both applications use the same database, and i want a case where when one application updates a database, the other is notified.

    What i've tried to do is to have a class/dll called publisher that contains a static event handler and a static public method that can be used to raise the event.  Therefore Applicaition B can subsribe to an event, and Application A could raise the event.  Both applications reference the dll with i've installed in the global assembly cache.

    The publisher.cs file, which as been complied into a dll and installed in the GAC, is as follows

    public class Publisher

    {

    //delegate

    public delegate void BankAddedEventHandler(object sender, PublisherEventArgs me);

    //event handler

    public static event BankAddedEventHandler OnBankAdded;

    public Publisher()

    {

    }

    public static void AnnounceBankAdded()

    {

    PublisherEventArgs e = new PublisherEventArgs();

    if(Publisher.OnBankAdded != null)

    OnBankAdded(null, e);

    }

    /// <summary>

    /// Custom Event Args

    /// </summary>

    public class PublisherEventArgs: EventArgs

    {

    public PublisherEventArgs(){ }

    }

    The piece of code in Application A, which updates the database and raise the static event is as follows

    private void Button1_Click(object sender, System.EventArgs e)

    {

    try

    {

    this.cmdInsertBank.Parameters["@bank"].Value = this.TextBox1.Text;

    this.connTest.Open();

    this.cmdInsertBank.ExecuteNonQuery();

    Publisher.AnnounceBankAdded();//The static method of the publisher.dll which raises the event

    }

    catch(Exception ex)

    {

    throw ex;

    }

    finally

    {

    this.connTest.Close();

    }

    }

    The piece of code in Application B which subscribes the the event handler is as follows. Note I placed this code in the Global.asax file.

    protected void Application_Start(Object sender, EventArgs e)

    {

    try

    {

    Publisher.OnBankAdded += new Publisher.BankAddedEventHandler(BankAdded);

    }

    catch

    {

    }

    }

    Method that is supposed to be called whenevent is raised

    public void BankAdded(object Sender, Publisher.PublisherEventArgs e)

    {

    System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();

    try

    {

    conn.ConnectionString = "workstation id=DEVELOPER;packet size=4096;integrated security=SSPI;data source=\"DEVELOPER\\SQL2000\";persist security info=True;initial catalog=Test";

    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();

    cmd.CommandType = System.Data.CommandType.StoredProcedure;

    cmd.CommandText = "GetBanks";

    cmd.Connection = conn;

    conn.Open();

    using(System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader())

    {

    if(reader.HasRows)

    {

    ArrayList Banks = new ArrayList();

    while(reader.Read())

    {

    Banks.Add(reader.GetString(1));

    }

     Application.Lock();

     Application["Banks"] = Banks;

     Application.UnLock();

    }

    }

    }

    catch(Exception ex)

    {

    throw ex;

    }

    finally

    {

    conn.Close();

    }

    }

    The problem I'm having is that the event does not get raised, during debugging, although application B subsribed to the event,

    the event handler is empty when it is raised .ie "if(Publisher.OnBankAdded != null)" always returns null

    thanks

     

    Monday, July 17, 2006 12:52 PM

Answers

  • It's not possible to raise an event between two separate applications, at least not .NET events.

    What you'd probably want to look at is a named WaitHandle-based object, if you're in .NET 2.0.  EventWaitHandle would be a good choice if there's only a one-to-one relationship, if you think you'll have a one-to-many relationship a semaphore is a better choice.

    When you open a named WaitHandle object you're opening a system-wide resource that can also be opened by any other application (with the correct, optional, permissions).  Two applications can then communicate between them, in this case, state change information.

    Monday, July 17, 2006 1:32 PM