473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Windows Service with Publish/Subscribe Pattern

I have two windows services that I've written: the first is a "Listener
Service" that listens for MSMQ Message delivery. The second is a
"Watcher Service" that monitors the state of the first. If the
"Listener Service" goes down I want the "Watcher Service" to receive a
notification and perform a set action.

To enable this communication I've written a separate assembly (that
both services reference) that exposes two interfaces :
IServiceStatePublisher and IServiceStateSubscriber. The way that I
register the "Watcher Service" as a subscriber to the "Listener
Service" is with the following code in the OnStart event of the
"Watcher Service"::

protected override void OnStart(string[] args)
{
//Line 1
ServiceBase qw = new QueueWatcher();
//Line 2
((IServiceStatePublisher)fd).addSubscriber((IServi ceStateSubscriber)this);
//Line 3
ServiceBase.Run(qw);
}

I thought it reasonable that this would work, however, when I start the
"Watcher Service" the Application Event log shows the message "Service
cannot be started. An instance of the service is already running". I've
determined that the error is specific to the code at Line 3, but I
can't identify the reason it's happening. Neither service is running
when the "Watcher Service" is started and I don't have the ability to
step into the ServiceBase code to see what's going on there.

I'm stumped, so any help is greatly appreciated.

Aug 8 '06 #1
4 6421
cm********@gmail.com wrote in
news:11*********************@i42g2000cwa.googlegro ups.com:
I have two windows services that I've written: the first is a
"Listener Service" that listens for MSMQ Message delivery. The second
is a "Watcher Service" that monitors the state of the first. If the
"Listener Service" goes down I want the "Watcher Service" to receive a
notification and perform a set action.
Perhaps a better route would be to completely decouple the two services,
and write a separate (new) 'watcher' service that utilizes the
ServiceController class to monitor the status of the Listener service, and
restart it if necessary.

-mdb
Aug 8 '06 #2
cmgarcia,
You can use this pattern to debug a windows service without it being
installed. It should be self-explanatory:

// The main entry point for the process
static void Main()
{
#if (!DEBUG)
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
#else
// debug code: allows the process to run as a non-service
// will kick off the service start point, but never kill it
// shut down the debugger to exit
Service1 service = new Service1();
service.();
System.Threading.Thread.Sleep(System.Threading.Tim eout.Infinite);
#endif
}

========================================
Possibly the problem you describe is caused by forgetting to make both the
service and the installer classes all have distinct, separate service names?
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"cm********@gmail.com" wrote:
I have two windows services that I've written: the first is a "Listener
Service" that listens for MSMQ Message delivery. The second is a
"Watcher Service" that monitors the state of the first. If the
"Listener Service" goes down I want the "Watcher Service" to receive a
notification and perform a set action.

To enable this communication I've written a separate assembly (that
both services reference) that exposes two interfaces :
IServiceStatePublisher and IServiceStateSubscriber. The way that I
register the "Watcher Service" as a subscriber to the "Listener
Service" is with the following code in the OnStart event of the
"Watcher Service"::

protected override void OnStart(string[] args)
{
//Line 1
ServiceBase qw = new QueueWatcher();
//Line 2
((IServiceStatePublisher)fd).addSubscriber((IServi ceStateSubscriber)this);
//Line 3
ServiceBase.Run(qw);
}

I thought it reasonable that this would work, however, when I start the
"Watcher Service" the Application Event log shows the message "Service
cannot be started. An instance of the service is already running". I've
determined that the error is specific to the code at Line 3, but I
can't identify the reason it's happening. Neither service is running
when the "Watcher Service" is started and I don't have the ability to
step into the ServiceBase code to see what's going on there.

I'm stumped, so any help is greatly appreciated.

Aug 8 '06 #3
If I do that I lose the notification event model that the design
pattern gives me. Initially I was going to write the "Watcher Service"
to poll the ServiceControlManager at periodic intervals, but I want to
know why and when the state of the "Listener Service" changes. A
polling service doesn't provide that level of detail.

Michael Bray wrote:
cm********@gmail.com wrote in
news:11*********************@i42g2000cwa.googlegro ups.com:
I have two windows services that I've written: the first is a
"Listener Service" that listens for MSMQ Message delivery. The second
is a "Watcher Service" that monitors the state of the first. If the
"Listener Service" goes down I want the "Watcher Service" to receive a
notification and perform a set action.

