tags:

views:

62

answers:

4

I'm currently learning wcf for an up and coming project. The service I am creating is using MSMQ to update the database, but the database can't accept messages at certain times.

The service is going to be a windows service. The one thing I am coming up against at the moment is how I can get the service to stop reading messages from the queue at these times, for instance lets say I don't want to read messages from the queue on sundays. How would I go about implementing this. So that the client can send messages to the queue that update the database but the service doesn't read the messages until monday, so that the database gets all the updates on the monday?

I have started looking at creating a customhost, but I'm not sure if I'm heading in the right direction with this.

Thanks in advance.

A: 

This is really nothing that WCF can help you with - this is your app's logic.

If you don't want to accept any messages on Sunday - turn off your Windows Service on Sundays. You can use a scheduled task to do this, e.g. turn it off on Saturday 23:59:59 and turning it back on on Monday morning or something.

Or handle it in your code - if it's Sunday, do not process any messages.

This really has nothing to do with WCF, I'd say. There's nothing in WCF that allows you to "disable" or hide a WCF service for a given period of time.

marc_s
Sorry for not being clearer but the issue I am struggling with is how to stop the service processing messages in code, without using a scheduled task. I'm sure this should be easy to do, but I'm new to the whole WCF stack.
Miker169
@Miker169: so what exactly is it you want to do? Just stop the WCF service and not accept any messages for a period of time?? Or do you want to accept the messages into MSMQ, but not process them from the queue?
marc_s
I want to accept the messages into MSMQ but not process from the queue until , the monday.
Miker169
@Miker169: well, in that case - once again - the easiest would be to just turn your NT Service hosting the WCF service off during that period of time. Just do a `NET STOP MyService` on Saturday, and a `NET START MyService` again on Monday. There's nothing in neither WCF nor Windows otherwise that makes this really easy.....
marc_s
@marc_s: Thanks for the advice it does seem the best route to take is to have it setup as a scheduled task. In reality automation was something I would prefer. I gave sunday as an example in reality, there is a period of 3days, 3 times a month where updates can't be done to the database. There is around 15/16 services , that wouldn't be able to process messages from their queues. I was planning on having a seperate service tell the 15/16 services times when they can't process messages from their respective queues. The dates when this happens also changes from month to month.
Miker169
+1  A: 

You can write something like this.

If Date.Now.DayOfWeek = DayOfWeek.Sunday Then
 <Store it in a List(Of <MyClass>) > 
ElseIf Not <List(Of <MyClass>)> is Nothing Then
 <Call update Db function passing <List(Of <MyClass>)> and clear <List(Of <MyClass>)>)
End If

So if it's sunday, you'll just store messages and if it's not you'll update DB with stored messages and clear that storage.

hgulyan
Ok, but I want to keep the messages in the queue ideally. Is there a way to dynamically change the endpoint a service is listening to at runtime, so that messages that are in the queue just stay there until monday?
Miker169
No, as Marc said, there's nothing in WCF that allows hiding or disabling of a WCF Service. You should do it by yourself in your code.
hgulyan
A: 

I'm going to basically repost @hgulyan's answer with the logic I believe you want:

If Date.Now.DayOfWeek = DayOfWeek.Sunday Then
 ' Do nothing, it is Sunday and I don't want to process anything. 
Else
 ' Call function that processes messages in MSMQ
End If

This way your service can always run, but if it is Sunday... it does nothing.

Clarence Klopfstein
A: 

To finally get a solution that worked I rolled my own IInputChannel (which overrided the WaitForMessage and BeginWaitForMessage methods so that they returned False on say a Sunday) this is so that the channel couldn't receive any messages, I also created a new channel Listener to implement the custom channel, and then created a new bindingelement for the channel listener.

I had to change a few things like the time to live for messages and also the default timeout for the the channel listener.

The basics of the service are up and I now have a working concept to go forward with.

Miker169