Brendan Tompkins

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted Thu, Nov 13 2003 7:59 AM by Brendan Tompkins
Filed under:

[Advertisement]

Comments

Guilherme wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Fri, Mar 12 2004 5:59 AM
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
Philip_C wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Tue, Mar 23 2004 2:02 AM
This is a great tutorial ! Thank you and please write the Part 3 i really need it !
:D
Carlos wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Thu, Jun 10 2004 11:57 AM
Please continue with third part. Thanks
George wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Thu, Jun 10 2004 11:59 AM
This is a useful tutorial ! Thank you and please write the Part 3 we really need it !
Albert wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Fri, Jun 11 2004 11:31 AM
Thank you for this article very helpful Brendan, i really need the third part.
Yi wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Tue, Jul 13 2004 10:47 AM
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.
Brendan Tompkins wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Tue, Jul 13 2004 12:17 PM
The principle should be the same, see part 3.
Charlie wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Mon, Sep 12 2005 3:57 PM
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.
Charlie wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Tue, Sep 13 2005 9:46 AM
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

myMailMarket wrote re: Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object
on Fri, Jan 27 2006 4:26 AM
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

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us