What is the best way to handle any faults that may occur for a WCF service? So apart from try/catch in the service/WCF itself, what if the client faults for example the system went down (i.e MSMQ went down on a cluster or something) - things like this will cause WCF service host to fault.
How can I restart the service safely after a period of x seconds? I tried doing this but even when I create a new ServiceHost after Abort() when I have entered the Faulted state, I always get an error saying that the communication channel has faulted or is closed.
What can you recommend as a good solution to restart the service host app if it faults, and to successfully re-establish that host after it being faulted?
Try to implement WCF service as Windows service. In this case when the server restarts you host will restarted too. You should implement Windows Service class inherits from ServiceBase and then override OnStart and OnStop methods.
Related
We are trying to host a send-only endpoint within a WCF service. Due to its fire-and-forget nature, the WCF hosting option here isn't really what we are looking for, since we don't want to wait for the messagehandler to have to reply. In other words, all our WCf methods are void/Task.
However, we are having trouble figuring out how to deal with properly shutting down the endpoint. WCF doesn't really have a lifecycle API to use to handle shutdown behaviour.
So my questions are as follows:
Do send-only endpoints need to be shutdown?
If so, how would I do this in a WCF service?
You don't 'need' to shut down a send-only endpoint. It is more critical for processing endpoints to shutdown gracefully, as there may be messages in-flight being processed.
Is it a good idea to initialize a timer to do a periodic task in a WCF service host or will that mess with the lifecycle or performance of the service being hosted?
I have a custom service host that announces it's availability (with the goal of creating a registry for clients) upon starting and stopping. I want to create a timer to do this periodically but my concern is that this will cause problems (I haven't ran into any yet but maybe I haven't stressed it enough or something) or maybe it will eat resources and kill the performance of the service. The idea is to hookup the timer on the OnOpened and OnClosing events, of course.
For what is worth, this service is hosted in IIS so it is IIS the one instantiating and managing the service host.
Is it a good idea to initialize a timer to do a periodic task in a WCF
service host or will that mess with the lifecycle or performance of
the service being hosted?
That will depend on where your WCF service is hosted. If it is hosted inside IIS it might be problematic. The reason for this is because IIS can decide to recycle the ASP.NET application pool at any time and your timer will simply stop working. It is not something you can rely upon. If on the other hand you have hosted your WCF service inside a Windows Service self host, it is fine to use a timer. You may take a look at the following blog post about the dangers of implementing recurring background tasks in ASP.NET hosts.
I have a windows services which uses EasyNetQ and RabbitMQ.
The service starts normally from the service control manager.
However I have seen occasionally on a reboot, the service does not start with the error in the services event log :
A timeout was reached (30000 milliseconds)
The <serviceName> service failed to start due to the following error:
The service did not respond to the start or control request in a timely fashion.
I have tried auto delaying the service and this has not helped.
In addition, I was thinking about setting the recovery mechanism so that if it does not start it restarts on first/second and subsequent failures. Not sure if this will work.
So my questions is how can I determine what the dependency is that is causing my service not to start sometimes?
In order to determine what dependency is causing the error you could try to attach a handler on TopShelf "OnException" (https://topshelf.readthedocs.io/en/latest/configuration/config_api.html#onexception)
and log the exception that caused the error.
I am having trouble getting a Windows Store App to make calls into a WCF service.
The service is a Duplex service using a netTCP binding. The first time the client (A Windows 8.1 Store Application) uses the service, it throws an exception:
An exception of type 'System.ServiceModel.CommunicationException'
occurred in mscorlib.dll but was not handled in user code
Additional information: The socket connection was aborted. This could
be caused by an error processing your message or a receive timeout
being exceeded by the remote host, or an underlying network resource
issue. Local socket timeout was '00:09:59.9968452'.
This timeout is near-equal to my max, 10 minutes. The exception, however, happens immediately, and breakpoints in the service function are never hit. The two do seem to be talking at some level because altering the security protocol or the endpoint address cause other exceptions (security and connection as you would expect). I put a breakpoint in the service and the function I am trying to call never gets hit.
I have tried:
Ensure feature equivalence between Service and Client NetTcpBinding configurations
Raise timeouts, sizes (1-10 minutes for each, 10000000 for max sizes)
Ensure all passed object types are DataContracts with default constructors
Prayer; Considering a burnt offering
Any help would be greatly appreciated. New to WCF and having trouble finding help for the Windows Store / netTCP / Duplex targeted scenario.
If your Windows Store App client and WCF service are on the same computer and you're not running the Windows Store App client from inside Visual Studio, then you need to enable loopback communication. Check out this article:
http://msdn.microsoft.com/en-us/library/windows/apps/dn640582.aspx
At the very bottom it talks about the command line utility:
checknetisolation
Also, try making your method call as simple as possible with no return and no arguments. I know you said you checked all of the 'DataContract' attributes. But things can get tricky such as if you're using polymorphism and the base class doesn't have a 'KnownType' for a derived.
I have a iPad which is using WCF service.
The WCF service is loading some background data for every 10 seconds.
Sometimes background loading process fail and all the data and service methods become dysfunctional.
How can I provide a 'kill switch' to take every mobile service method down and provide a brief message (or send a mail) automatically when being called?
Is it possible to implement in the service setting or should I do something else?
If you find your WCF service is in an bad state, you can call HttpRuntime.UnloadAppDomain. See also Recycling WCF Web Service on IIS