473,651 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 :
IServiceStatePu blisher and IServiceStateSu bscriber. 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
((IServiceState Publisher)fd).a ddSubscriber((I ServiceStateSub scriber)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 6445
cm********@gmai l.com wrote in
news:11******** *************@i 42g2000cwa.goog legroups.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
ServiceControll er 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.ServiceP rocess.ServiceB ase[] ServicesToRun;
ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new Service1() };
System.ServiceP rocess.ServiceB ase.Run(Service sToRun);
#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.Threadin g.Thread.Sleep( System.Threadin g.Timeout.Infin ite);
#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********@gma il.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 :
IServiceStatePu blisher and IServiceStateSu bscriber. 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
((IServiceState Publisher)fd).a ddSubscriber((I ServiceStateSub scriber)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 ServiceControlM anager 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********@gmai l.com wrote in
news:11******** *************@i 42g2000cwa.goog legroups.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
ServiceControll er 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.ServiceP rocess.ServiceB ase[] ServicesToRun;
ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new Service1() };
System.ServiceP rocess.ServiceB ase.Run(Service sToRun);
#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.Threadin g.Thread.Sleep( System.Threadin g.Timeout.Infin ite);
#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********@gma il.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 :
IServiceStatePu blisher and IServiceStateSu bscriber. 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
((IServiceState Publisher)fd).a ddSubscriber((I ServiceStateSub scriber)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
1116
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 'System.UnauthorizedAccessException' exception saying 'Access Denied' when attempting to call 'COMAdmin.ICatalogCollection.SaveChanges' method to subscribe to a transient subscriptions. The COM+ server runs under the 'network service' account and the...
1
5548
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 runs as a service, I would want it to call ServiceBase.Run (which will then call the execution logic). If it is run at the command-line, I simply want it to run as a normal application.
3
3598
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 and does some process. What I want to do now is create a small windows application that sits on the client machine and monitor the activities of the windows service. I am using Visual Studio .NET 2003 with C#. The question is how can I monitor a...
3
2604
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 at certain points while executing. The monitoring app I want to write will be very simple, just picking up these traces and displaying them to show progress of the service. My question is what is the best method of communication between the...
3
6254
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 access the web service but it is closed after few seconds (I know 'cause I have in global.asax.cs Application_Start/End Trace.WriteLine(DateTime.Now());)
0
2070
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 if it doesn't exist yet. The event manager uses weakrefs, so lists of listeners won't stop them
2
2351
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 not sure if there is a better fit?
2
2255
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 the GUI for this service and the other windows non gui .NET clients. What's the best migaration pattern for us considering not to rewrite the thousands of lines of code. The question i have are the follows.
17
2180
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 methods from a test application. When I start up the web service in VS2008, the url has the usual address-type of http://localhost:3998/Webservice1/Service.asmx.asmx and so I am finding that if I wish to use the web methods in my test application,...
0
8349
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8695
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8460
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7296
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4281
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.