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, EventHandlercallback); } 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.
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.
ReplyDeleteData 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
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.
ReplyDeletepython training in annanagar
python training in chennai
python training in chennai
python training in Bangalore
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.
ReplyDeleteBlueprism training in velachery
Blueprism training in marathahalli
A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
ReplyDeleteangularjs Training in chennai
angularjs-Training in tambaram
angularjs-Training in sholinganallur
angularjs-Training in velachery
angularjs Training in bangalore