Windows Service Administration with ASP.NET – Part 2 – Remoting to the Marshalled Object

Yesterday, I posted Windows Service Administration with ASP.NET – Part 1 – Marshalling Status Information to
show how to Marshal an object from a Windows Service to for monitoring
from an ASP.NET control.  Here’s how to remote to it and use it
from an ASCX control.

Step 1:  Add a method to
Activate the remotable object, and return the inner class. I’ve
implemented this in a Business-tier class, but you can put this code
directly in your .ascx control if you wish.


public WatchListStatus GetCurrentStatus()
{
  try
  {
  
  // Call GetObject to Activate our Remotable object
     WatchListStatusRemotable wlsr = (WatchListStatusRemotable) Activator.GetObject(
         
typeof(WatchListStatusRemotable), tcp://[SERVER]:8001/GateStatus”);

     // Return the inner class that contains the monitoring info
     
return
wlsr.ServiceStatus;
  }
  catch
(System.Exception ex)
  {
      System.Diagnostics.Trace.WriteLine(ex);
      System.Diagnostics.Trace.WriteLine(ex.InnerException);
     
return null
;
   }
}

Step 2: … Is so simple, I’m not
sure I even need to explain it.  Just call your Get method, and do
whatever you want with your remote object. 

WatchListStatus status = GetCurrentStatus();

foreach(WatchListStatusItem item in status.StatusHistory)
{
  // … Do some cool stuff with your remote object here and amaze your friends.
}

Part 3 – Controlling your Service. 

-Brendan

Music Tip – Explore a good Record Label, Like Candle Records

About Brendan Tompkins

Brendan runs CodeBetter.Com and Devlicio.Us. He is a former MVP for Microsoft .NET and is president of Port Technology Services, a partner with Port Solution Integrators a provider of hardware and software integration services for the transportation and logistics industry.
This entry was posted in ASP.NET Tutorials. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://www.mymailmarket.be myMailMarket

    Thanks Brendan, for our myMailMarket e-marketing tool we needed to have an admin page to control all running background services. This is just what we were looking for!

    Thx from the guys of http://www.mymailmarket.be

  • Charlie

    Here is more detail on the problem Im having:

    I have a service which periodically pings a remote IP address to determine connectivity. When connectivity is dropped or restored, I want to know the duration of the downtime, so I have a couple of classes storing that data.

    I need to be able to remotely query the object which is storing that data, so your code is a big help (if I can get it to go).

    Here is the server code which mashalls the object Im trying to use remotely (In OnStart):
    // Register the TCP Channel on port 8001
    ChannelServices.RegisterChannel(new TcpChannel(8001));

    // Register the type
    RemotingConfiguration.RegisterWellKnownServiceType (
    typeof(ConnectionStateRemotable),
    “cConnectionStateChanges”,
    WellKnownObjectMode.Singleton);

    // Now, Marshal it this will make it available to other processes via the TCP remoting channel
    try
    {
    RemotingServices.Marshal(m_ConnectionStatus, “cConnectionStateChanges”);
    eventLog1.WriteEntry(“cConnectionStateChanges Marshalled”);
    }
    catch (System.Exception ex)
    {
    eventLog1.WriteEntry(ex.Message);
    eventLog1.WriteEntry(ex.InnerException.Message );
    }

    The object does get marshalled (I see an event in the event log & no exceptions).

    As a test, I have a small windows forms app with a button which tries to get the remote object. Here is the code from the Click event and the GetCurrentStatus method:

    private void button1_Click(object sender, System.EventArgs e)
    {
    PingChecker.cConnectionStateChanges x = new PingChecker.cConnectionStateChanges();
    x = GetCurrentStatus();

    foreach(PingChecker.ConnectionStateChange item in x)
    {
    System.Diagnostics.Trace.WriteLine(“item.Timestamp=” + item.Timestamp.ToUniversalTime());
    }

    System.Diagnostics.Trace.WriteLine(“Count is: ” + x.Count);
    }
    public PingChecker.cConnectionStateChanges GetCurrentStatus()
    {
    try
    {
    // Call GetObject to Activate our Remotable object
    PingChecker.ConnectionStateRemotable cscr = (PingChecker.ConnectionStateRemotable) Activator.GetObject(
    typeof(PingChecker.ConnectionStateRemotable), “tcp://wired:8001/cConnectionStateChanges”);

    // Return the inner class that contains the monitoring info
    return cscr.ConnectionStateStatus ;
    }
    catch(System.Exception ex)
    {
    System.Diagnostics.Trace.WriteLine(ex);
    System.Diagnostics.Trace.WriteLine(ex.InnerException);
    return null;
    }
    }

    The problem I am seeing is after the call to Activator.GetObject an error in the cscr object stating “Cannot evaluate field of a proxy object”. The GetCurrentStatus method returns without an exception, but it comes back empty-handed – no PingChecker.cConnectionStateChanges object.

    TIA for your help on this.

    Charlie

  • Charlie

    Hi,

    Im trying to implement your code & have a question:

    -Your code references a methos ‘GateStatus’
    “tcp://[SERVER]:8001/GateStatus”

    Where is this method implemented?

    From my client code (Windows Form app), Im getting an exception of type “Requested Service not found” whn I try to return the object from the GetCurrentStatus() method.

  • Brendan Tompkins

    The principle should be the same, see part 3.

  • Yi

    What about the case for using Web Service to control Windows Service? I need to start or stop a windows service by sending a SOAP request to the web service on the same machine, haven’t figure out how to do it.

  • Albert

    Thank you for this article very helpful Brendan, i really need the third part.

  • George

    This is a useful tutorial ! Thank you and please write the Part 3 we really need it !

  • Carlos

    Please continue with third part. Thanks

  • Philip_C

    This is a great tutorial ! Thank you and please write the Part 3 i really need it !

    :D

  • Guilherme

    Dear,

    I’m new in .NET and i have to do a Windows Service that will monitoring two things:

    1)If a website is online.

    2)If a Data Base is ok, and the time of response os this Data Base.

    Can you help me ??

    Thanksss..

    Guilherme

    guimendonca@veloxmail.com.br