It is possible to add Windows Service to an Existing project? - c#

I've created a Windows Form Application in C# and I added a Window Service on it. The problem is every time I started the Service after installing it, I always get the Error 1053 the service did not respond to the start or control request. But after creating a new project and Select Windows Service and Installed and Run it there's no error and the Service is Starting correctly.
So do I have to create a Separate project for Windows Service or I am just missing something?
My target Framework is 4.5.2 and I am planning to have UDP and TCP function inside my Windows Service.

Calling ServiceBase.Run() from Main() is what makes an application a service rather than a normal application. If you created a project using the Windows Service template, but took away the call to ServiceBase.Run(), the result would be a normal application rather than a service. (Probably a broken application, but an application nonetheless.)
Under the hood, ServiceBase.Run() calls StartServiceCtrlDispatcher(), which calls the internal ServiceBase.ServiceMainCallback() function, which calls your OnStart() function. So if you don't call Run() there will be no call to OnStart() and your service won't do anything.
The StartServiceCtrlDispatcher() function is also indirectly responsible for calling OnStop() and all the other related methods. Basically, it's the core of the service, and without it nothing will work. Also, of course, if you don't call it Windows will eventually notice that the control dispatcher hasn't started, assume that the process has hung, and kill it. That's what error 1053 means.
While it is possible to incorporate both a service and an application in a single executable, it isn't trivial to get it working properly. It is also an unusual approach, not often used. Unless you have a compelling reason to avoid doing so, I'd recommend that you use a separate project for your service.

Related

Is it possible for a WCF service to restart itself?

I'm currently working on a WCF service which holds and processes all the data for an application, while a MySql database is used for persistence. The service currently works as a singleton (InstanceContextMode.Single) and supports multiple concurrent calls (ConcurrencyMode.Multiple). I'm not really sure what version of IIS the service is hosted in, but I believe it is IIS 7.5.
The problem is that there are some situations where if an exception occurs (eg.: while releasing ReaderWriterLockSlim locks), the service will be in a unreliable state and data may get corrupted (and written into the database) if users keep calling the service.
Currently I know of two ways of preventing users from calling the service: either closing the InstanceContext object (through OperationContext.Current) or raising an exception in IDispatchMessageInspector.AfterReceiveRequest if the service is in a faulted state. The problem with both of these two ways is that they make the service unavailable until I restart the server/application pool (which I can't, see note below) or re-deploy the service.
Important note: Although I have Full-Trust, the service is currently hosted on a shared server, so I can't restart the server or the entire application pool (if that is possible) because that would restart other people's services as well.
Update:
I tried unloading the AppDomain as #usr suggested, but that doesn't work as well: after unloading it, an exception is raised for every call to the service.
Currently I'm trying to find out what WCF/IIS uses as a condition to decide if the service should be created again. I noticed that in the code generated for the client checks if there is any channel available to communicate with the service; if there isn't, a new one is created. Thus, I tried to close all channels in the service: I tried closing OperationContext.Current.InstanceContext.OutgoingChannels, OperationContext.Current.InstanceContext.IncomingChannels, OperationContext.Current.Channel, and many other properties with "Channel" in their name, all of them with no success.
The way to warm-up anything in IIS prior to version 7.5 is using scheduled console application to ping your web site / services and warm them up. It's not a good fix but it works, it is easy and I saw it on every project which had to deal with this requirement.
Or If you are using IIS 7.5 then
You can use Windows Server AppFabric, it has Auto Stat feature to keep the service always on. But you need to be on IIS 7.5 to install App Fabric.

Workflow application in c#

I have a workflow application hosted (for now) in a Console Application, using WorkflowServiceHost to host an activity. This was based on a MS example, and works fine.
I'd like to add another 'WebMethod' to this application. This method has nothing to do with the workflow being hosted - it will simply be a 'ping' service so that clients can tell that the application is alive and running.
Is this even possible? If so, how would I go about adding this new method?
Thanks
Don't create a new method inside the Console Application as that would be poor practise (1 thing should do 1 job)
Instead create a new WCF project inside your solution.
Reference this project in your Console app and start the WCF host when the Console app initializes.
The WCF project can then be written to respond to 'ping' requests.
This is a handy primer in WCF
http://www.codeproject.com/Articles/406096/A-beginners-tutorial-for-understanding-Windows
Richard,
Thanks for your answer. I was able to add a new endpoint to the Console App but defining the service interface, and adding another host and starting it.
This is fine for the proof-of-concept code, but wouldn't be acceptable for release.
Dave

Start/stop self-hosted WCF data service programmatically

HI I have a solution with a couple of projects.
One of them is a self-hosted WCF Data service.
This service is running fine on his own, but I want to start it from my main method.
This way I can control wether is should start or stop.
Why I need to do this is that I can run my main method and let the service start, but when the program exits, the service is still running.
Anybody know how to fix my problem?
--EDIT--
This service thing is new to me. So I'm not even sure it's possible.
Let's say I have 2 projects. One contains my main method and the other is a WCF Service Application project.
I want to be able to stop my service from within the main project whenever I want to.
--EDIT2--
I am creating a system that can provide data to clients through an API (WCF Data services). When my system starts, I want to start the service. When my system shuts down, my service have to stop existing or at least providing data.

VB6 .dll in ASP.NET Web Service project

I got a strange problem recently. I have some VB6 .dll that I must use in my ASP.NET Web Service project. When I test the .dll in Console application - all works fine - I create an object and can use all methods as it should be. But when I start to use it in my Web Service I got a strange problem. When I'm creating an instance of dll class - it is created (after a long pause) but all properties instead of being nulls (as it happens when I test it in Console Application) in the debugger are set as "Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation". Something competely strange I've never seen before :) And at the same time in my client application (which actually invokes the service) I'm getting a connection reset.
Any suggestions?
If this DLL was created for a desktop environment, then it may simply not work in a server environment. It may depend on using a Windows message loop for synchronization, and may not be thread-safe at all (and it needs to be thread-safe in order to use it in a multithreaded server environment like ASMX).

How to make a C# application act as a service?

I have developed a C# program, although when I try and create a service from it e.g.
sc create "servicetest" binPath=
"C:\Users\Admin\Desktop\test\test.exe" start= auto error= ignore
I get the following message:
[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.
You need to create it as a service through the Visual Studio interface, which will provide you with the correct classes and methods you need to implement.
You need to base your application on the Windows Service template available in Visual Studio (not available in the Standard Edition.) See here:
How to: Create Windows Services
There is also another way of implementing windows service using TopShelf. You can actually run a console application as windows service using topshelf. Advantage of it is easy to debug. As far As I know when you want to debug windows service, you have start the service in service console. Stop the service if you are compiling and service installed from build output directly. It is extra painful step.
If you are using topshelf, you can start the service like how you start windows forms application to debug. There are other benefits too. Please refer the web site.

Categories

Resources