Saturday, March 10, 2012

simple Web Service Agent Pattern Webservice Endpoint configuration for local/live hosting

Setting up webservice to consume is difficult as it has to be hosted in different enviornements and sometime it is difficult to understand the webservice call errors when moving from local to live hosting. Most of the time we rely on auto generated Urls in configuration files.

Best way to handle such situation is to properly configure Urls in local and hosting enviornements. But if it is required to configure the webservice from code behind, i have a simple solution.

Other than that i am going to introduce you to service agent pattern. This is a very simple and generic pattern for Asynchoronous web clients like silverlgiht. This helps configure webservice in reuseable way and makes testing easier. Helps in plugable webservice, and specially in IoC/Dependecny injections.

namespace FileUploader.ServiceAgents
{
    public interface IImageServiceAgent
    {
        void SaveImage(byte[] buffer, string fileName, EventHandler callback);
    }

    public class ImageServiceAgent : IImageServiceAgent
    {
        private readonly ImageServiceSoapClient _proxy;


        public ImageServiceAgent()
        {
            Binding binding = new BasicHttpBinding();
            var endpoint = new EndpointAddress(
                new Uri(Application.Current.Host.Source, "../Webservices/ImageService.asmx"));
            _proxy = new ImageServiceSoapClient(binding, endpoint);
        }

        #region IImageServiceAgent Members

        public void SaveImage(byte[] buffer, string fileName, EventHandler callback)
        {
            _proxy.SaveImageCompleted += callback;
            _proxy.SaveImageAsync(buffer,fileName);
        }

        #endregion
    }
}

Lets start with the inteface. First line defines the IImageServiceAgent. This interface lists all the functions of webservice along with their call backs.

In the implemenations, it has service client that is using basicHttpBinding with endpoint configuraiton in such a way that it will automatically handle any enviornment given that your client and web service is hosted in the same hosting url.

Implementaion of SaveImage is simple, first it subscribes to SaveImageCompleted call back and then calls the Async client.

This is a good way as it can helps using anonymous functions in a good way. Let me know if this helps you or you have a better solution to today world coding standards.

4 comments:

  1. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    Data Science with Python training in chenni
    Data Science training in chennai
    Data science training in velachery
    Data science training in tambaram
    Data Science training in anna nagar
    Data Science training in chennai
    Data science training in Bangalore

    ReplyDelete
  2. I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.
    python training in annanagar
    python training in chennai
    python training in chennai
    python training in Bangalore

    ReplyDelete
  3. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    Blueprism training in velachery

    Blueprism training in marathahalli

    ReplyDelete