Hangfire: Check the status of Windows service - hangfire

We have created hangfire background job as the windows service.
Sometimes what happens is the windows service goes down with some reason or the other as its a shared server.
I wanted to know if there is any way to check the state of the windows service from hangfire to see if the service is running fine or down. So that we can raise alerts that the service is down. Or if there is any possibility to change the status when the service goes down.
But issue is how to get the state of the windows service here.
Would appreciate inputs.

Related

IIS Restart Web API in ordered

When IIS Server is the outage, the server is restarted and all applications and web APIs are restarted as well, the problem I'm facing that some Web API depends on BUS. Could we let site/app wait until BUS ready before starting from IIS without touching the application code? In Docker, we can use the WAIT command or other third parties to wait until service is available before starting a container.
The Web API we are built on .Net Core 3.1
Any help is appreciated.
During the startup could you use something like
https://github.com/App-vNext/Polly to enter a period of retry
(https://github.com/App-vNext/Polly#retry). This would allow you to
retry the call to the API until it's hopefully it's available.
Use a windows service to monitor heartbeats of the applications and trigger application pool restarts on applications not working correctly. This should help you get into a running state.
Ultimately I'd try and remove this dependency, if you could give a little more information around the webapi requirements I'd be happy to suggest more ideas.

How to force startup of WCF service

I have an IIS hosted WCF service, and a client Windows application which, upon the first use of the day, takes a while to respond to the first service call. I believe this to be because IIS shuts down services which are not used for a period of time (and the delay is the restart time for the service). I was wondering whether I could alleviate this by making an asynch call when my application starts up (just to, potentially, get the service to start). I therefore, upon application start, created a "fire and forget" background thread which just opens a connection to the service. The intention being that when my application has finished its own startup, and wants to use the WCF service, the thread will (probably) have finished and the service startup delay will not be encountered by the user.
Is this reasonable ? Is opening a channel to the WCF service enough, on its own, to start the service, or do I need to write some dummy method and call that ?
Thanks
Ross
Check if you really want your app to handle this task and checkout the AppWarm-Up Module for IIS, maybe you can use this without adding code to your serivce.
Is this reasonable ?
It doesn't sound like a good approach to me. If you have control over how the service is hosted I would advise you to self-host it.

Windows Services & WCF

I need to create a Windows Service to watch a folder on our network and action files that are placed within it. The process is quite slow and I need the ability to check the progress from a client application (which will be running in about 10 places on the same network as the machine running the Windows service).
Is hosting some WCF service in the windows service the right way to go about this and if so, are there any resources on how I would do this?
Thanks!
it seems a reasonable approach to me.
you can get details of how to host a WCF service inside a windows service in the MSDN how to
This code project page also has an example.
you might need to debug start up issues with the service, and I find adding a
Debugger.Launch();
to the beginning of the OnStart method is the easiest way of doing that. it enables you to debug through the start up process of your service and see any exceptions that occur.

What services are required for AppFabric to run properly and monitor WCF Services

I'm running AppFabric on IIS7 with Windows 7 for development.
AppFabric works fine for some period of time, but then it will stop updating.
I can send service requests through, and AppFabric doesn't show them in the dashboard when I refresh it.
I think a service is stopping, or there's a permission issue. Does anyone know what services are required to for AppFabric to run properly?
SQL Server and AppFabric Event Collection Service, off the top of my head (for event collection; there are other services, but since you say AppFabric itself runs requests, I presume they're OK). There's also Event Tracing for Windows, but it's not, strictly speaking, a service.
I presume you've also checked the tracking profile is set correctly and have looked in the Application event log?

Windows service connecting to other service over wcf crashes

I have two windows services. One ('server') acts as a WCF host to which the other ('client') connects. So I have configured a dependency from client to server. Both are also set up to start automatically.
When I start these services by hand, everything works fine. When I stop both services and tell client to start, then server will be started before client and all is fine.
However, when I reboot the machine only server is started.
When I add a diagnostic listener I see it got a TimeoutException with the helpful message:
The HTTP request to 'http://[server address]' has exceeded the allotted timeout of 00:00:00. The time allotted to this operation may have been a portion of a longer timeout.
At some other SO question there was an answer that claims WCF is probably confused about what went wrong and therefore starts lying about the timeout.
Did I perhaps miss a dependency for either service? Does WCF require something that hasn't or is being started when client is trying to contact server?
I think you should check your client service. On startup windows services are starting while network devices are still being initialized. Services should be ready to start without network and without any network device. Usual approach is to keep periodic retries to establish connection. You can do little experiment on your machine by uninstalling all network adapters and trying to start up your services.
Additional quick workaround you can do is to setup recovery options on your service -- for example you can configure it to restart service on crash after some timeout -- you can do this through UI in services.msc or in command line using 'sc config' command.
Configuring the dependency between the two Windows Services is not necessarily sufficient to avoid there being a race condition: i.e. to avoid the client service calling the WCF service before the server's WCF channel stack is fully initialised.
The service dependency just ensures that the Windows Service Control Manager won't start the client service process before the server Windows Service has notified the SCM that it has started. Whether this is sufficient depends on how you write the server.
If the server service starts a new thread on which to initialize the WCF stack, your OnStart method is probably returning before the WCF stack is ready for clients. There is then a race condition as to whether the client's first call will succeed.
On the other hand, if the server service does not return from OnStart (and thus doesn't notify the SCM that it has started) until the channel stack is fully open, the dependency removes the race condition, but there is a different pitfall: you need to beware that the SCM's own timeout for starting the Windows service is not triggered while waiting for the WCF stack to initialise, as might well happen on a reboot if the WCF service depends on the network stack, for example. If the server's OnStart does not return within the SCM's timeout, the SCM will not try to start the dependent client service at all, because it does not receive the server's start notification. (There will be a message in the Windows event log from the SCM saying that the server service didn't start within the expected time.) You can extend the SCM timeout by calling ServiceBase.RequestAdditionalTime while the WCF service is being initialised.
Either way, the client service really ought to be written so that it doesn't fail completely if the first WCF call doesn't succeed.
You don't actually say what binding you are using. If client and server services are always running on the same machine, as you seem to indicate, then consider using the NetNamedPipeBinding: then your service won't be dependent on initialization of networking resources and startup should be quicker.