如何以五分钟的间隔调用Web服务?

时间:2016-05-29 11:56:06

标签: c# .net wcf

我有这段代码:

class ServiceConsumer
{
    static void Main(string[] args)
    {
         //call from here retriveEvents method evry 5 minutes!
    }


    private static void retriveEvents()
    {

        IContract proxy = ChannelFactory<IContract>.CreateChannel(new BasicHttpBinding(),
                                        new EndpointAddress("http://someAddress/Hydrant.svc"));

        using (proxy as IDisposable)
        {
            var rows = proxy.GetData(new DateTime(2000, 5, 1));
        }
    }
}

retriveEvents()方法是从主机服务创建代理和检索数据。 如何调用asyncroniusly retriveEvents()方法访问主机服务evry 5分钟?

2 个答案:

答案 0 :(得分:0)

static void Main(string[] args)
{
   System.Timers.Timer aTimer = new System.Timers.Timer();
   aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
   aTimer.Interval=360000;
   aTimer.Enabled=true;

   while(true) { }
}

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
     // Do your stuff
     retriveEvents();
}

答案 1 :(得分:0)

var timer = new System.Threading.Timer((e) =>
{
    MyMethod();
}, null, 0, TimeSpan.FromMinutes(5).TotalMilliseconds);