Click to See Complete Forum and Search --> : WCF and Window service


asafaa
June 28th, 2009, 02:12 AM
I need to create a window service and i want to create it using the latest c# technology.

i'm just reading about WCF and i see no article that explain the connection between WCF and Windows service (only WCF and Web Services).

Can i write Window service, in WCF ?

dannystommen
June 28th, 2009, 05:09 AM
Yes it is possible.

See the example http://msdn.microsoft.com/en-us/library/ms733069.aspx

Arjay
June 28th, 2009, 02:40 PM
They are different things.

Web Service (http://en.wikipedia.org/wiki/Web_services) - essentially and api (i.e. class and methods) exposed through the internet.
Windows Service (http://en.wikipedia.org/wiki/Windows_service) - an long running app that automatically starts at system startup and provides non-UI type functionality.
WCF Service (http://msdn.microsoft.com/en-us/library/ms731082.aspx) - in a nutshell WCF is the newer way to create web services over ASMX.

One of the differences between Web Service in ASP.Net and WCF (asmx), is that the older web service had to be hosted in IIS. With WCF you have several options for hosting: IIS (like the older ws), a console app, a Windows app, or a Windows service.

Decoupling from IIS has several benefits because, while you may want to host a web service, you may want to limit the security footprint and not have IIS running.

Since WCF services can now be hosted in windows apps, WCF can be used as interprocess communication between two applications.

Windows Services make an ideal WCF hosting platform because it can run without requiring a user logon. This type of arrangement solves the classic problem of notifying clients from a Windows service (remember Windows Services in this day and age shouldn't interact with the desktop). So say I have a monitoring app that monitors some process and if a failure occurs needs to send a notification to users logged on to a machine. The design would be a two component design where the monitoring functionality would be contained within the windows service. The UI component (tray notification) would be started when a user logs on and WCF web service would be used to communicate between the windows service and each UI component. Along with providing the monitoring functionality, the Windows Service would also host the WCF service that communicates with the UI components. Typically a duplex interface would be used so the Window Service hosting the WCF would fire notification events that the UI component would respond to.

I mentioned that WCF services can be hosted in a console application. In my opinion, this is only useful for test applications but not desireable for production. Sure you can do it, but it's pretty fragile (in that someone can accidently close the console window and take down the WCF service).

WCF offers additional options over the traditional ASMX approach including transport and security options. My recommendation if you are starting to write a web service is to use WCF instead of the older approach.

In my work, I typcially favor hosting WCF inside a Windows Service. I like it so much I have created a generic windows service that hosts any WCF services found within the config file.

asafaa
June 29th, 2009, 02:28 AM
WOW! These are professional Answers!

Thats dannystommen and arjay.

Arjay, so if i understand you right, A Window service can't interact with the GUI so i create a WCF web service that the Window service host it.
Do i create the window service in the "usual way"? or there is some kind of new WCF Window service?

Can i use the WCF service as a stand alone web service in the future? or i create a special WCF service for the specific window service hosting?

Arjay
June 29th, 2009, 02:50 AM
Arjay, so if i understand you right, A Window service can't interact with the GUI so i create a WCF web service that the Window service host it.The fact that the Windows Service doesn't interact with a UI isn't really important for hosting a WCF service. I mentioned an example of this only to show that you can have a Windows service do some work and use a WCF service to communicate with some UI clients.

Do i create the window service in the "usual way"? or there is some kind of new WCF Window service?No, it's a standard Windows Service. Inside it, you use the ServiceHost class to host the WCF service.


Can i use the WCF service as a stand alone web service in the future? or i create a special WCF service for the specific window service hosting?You create the WCF service as you normally do. Later you can still host it in IIS (or a Windows Service, or a Windows app, etc.).

asafaa
June 29th, 2009, 03:02 AM
Great! Thanks!

denizkartan
November 1st, 2009, 03:50 PM
Arjay's reply we very informative, but I can't clearly figure out a design solution for my problem:

I have a windows forms application which has to do some work when requested by a remote computer on a network.
For example;
a WinForms application, program.exe, is running on computer A.
program.exe has a variable named 'variable1' which needs to be changed on run time.
Note that program.exe has methods, i.e. SetVariable1(int val), which are readily used by its GUI.
The requirements are:
1. On computer B another WinForms application, say programClient.exe, should be able to change variable1 (either by calling SetVariable1 or by any method you would suggest).
2. A web page is hosted on Computer A say webPage1. A browser is viewing webPage1 and it should be able to change variable1 from webPage1.

WCF seems to be the solution for this task according to what Arjay says however as I said in the beginning I am not able to see clearly how WCF can be used in this task.

Arjay
November 1st, 2009, 07:50 PM
This shouldn't be too difficult. See my reply in this post, http://www.codeguru.com/forum/showthread.php?t=487121, for an example of using WCF to interact between two instances of a console application.

That code should be a good starting point.