开发者

How to make a service not receive messages at certain times

I'm currently learning wcf for an up and coming project. The开发者_如何学C 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.


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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