Asynchronous communication with WCF - wcf

I am developing a generic logging object which will be used within all of our future applications. It will submit a log to MSMQ, which will then asynchronously send it off to our server that will log that message to a database.
Currently I am trying to understand the architecture of how this will work. On the client side, once a log is submitted to MSMQ, will MSMQ then submit the log to WCF to send off to the server (which I assume will have another WCF endpoint receiving the messages)? Basically, I am asking what is the order of services that the log will travel through? I have read about netMsmqBinding for WCF, is this what I will need in order to send a log from MSMQ to WCF, and then I can use a basicHttpBinding to send it from WCF to WCF on the server side?
Something like:
[Client application] -> Logger -> MSMQ -> WCF ----------> [Server] WCF -> DB

WCF has netMsmqBinding that can handle both client and server messaging. If you use it MSMQ will be almost invisible to you. You will send message to WCF service, it will be put to MSMQ and server-side WCF will pick it and invoke method like with any other binding.
If you have any experience in creating WCF service you should do the same but also create MSMQ Queue.
Here are useful links: http://sukasom.wordpress.com/2008/08/18/wcf-and-msmq-part-1/, http://msdn.microsoft.com/en-us/library/ms752217.aspx

Related

Can WCF duplex service be used to call client when client is offline?

Is it possible a service to call client after 4 -5 days when client is offline? e.g.
1. The client request some reports through service.
2. Service updates database with client request.
3. Offline work is done on the request
4. Report is uploaded to the database.
Can we service call its client and send report as soon as report is uploaded to database?
Can WCF duplex service be used to call client when client is offline?
Yes. WCF can be configured to use MSMQ as a transport. MSMQ is the only WCF transport that allows for all three:
disconnected scenarios
resume when computer becomes online and
optionally provide a level of guaranteed delivery
MSDN:
If you need to support disconnected queuing, use netMsmqBinding. Queuing is provided by using Microsoft Message Queuing (MSMQ) as a transport, which enables support for disconnected operations, failure isolation, and load leveling. more...
Essentially you invoke a WCF method (send a MSMQ message) and it will be delivered when the computer comes on-line again. Assuming you have set the appropriate expiration options.

MSMQ to WCF - automatic notification

I have an application that places messages on MSMQ, than I have a WCF service that I want to automatically read these messages from MSMQ.
I do not want to trigger my WCF service but I want it to be notified whenever a message is pushed on MSMQ. I have found some stuff related to WAS and netMSMQBinding, but the details seems a little unclear about automatic notifications from MSMQ to WCF.
Any help/direction in this regard? Thanks.
What is it that you're trying to achieve?
There is really no such thing as "be notified when a message is pushed on MSMQ". MSMQ uses a pull model to retrieve messages from a queue, so there always needs to exist some kind of "listener" that reads messages from the queue and processes them.
In this case, this "listener" is provided by the WCF NetMsmq or MsmqIntegration bindings automatically, so the programming model for a WCF service that is exposed over MSMQ is just like that of a regular one-way service.
As you state, you can either host that WCF service using your own custom host (such as a Windows service), or, in many cases, the preferred method will be to host that service in IIS 7.X/8.X through WAS.
The following sample shows how to host an MSMQ service in WAS: http://msdn.microsoft.com/en-us/library/ms752246(v=vs.110).aspx
Notice that you do need some specific configuration in IIS to be able to host non-HTTP services, which includes enabling the "Non-HTTP Service Activation" feature. The following articles will be useful to get this done:
http://blogs.msdn.com/b/tomholl/archive/2008/07/12/msmq-wcf-and-iis-getting-them-to-play-nice-part-1.aspx
http://blogs.msdn.com/b/tomholl/archive/2008/07/13/msmq-wcf-and-iis-getting-them-to-play-nice-part-2.aspx
http://blogs.msdn.com/b/tomholl/archive/2008/07/14/msmq-wcf-and-iis-getting-them-to-play-nice-part-3.aspx
http://blogs.msdn.com/b/tomholl/archive/2008/05/17/building-a-pub-sub-message-bus-with-wcf-and-msmq.aspx