Perhaps a better route would be to completely decouple the two services,
and write a separate (new) 'watcher' service that utilizes the
ServiceController class to monitor the status of the Listener service, and
restart it if necessary.

-mdb
Aug 9 '06 #4
I checked on the info you provided and the services and installer
classes all have unique names. I can even start and stop the "Listener
Service" through the MMC or shell with a NET command and once running
it'll handle MSMQ Message arrival without any errors. The problem is
when I try to start that service from within the "Watcher Service". Am
I trying to do something that the .NET Framework doesn't support???

Peter wrote:
cmgarcia,
You can use this pattern to debug a windows service without it being
installed. It should be self-explanatory:

// The main entry point for the process
static void Main()
{
#if (!DEBUG)
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRu n);
#else
// debug code: allows the process to run as a non-service
// will kick off the service start point, but never kill it
// shut down the debugger to exit
Service1 service = new Service1();
service.();
System.Threading.Thread.Sleep(System.Threading.Tim eout.Infinite);
#endif
}

========================================
Possibly the problem you describe is caused by forgetting to make both the
service and the installer classes all have distinct, separate service names?
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"cm********@gmail.com" wrote:
I have two windows services that I've written: the first is a "Listener
Service" that listens for MSMQ Message delivery. The second is a
"Watcher Service" that monitors the state of the first. If the
"Listener Service" goes down I want the "Watcher Service" to receive a
notification and perform a set action.

To enable this communication I've written a separate assembly (that
both services reference) that exposes two interfaces :
IServiceStatePublisher and IServiceStateSubscriber. The way that I
register the "Watcher Service" as a subscriber to the "Listener
Service" is with the following code in the OnStart event of the
"Watcher Service"::

protected override void OnStart(string[] args)
{
//Line 1
ServiceBase qw = new QueueWatcher();
//Line 2
((IServiceStatePublisher)fd).addSubscriber((IServi ceStateSubscriber)this);
//Line 3
ServiceBase.Run(qw);
}

I thought it reasonable that this would work, however, when I start the
"Watcher Service" the Application Event log shows the message "Service
cannot be started. An instance of the service is already running". I've
determined that the error is specific to the code at Line 3, but I
can't identify the reason it's happening. Neither service is running
when the "Watcher Service" is started and I don't have the ability to
step into the ServiceBase code to see what's going on there.

I'm stumped, so any help is greatly appreciated.
Aug 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ollie | last post by:
First off I am using windows server 2003, IIS 6.0 .Net framework 1.1.4322..... I am attempting to use COM+ Loosely Coupled Events (LCE) from an asp.net web service but I am receiving a...
1
by: Peter Rilling | last post by:
I have an EXE that I would like to be able to run from either the command-line or as a windows service. Is there a way that I can tell which context the program is running in? Basically, if it...
3
by: BC | last post by:
Hi all, I have a windows service (on client machine) that monitors a MSMQ queue (on server machine). When a new message is in the server queue, the client windows service will get the message...
3
by: Chris Dunaway | last post by:
I am writing a Windows Service that reads and processes files on a set schedule. I want to create a second app that can monitor the Windows service. The Windows service will write trace messages...
3
by: Bragadiru | last post by:
Hi, I can publish My Asp.NET 2.0 web service, through a WebSetup project, on a Windows XP Pro Sp2 and it works fine. When I'm trying to publish it on a Windows 2003 Server IIS 6 => when I can...
0
by: Kamilche | last post by:
''' event.py An event manager using publish/subscribe, and weakrefs. Any function can publish any event without registering it first, and any object can register interest in any event, even...
2
by: gregory_may | last post by:
I am looking for a publish/subscribe mechanism for .Net/Atlas. Does this exist? I need to make web service callbacks to clients when specific events happen. It almost sounds like RSS, but I am...
2
by: shyam | last post by:
I have a C++ COM based windows service which have more than 30k lines of code and which is stablized over years. We need to take the advantage of the ..NET framework for this project, especially...
17
by: E11esar | last post by:
Hi there. I have written a web service in C# (VS2008) and ASP.Net and all is working fine with it. The problem I am having is when I expose the web service and hence then try to subscribe to its...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.