none
Client code to send a request to BizTalk wcf service RRS feed

  • Question

  • I have created a client code to send few requests to BizTalk wcf service, I want to send each request to BizTalk at the interval of few seconds. should I use Thread.sleep or Task.Delay. Is there a better way to do it?
    Sunday, May 29, 2016 7:08 PM

Answers

  • If this is a client app for testing, just use Thread.Sleep(), in a loop of course.  This is easiest way to get what you need.  You'll loose some UI responsiveness but, well, no big deal.

    You can get the same effect with Tasks, but the extra complication in the client doesn't get you anything on the BizTalk side.

    Sunday, May 29, 2016 11:24 PM
    Moderator

All replies

  • Hi Udal

    From BizTalk point of view, it does not matter - both patterns will call BizTalk after the designated time interval. The difference will be for the calling client app.

    What type of application is your client code? Is it a console app that is just designed to send requests to the BizTalk WCF service? In that case it doesn't really matter. If your client code has a UI (eg: a WinForms app), where the UI thread should not be blocked, use Task.Delay.

    Use Thread.Sleep when you want to block the current thread. If your client app has a UI, like a WinForms application, the UI is not responsive while Thread.Sleep is called. If your code is synchronous - i.e., you are not doing anything other than calling the BizTalk service, and there is no UI to display, using Task.Delay doesn't make much sense. You can safely use Thread.Sleep in this case.

    Use Task.Delay when you want a logical delay without blocking the current thread. Task.Delay does not block the calling thread so the UI will remain responsive(if your client app has a UI like a WinForms app).

    http://social.technet.microsoft.com/wiki/contents/articles/21177.visual-c-thread-sleep-vs-task-delay.aspx

    http://stackoverflow.com/questions/20082221/when-to-use-task-delay-when-to-use-thread-sleep


    Thanks Arindam








    Sunday, May 29, 2016 7:54 PM
    Moderator
  • If this is a client app for testing, just use Thread.Sleep(), in a loop of course.  This is easiest way to get what you need.  You'll loose some UI responsiveness but, well, no big deal.

    You can get the same effect with Tasks, but the extra complication in the client doesn't get you anything on the BizTalk side.

    Sunday, May 29, 2016 11:24 PM
    Moderator
  • Thanks Arindam! I am using console application only to send the request to BizTalk WCF serve and I am using Thread.sleep. But the problem is, it is not accurate, there is a difference of few milliseconds. 
    Monday, May 30, 2016 5:35 AM
  • Thanks ! I am using console application only to send the request to BizTalk WCF serve and I am using Thread.sleep. But the problem is, it is not accurate, there is a difference of few milliseconds.
    Monday, May 30, 2016 5:35 AM
  • Hi Udal

    Thread.Sleep is bit more accurate than Task.Delay as thread pooling comes into play with Task.Delay.

    What exactly do you mean by accuracy - are you measuring something, or do you think that the call is not being made at the correct interval?


    Thanks Arindam


    Monday, May 30, 2016 5:40 AM
    Moderator
  • Hi,

    Task.Delay is much better compare to Thread.Sleep, as Thread.Sleep will pause the current thread for defined interval.

    If your primary motive is to check the Performance Test or Accuracy of the application I would suggest to use the StopWatch on MSDN class

    Here is good article which explains the functionality with good and bad preparations. Precise Run Time Measurements with System.Diagnostics.Stopwatch

    Hope this Helps!!!

    Please Mark as Answered if you satisfy with Reply.

    Monday, May 30, 2016 6:38 AM
  • Actually, I am creating a channel factory each time when sending a request to endpoint. It is taking time when creating a channel and sending a request each time. Since, endpoint is same for all the request, is there a way I can create a channel only once and use it for every request message to send.
    Monday, May 30, 2016 7:58 AM
  • Actually, I am creating a channel factory each time when sending a request to endpoint. It is taking time when creating a channel and sending a request each time. Since, endpoint is same for all the request, is there a way I can create a channel only once and use it for every request message to send.
    Monday, May 30, 2016 7:58 AM
  • Hi Udal

    To be frank for a simple app to just call a WCF service, don't worry about the overhead to create your ChannelFactory instance.If performance is a key requirement for you, please read on.

    Yes you can certainly cache your ChannelFactory instance - this will have some perf benefits. However, before each call to the service operation, use your cached ChannelFactory instance to get a new Channel to make the actual call to the service.

    See this for an example-

    public static T GetFactoryChannel<T>(string address)
    {
    
        string key = typeof(T.Name);
    
        if (!OpenChannels.ContainsKey(key))
        {
            ChannelFactory<T> factory = new ChannelFactory<T>();
            factory.Endpoint.Address = new EndpointAddress(new System.Uri(address));
            factory.Endpoint.Binding = new BasicHttpBinding();
            OpenChannels.Add(key, factory);
        }
    
        T channel = ((ChannelFactory<T>)OpenChannels[key]).CreateChannel();
    
        ((IClientChannel)channel).Open();
    
        return channel;
    }

    http://stackoverflow.com/questions/7841748/wcf-channel-and-channelfactory-caching


    Thanks Arindam


    Monday, May 30, 2016 8:12 AM
    Moderator
  • Also note, if you are on .NET 3.0 SP1 and above, you can create your proxy based on ClientBase<T>, and it would be cached automatically for you.

    So, instead of using ChannelFactory, use Add Service Reference option to create your proxy (example here). Create your proxy object globally before you start your loop to invoke the service operation. Now, when you make the service calls within the loop, WCF will use a cached proxy instance based on ClientBase<T> automatically for you.

    I would strongly suggest you try this approach first.


    Thanks Arindam





    Monday, May 30, 2016 8:25 AM
    Moderator
  • Why are you worried so much about this client app?  Will it ever be used for production purposes?

    I've thrown together dozens of these using the most basic pattern possible.

    Monday, May 30, 2016 11:53 AM
    Moderator
  • Hi Vikas,

    It's better that other people decide if a post is useful or qualifies as answer.

    Steef-Jan Wiggers

    Microsoft Azure MVP


    BizTalk

    Saturday, June 18, 2016 3:38 PM
    Moderator