Web Service to Window Service communication

I have a C# windows service running on a local machine, and a webservice running elsewhere (may or maynot be local). Is it possible to pass data back and forth between the webservice and the windows service? I've looked into using the TcpChannel remoting stuff, but there aren't any really good examples.

Say for instance, I want to pass a string from a webservice to a windows service, and modify it within the windows service, then pass the modified string  back to the webservice, how would I go about that?

Who is Participating?

Improve company productivity with a Business Account.Sign Up

joechinaConnect With a Mentor Commented:
Look at the following two links, it should help:
A simple WCF sample:
And how to host WCF with windows service:

You can pass a reference to a remote serice.
The object has to be serialized and deserialized.
WCF hides a lot of low level serialization and communication and makes your life much easier.

TcpChannel would work but more effort.
Gary DavisDir Internet SvcsCommented:
Normally, the windows service would send requests to the web service. Sounds like you want to do it the other way.
smithdale87Author Commented:
Yes, that seems to be the case. Any ideas?
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

The name is "Service", meanin gthat the software provides a 'service', at the request of some other software.  SO, normally there is a client, of some kind, which issues a request to the service, which then responds.  Since you have two service, how is the initial request supposed to be issued?  The Windows Service normally does not initiate a request, nor does the Web Service.  How is the request to be initiated?

I am with Arthur Wood on this one. In order to use a Web Service, an application has to essentially "subscribe" to the web service by making a reference to that service. Thus the act of subscribing dictates that the subscriber is the client. So in your case, assuming that the Windows service subscribes to the Web Service, your Windows service becomes the client.

In order to exchange info with the Web Service, the Windows service would need to initiate the conversation by calling a Web Service method.

smithdale87Author Commented:
Makes sense. Thanks.

The reason I am asking is because one of my co-workers asked me if it was possible for me to develop a series of applications such that a Java client passed a string object to a web service, which then passed the string on to a windows service, which then calls a local c++ dll to modify the string. Of course everything has to be passed by reference rather than by value.

However, I'm guessing the only solution is that once the string reaches the web service, the windows service should be continuously polling the web service to see if the web service has received the string from the Java client yet. If so, get that string, and pass it along to the DLL.

Sound more feasible?
Be careful there. Each new web service instance runs in it's own session on the web server. You can create global variables to share between sessions, but that can get messy real quick.
Hi, smithdale87,

Why a windows service to do the job?
Could you just wrap the c++ dll with a web sercive? It will make things much easier. (or even a COM object.)
Your polling solution will work, but it's not a good solution.

If you just don't want to install IIS on that server, you can build a windows service which which host a WCF service.
Google "Host WCF by windows service" should give you some hints.
Good Luck
smithdale87Author Commented:

I'm not sure what the reason for the windows service is. Initially, my co-worker wanted java->web service->dll, but then at the last minute he threw in the windows service as an intermediate step. I assume it has some direct application to something he may be working on.
Gary DavisDir Internet SvcsCommented:
Actually, a web service interface to a windows service is a pretty good idea. However, the windows service should not periodically probe the web service. When the web service gets a request from the client, it should notify the windows service. Windows services are always looking for things to do so this would just be another input.

For example, let's say you wrote a print spooler service that took jobs to print from various local apps. A web service could control that windows service like get the list of print jobs on the queue or shut down the spooler. Then you have control from the internet to your windows service. There are many other examples.

I just don't know the best way for this IPC (interprocess communication from web service to windows service). Traditional IPC techniques are shared memory, named pipes, semaphores, message queues and of course TCP/IP sockets. I't sure there are other options.

Gary Davis
smithdale87Author Commented:
Finally, someone who actually sees things my way :-)

As far as the WebService to Windows Service ipc, i've looked into using C#'s TcpChannel class but it only seems to allow me to create a remote object that is hosted on the local machine ( ie where the windows service is running ). So theoretically, the remote object should have access to a local DLL. I could just create a new remote object from the webservice, which will then be hosted on the local machine. The problem is when I call the DLL from within the remote object, and pass a string by reference to the DLL to be modified, it doesn't seem to affect it. It appears that eveyrthing is done on the stack and just disappears when the call returns from the DLL.

However, with a primitive int, if I pass it by ref, and modify it inside the DLL, then the changes appear back on the java client side. Not the same with the string object. I'm not sure why. Any insight?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.