CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

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



Comments

Philip_C said:

This is a great tutorial ! Thank you and please write the Part 3 i really need it !
:D
# March 23, 2004 2:02 AM

Carlos said:

Please continue with third part. Thanks
# June 10, 2004 11:57 AM

George said:

This is a useful tutorial ! Thank you and please write the Part 3 we really need it !
# June 10, 2004 11:59 AM

Albert said:

Thank you for this article very helpful Brendan, i really need the third part.
# June 11, 2004 11:31 AM

Yi said:

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.
# July 13, 2004 10:47 AM

Brendan Tompkins said:

The principle should be the same, see part 3.
# July 13, 2004 12:17 PM

Charlie said:

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.
# September 12, 2005 3:57 PM

Charlie said:

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

# September 13, 2005 9:46 AM

myMailMarket said:

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
# January 27, 2006 4:26 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors