How to replace rotation with a WCF subscription

Source: Internet
Author: User

Because some people in certain positions do not know is inconvenient or what the reason, so casually made a separate from all systems of the mail approval service, the function is that those people in the mail to the approval of documents issued a "consent" on the automatic approval, Roughly divided into 3 parts: the first part of every fixed time to the mail server to catch a group of mail down; the second part analyzes the message format, if it conforms to extract the necessary mail content; The third part submits approval flow driver for approval.

I've always wanted to be a mobile app and waste it, but it seems that the leader thinks it's going to last, but it's impossible to kill 1:30.

So, the game still has to be optimized, here is to say the first part:

Crawl at every fixed time and then execute the existing problem, for example, now every 10 minutes to grasp, processing is not timely, and even if there is no new mail will be caught once, there is a hidden problem, is why set 10 minutes, mainly mail server there are other processing, need a receipt, But this is a single-threaded service (because of the very few people) so worry about setting the time is short of this batch of crawling has not finished processing, here are some unrelated things are coupled.

Solution: No longer to grasp the mail, but if the mail approval service is idle, go to the mail server to register, if there is a new mail, the mail server to publish the task, so that more people can do distributed processing later (of course, I still tend to do not use this way, Because a variety of clients sent out of strange mail, parsing can not be correct.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

This is done using a WCF subscription release, but I think that some message queues can be simplified in this way without any pressure

Here is the test code:

First, the configuration file

<service name= "Hotelservice.publishservice" >
        <endpoint address= "" binding= "wsdualhttpbinding" contract= "Hotelservice.ipublishservice"/> <endpoint address= "Mex binding="
        mexhttpbinding "contract=" IMetadataExchange "/>
      </service>

Service and callback contracts:

[ServiceContract (callbackcontract = typeof (Isubscribecallback), Namespace = "http://www.justonlyatest.com")]
    Public interface Ipublishservice
    {
        [OperationContract (IsOneWay = true)]
        void DoWork ();
    
        [OperationContract (IsOneWay = true)]
        void Subscribe (string id);
    
        [OperationContract (IsOneWay = true)]
        void unsubscribe (string id);
    
    Public interface Isubscribecallback
    {
        [operationcontract]//(IsOneWay = true)
        void Callbackwork ( String workstate);
    

Service implementation, the comments are simple to explain the effect of the following instance model

Instancecontextmode.single synchronously notifies all subscribed clients that the service can be used as a client of the version server, synchronizing the version information of the distributed service//Instancecontextmode.persession  Synchronize all subscriptions under the same session, in this case all subscriptions//Instancecontextmode.percall for a single client instance cannot implement subscriptions//ConcurrencyMode.Single because all requests are standalone service instances Callbacks must be IsOneWay//callbacks are isoneway when all subscribers are synchronized, otherwise they can be notified sequentially only [ServiceBehavior (ConcurrencyMode = concurrencymode.multiple, Ins Tancecontextmode = instancecontextmode.single)] public class Publishservice:ipublishservice {Subscriber
        s subscribers = new subscribers ();
            public void DoWork () {String workstate = "Completed";
            Isubscribecallback callback = operationcontext.current.getcallbackchannel<isubscribecallback> (); Callback.
    
            Callbackwork (workstate); dictionary<string, isubscribecallback> subscribes = Subscribers.
            Subscribes; foreach (Var key in subscribes). Keys) {Subscribes[key].
            Callbackwork (key + ":" + workstate);
  }      public void Subscribe (string id) {Isubscribecallback callback = OperationContext .
            Current.getcallbackchannel<isubscribecallback> (); Subscribers.
        Addsubscriber (ID, callback); public void unsubscribe (string id) {Isubscribecallback callback = Operationcontext.cur Rent.
            Getcallbackchannel<isubscribecallback> (); Subscribers.
        Removesubscriber (ID, callback); }
    }
Public class subscribers
    {public
        dictionary<string, isubscribecallback> subscribes {get; set;}
    
        Public subscribers ()
        {
            subscribes = new dictionary<string, isubscribecallback> ();
        }
    
        public void Addsubscriber (String Id,isubscribecallback callback)
        {
            if (! Subscribes.Keys.Contains (ID))
            {
                subscribes.add (ID, callback);
            }
        }
    
        public void Removesubscriber (string ID, Isubscribecallback callback)
        {
            if (Subscribes.Keys.Contains (ID))
            {
                subscribes.remove (ID);
            }
        }
    }

Client Testing

Publishservice.publishserviceclient client;
        string ClientID;
    
        Public Testsubscribe ()
        {
            InstanceContext context = new InstanceContext (new Callbacksubscribe ());
            Client = new Publishservice.publishserviceclient (context);
    
            ClientID = Guid.NewGuid (). ToString ();
        }
    
        private void btnTest_Click (object sender, EventArgs e)
        {
            client. DoWork ();
        }
    
        private void Btnregist_click (object sender, EventArgs e)
        {
            client. Subscribe (ClientID);
        }
    
        private void Btncancellation_click (object sender, EventArgs e)
        {
            client. Unsubscribe (ClientID);
        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.