queuing requests with WCF service

I have a WCF service that can receive several requests/minute (or seconds) that need to write information to the database. Rather than write it synchronously, I would like to place these requests in some sort of a queue on the server so that another proces can come along and process them. The client just needs an acknowledgement that the request was received. I have read a lot about MSMQ and WCF etc, but it seems that with MSMQ you write to the queue from client and not to the web service, which is not what I want.
Is there a way to do the following inside a WCF method that does not involve a database. Perhaps i have not grasped the concept of MSMQ right.
public bool ProcessMessage(string message)
{
if(IsValid(message))
return AddToQueue(message);
return false;
}
EDIT: I need to validate the message before writing to the queue.
I do this currently in an application I created. A WCF service is hosted as an HTTP Service on IIS. It accepts calls, and packets of data, I take that data, validate it (tell the caller it's wrong or not) then send the data to another WCF service that is using netMSMQ binding, that service then does the final writing to the database. The good thing about this is it will queue up on one MSMQ and the WCF Service that is bound to this MSMQ pops off one message at a time and processes it. The HTTP WCF service can then handle as many requests as it wants and does not have to worry about pooled up messages as that's the job of the WCF/MSMQ-bound service. The common name for this pattern is a Bridge framework.
ETA: the second service (the MSMQ-bound WCF Service) is run as a Windows service always on. It also handles separation of concerns. The HTTP service validates and does not care about the database, the other service handles writing to the Database.
The point of using MSMQ should be to remove the need for your service to worry about queueing anything. MSMQ will guarantee that your messages get delivered in the proper order and that your service processes them in the proper order.
Your service shouldn't maintain a queue at all if you set this up properly.

How to update datat in clients from WCF service after updating data on server?

When one of clients changes some data on server I need to send a message from WCF service to all clients that makes them to download changed data. How can I make that?
You are looking for Publish-subscribe message exchange pattern where all client first have to register on the service (subscribe) when service receives new data it sends them to all other clients (publish).
WCF support this when using duplex communication - Net.Tcp or WSDualHttpBinding. You can check complex artice by Juval Lowy in MSDN magazine.

Using Windows Services to process MSMQ messages via WCF

We have a solution where we are picking the messages using Windows Service.
The Windows Service fires every after 2 minutes and retrieves the MSMQ message to pass it to a Web Service.
Can I create a WCF service which will automatically picks up the messages from MSMQ Queue?
Can I avoid Windows Service by using WCF Service if it support auto invocation?
Q1: you can automatically pick up messages from MSMQ, you will need to look into the netmsmqbinding, there are some design considerations that you have to think about though, if you are used to the native MSMQ, you know that you have the ability to peek at the messages. But when you use WCF, you loose that ability to peek. WCF will intercept the messages in MSMQ and you are responsible for keeping your WCF service and the peeking app in synch.
You will also need to look into whether you need transactional or non-transactional queues and you will have to modify your binding based on that.
Q2: You will need to host the WCF service in windows service or in IIS7. if you host in IIS7 look into enabling MSMQ WAS listener
Here is a nice article:
http://blogs.msdn.com/tomholl/archive/2008/07/12/msmq-wcf-and-iis-getting-them-to-play-nice-part-1.aspx
One way to transfer messages from an MSMQ to a web service call is to use a netMsmqBinding service endpoint and a basicHttpBinding client endpoint that support the same contract. The netMsmq service will automatically grab messages from the queue and deserialize them into an object. In your implementation of your netMsmq service, just instantiate your basicHttp client proxy and just call the same method. Basically a pass-through or proxy pattern from the web-service to the MSMQ and vice-versa. In Juval Lowy's "Programming WCF" he calls this pattern the "HTTP Bridge" for queues.