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.
Related
I have developed a .NET UI and windows service which communicate with each other using WCF through the NetNamedPipeBinding protocol. I chose NetNamedPipe because it is the best and efficient choice for interprocess communication on the same machine.
However, one of the key requirements of the application is that the UI should be able to send actions to the windows service and vice versa asynchronously. This means that the windows service should be able to call back the .NET UI application without the need for the .NET UI application to connect to the windows service first.
Keeping this in mind, I have hosted a WCF service in the windows service and a separate WCF service in the .NET UI application so that they can communicate asynchronously without the need to keep the communication channel always alive.
I would like to know if this makes good design sense.
I would appreciate your comments on the same.
Thanks in advance.
Subbu
Hosting another WCF service in the client to receive calls from the main WCF service is not a good idea. You need to use WCF duplex communication (as Subbu posted) aka WCF callback.
WCF: Duplex Operations and UI Threads
The problem with this approach is that your service needs to connect to the client as a host.
A better option would probably be to setup a WCF Callback when you first startup the client. The service can then use the callback for service to client notifications. For details, see this MSDN article which shows using WCF callbacks.
WCF Callback allows server to drive events to the client. See a short and simple example here: A simple WCF Callback example
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.
I am running WCF self hosting server. Connection is coming fine and I can process them.
But now for long connections we decided to break a connection, process request and send results back to self hosting server on client side.
How do I know which client should I send a request back? DO I need remember every client address? How do I know their address, should they provide back address as input parameter? Or maybe there are much easier, correct and elegant way to solve this common issue?
As cillierscharl already said, you could use callback contracts.
Links to give you a quick start:
WCF Essentials - What You Need To Know About One-Way Calls, Callbacks, And Events
WCF Callbacks – A quick introduction
Simple Example from Stack Overflow
Chat Example
WCF Duplex Reentrant Services
Best (beginners) article about WCF Callbacks
Stack Overflow: keeping a wcf callback channel open indefinitely / reconnecting from client if it faults
Synchronization Contexts in WCF
Advanced Stuff and things you should worry about:
Detecting Client disconnection
Reestablish Connection Management
Concurrency Mode and Instancing (Threading..) / Synchronization Contexts
We have a number of Windows services running in our system (built in C#). We use WCF to communicate with them and control them, since WCF offers very convenient communication with these processes.
Right now in our Windows GUI for managing, monitoring and troubleshooting the services, we simply register callbacks and receive notifications when a message is available from the service. Obviously this application is stateful and WCF provides the ability for the local delegate to be called when the maintained connection to the service indicates.
In our web application which users actually use, we'd like to use long-polling to have a status area on the web page (iframe, AJAX, whatever) which shows any issues which the services are reporting. We'd like to use a long-polling or other technique which minimizes actual polling on the network.
The problem we are running up against is that we need something to make the long-polling HTTP request against which will somehow always be running in IIS and which itself can be WCF-connected to our services and which can convert the event/delegate-based WCF response into a blocking-style long-poll response. It feels like a chicken-and-egg situation that some component in our system is always going to be in a loop, polling - and that's exactly what we are trying to avoid.
Does anyone have an example of doing this?
Well, if your services present with WCF, why not simply consume the WCF services with javsacript? Then you remove your IIS servers from the equation completely. if a user wants to see what the services are doing then they can retrieve the information directly from the service.
Here's a blog with someone showing how to do this:Call wcf service from Json
I have a duplex WCF service that relies on a consistent named pipe connection between the service and the client. It's sort of a publish/subscribe system where the client calls Subscribe on the service and gets put in a subscription list. Then, the service calls certain update methods of its own, which will push the update to the client(s) via callbacks.
I've set the recieveTimeout for the netnamedpipebinding to "infinite." Can I reasonably rely on this connection to be open forever? More importantly, are there situations where the channel will fault outside of timeouts?
Since a named pipe is just a shared memory location, I can't think of many reasons why it would fail consistently besides hardware problems. Furthermore, there isn't much of a way of guaranteeing the connection outside of pinging at a certain interval.
On a side note, my gut feel is to avoid also making the client a WCF service. I know it's not a real circular dependency, but it just feels icky. However, I'm open to people telling me I'm just being paranoid and that that kind of pattern is a-okay.
You can never rely on the connection. Single unhandled exception / fault on the service will close the channel. I think you should handle Closed and Faulted events on client or implement pinging mechanism with proxy recreation and resubscribtion.
When using duplex communication you simply need the client to expose contract - it is not the same as exposing the service because client do not expose an endpoint. The communication is performed by single channel created from client to service because Named pipes are duplex by design. Every time you will want to have duplex communication over some transport you will need some handling code on the client.