Start/stop self-hosted WCF data service programmatically - c#

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.

Related

How to start my Application on Server startup

I have a Windows Forms Application which runs on a server. I need this Application to always start automatically. Even if the Server just gets restartet and nobody logs into it the Application should run.
So the solutions with Registry don´t work here. I than read into Windows Services but it seems like I can´t start a WinForm Application with it.
Does anyone have an idea how I can achieve this automatic Start on Server startup?
The way we do things like that is that we create a Windows Service which runs without the need to have anyone logged in, and then if there is a need we have a separate GUI application (WinForms in your case) which interacts with the service, when needed.
The communication between the GUI application and the Windows Service is usually done by means of named pipes, but if you can get away with something simpler, like the GUI application saving a configuration file for the service to pick up, you might make it easier for you.

It is possible to add Windows Service to an Existing project?

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.

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

.NET Service when finished or just before finishing start another .NET service?

I've been given 2 .net services. 1 needs to run right after the other. Ideally I'd merge the two services into 1, but given that this is a stopgap measure for only another month or two before its not needed any more I'd prefer not to have to do that much modification and testing on a large change. I was wondering how I could tell service A to start service B after A is basically complete?
ServiceController Class
Represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it.

Categories

Resources