Consuming WCF Service in WP7

In this post i will show you the very basic way of calling WCF service in WP7 application and persisting the service url in the isolated storage of mobile. In WCF there are two modes to configure endpoints i.e. Imperative and Declarative. Imperative means developer can configure the endpoints through code whereas in the declarative mode, it is configure in the configuration file.

Whats the problem?
We came to the question that suppose we have provided a WP7 application to one of our client. And that application consumes some WCF service. Suppose the service endpoint address is changed, now how can the client configure the endpoint address on his mobile/device.

Every mobile has their persistence storage media. And each application gets the quota where you can store some configuration settings or data to utilize where applicable. In WP7 there is a class called IsolatedStorage through which developer can get an access to the Application store.

Below i have shown a way to use the Isolated Storage and consume WCF service from WP7 mobile application.

Requirements: WP7 SDK, Visual Studio 2010.

Steps:
1. Open Visual Studio and create a new project of “Windows Phone Application”

2. Add another WCF Service Application Project.

3. Open the WP7 project and in the MainPage.xaml form add two textboxes for defining Server Name and Port, add a button to get the data from WCF service and the textbox to display the Service result.

4. Open the Service Application project and define operation contract named GetData as shown below.

namespace TestService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface ITestService
{

[OperationContract]
string GetData();
}
}

namespace TestService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class WCFTestService : ITestService
{
public string GetData()
{
return "Hello world!";
}

}
}

5. Add reference to this service in WP7 project

6. Now in the Button click event we need to call the service, following snippet shows the way to store the Server name and port in the isolated storage of mobile device and consume the service.

private void btn_Click(object sender, RoutedEventArgs e)
{
String strUrl = "http://" + txtServerName.Text + ":" + txtPort.Text + "/WCFTestService.svc";

IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
if (file.FileExists("Sources.res"))
{
file.DeleteFile("Sources.res");

}
IsolatedStorageFileStream fs = file.CreateFile("Sources.res");
System.IO.StreamWriter writer = new System.IO.StreamWriter(fs);
writer.Write(strUrl);
writer.Close();
fs.Close();
fs.Dispose();

System.ServiceModel.EndpointAddress epa = new System.ServiceModel.EndpointAddress(strUrl);
Services.TestService.TestServiceClient client = new Services.TestService.TestServiceClient("BasicHttpBinding_ITestService", epa);
client.GetDataCompleted += new EventHandler(client_GetDataCompleted);
client.GetDataAsync();
}


void client_GetDataCompleted(object sender, Services.TestService.GetDataCompletedEventArgs e)
{
textBox3.Text = e.Result;
}

7. In the MainPage constructor add following snippet to read the Server Name and Port from the isolated saved earlier.

// Constructor
public MainPage()
{
InitializeComponent();
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
string[] strArr=file.GetFileNames();
if (file.FileExists("Sources.res"))
{
IsolatedStorageFileStream fs = file.OpenFile("Sources.res", System.IO.FileMode.Open);
System.IO.StreamReader reader = new System.IO.StreamReader(fs);
String str = reader.ReadLine();
textBox1.Text = str.Substring(str.IndexOf("/") + 2, str.LastIndexOf(":") - 1 - str.IndexOf("/") - 1);
textBox2.Text = str.Substring(str.LastIndexOf(":") + 1, 4);
reader.Close();
fs.Close();
fs.Dispose();
}
}

Now run the application, specify the Server Name and Port and then click on the Retrieve Service Data button.

Advertisements

210 thoughts on “Consuming WCF Service in WP7

    1. Actually i didn’t edit the default config file and it was the same containing the complete endpoint address which adds up on referencing the service. We cannot remove the endpoint address otherwise it wont behave properly so just in sake we keep the address as it is and then in the code behind generate a new address and connect to it.

  1. Nice post. Thanks.

    You may want to correct your code in below 2 location

    1. client.GetDataAsync();
    I think this need an interger as a parameter

    2. client.GetDataCompleted += new EventHandler(client_GetDataCompleted);
    I think this will be client.GetDataCompleted += new EventHandler(client_GetDataCompleted);

  2. I just want to mention I am just newbie to weblog and honestly loved you’re web-site. Very likely I’m going to bookmark your blog post . You amazingly come with terrific posts. Thank you for sharing your website page.

  3. There are some interesting points in time in this article but I don’t know if I see all of them center to heart. There is some validity but I will take hold opinion until I look into it further. Good article , thanks and we want more! Added to FeedBurner as well

  4. I liked as much as you’ll receive performed right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get got an edginess over that you want be turning in the following. in poor health indisputably come further formerly again as exactly the similar just about very often inside of case you defend this hike.

  5. Hi there, simply become alert to your blog via Google, and located that it’s really informative. I’m going to watch out for brussels. I will appreciate if you proceed this in future. Lots of other folks might be benefited out of your writing. Cheers!

  6. A unique discussion might be priced at comment. I believe that you should produce more on this specific topic, may well be a taboo topic but frequently people are too few to speak for such topics. To the next. Best wishes

  7. You could certainly see your enthusiasm in the paintings you write. The arena hopes for even more passionate writers such as you who aren’t afraid to say how they believe. Always go after your heart.

  8. his is the appropriate weblog for anybody who desires to locate out about this topic. You realize so considerably its virtually laborious to argue with you (not that I truly would need…HaHa). You definitely put a brand new spin on a subject thats been written about for years. Excellent stuff, just wonderful!

  9. Hi there, just was aware of your blog via Google, and located that it is really informative. I’m gonna be careful for brussels. I will appreciate when you proceed this in future. Numerous other folks will probably be benefited from your writing. Cheers!

  10. Thank you pertaining to sharing that exceptional written content on your web site. I ran into it on google. I’m going to check back once more once you publish a lot far more aricles.

  11. I would like to thank you for the efforts you have put in writing this site. I’m hoping the same high-grade blog post from you in the upcoming as well. Actually your creative writing skills has inspired me to get my own website now. Really the blogging is spreading its wings rapidly. Your write up is a great example of it.

  12. Needed to draft you a bit of word just to give many thanks once again for the pleasing ideas you’ve shown on this page. This is really unbelievably open-handed with people like you giving unhampered precisely what a number of us would’ve advertised for an e book to earn some bucks for their own end, mostly seeing that you might have tried it in the event you decided. The thoughts as well acted like the easy way to know that other individuals have a similar zeal just like my personal own to understand more and more on the subject of this condition. I’m certain there are a lot more enjoyable moments ahead for those who examine your site.

  13. Thanks so much for providing individuals with a very terrific opportunity to discover important secrets from this blog. It can be very terrific and as well , jam-packed with a lot of fun for me and my office peers to search your web site the equivalent of three times in one week to find out the fresh things you have got. Of course, I’m usually happy for the surprising tactics you give. Selected 1 areas in this posting are without a doubt the most effective we’ve ever had.

  14. I just want to tell you that I am very new to blogging and seriously enjoyed your web page. Likely I’m going to bookmark your site . You amazingly come with impressive stories. Regards for sharing your web site.

  15. I simply want to say I am just beginner to blogs and seriously enjoyed you’re website. Probably I’m likely to bookmark your site . You amazingly come with really good writings. Thank you for sharing with us your webpage.

  16. Hello there, simply become aware of your blog via Google, and found that it is truly informative. I’m gonna be careful for brussels. I’ll appreciate when you proceed this in future. A lot of other people shall be benefited from your writing. Cheers!

  17. Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you can do with a few pics to drive the message home a little bit, but instead of that, this is fantastic blog. A great read. I will certainly be back.

  18. I do trust all the concepts you’ve introduced in your post. They are very convincing and can certainly work. Still, the posts are very short for beginners. May you please prolong them a bit from subsequent time? Thank you for the post.

  19. This really is the correct weblog for anyone who wants to uncover out about this topic. You realize so considerably its almost hard to argue with you (not that I in fact would want…HaHa). You surely put a new spin on a subject thats been written about for years. Fantastic stuff, just great!

  20. Pretty great post. I just stumbled upon your blog and wished to say that I have truly enjoyed surfing around your weblog posts. In any case I’ll be subscribing on your feed and I am hoping you write once more very soon!

  21. I admire the valuable information you be offering on your articles. I can bookmark your blog and feature my kids test up right here generally. I am slightly certain they’re going to learn lots of new stuff right here than anybody else!

  22. Admiring the dedication you put into your website and detailed information you offer. It’s nice to come across a blog every once in a while that isn’t the same outdated rehashed material. Wonderful read! I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

  23. Hiya, I am really glad I have found this info. Today bloggers publish only about gossips and web and this is really frustrating. A good blog with exciting content, that is what I need. Thank you for keeping this web site, I’ll be visiting it. Do you do newsletters? Can’t find it.

  24. I would like to thnkx for the efforts you’ve put in writing this blog. I am hoping the same high-grade site post from you in the upcoming as well. In fact your creative writing skills has encouraged me to get my own web site now. Actually the blogging is spreading its wings quickly. Your write up is a good example of it.

  25. I conceive this internet site contains some really excellent information for everyone. “Few friendships would survive if each one knew what his friend says of him behind his back.” by Blaise Pascal.

  26. I got what you mean , thankyou for putting up.Woh I am thankful to find this website through google. “Finley is going over to get a new piece of bat.” by Jerry Coleman.

  27. I’m also commenting to make you be aware of what a great encounter my wife’s child obtained reading through yuor web blog. She learned many pieces, which included how it is like to have an ideal helping mindset to get men and women easily master specific multifaceted matters. You undoubtedly did more than our desires. Thanks for providing the insightful, dependable, educational and in addition fun thoughts on this topic to Emily.

  28. My husband and i were quite comfortable Jordan could finish up his investigations with the ideas he made out of the web page. It is now and again perplexing just to find yourself giving freely techniques which usually other folks could have been making money from. And we all discover we have got the writer to give thanks to because of that. All of the explanations you have made, the easy site navigation, the friendships your site give support to foster – it’s got everything astonishing, and it’s facilitating our son in addition to us understand the topic is exciting, and that’s extraordinarily vital. Thank you for the whole lot!

  29. I want to express thanks to the writer just for bailing me out of such a predicament. Because of searching through the world-wide-web and finding principles which were not beneficial, I assumed my entire life was over. Living without the presence of answers to the issues you’ve resolved by means of your good report is a critical case, and ones that could have negatively affected my entire career if I hadn’t noticed your web page. The understanding and kindness in maneuvering every item was vital. I am not sure what I would’ve done if I had not come across such a subject like this. I am able to at this point look ahead to my future. Thank you so much for the skilled and effective guide. I won’t hesitate to propose your web page to anyone who ought to have counselling on this area.

  30. Many thanks for taking time to discuss this, I feel strongly about it and like learning additional on this theme. If doable, as you gain expertise, would you mind updating your weblog with excess facts? It can be very beneficial for me.

  31. Do you mind if I quote a few of your posts as long as I provide credit and sources back to your site? My blog site is in the very same area of interest as yours and my users would really benefit from a lot of the information you present here. Please let me know if this alright with you. Many thanks!

  32. Hi there! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done a outstanding job!

  33. You developed some decent points there. I looked more than the internet for any difficulty and discovered most individuals goes as effectively as together with your web web site.

  34. I really like blogging when it’s for or about something I believe in. I also read news blogs often and find that it makes me feel much much more intelligent every time I read them. I also feel like I’m a pretty excellent person who tries to treat other people with respect, no matter what their view is. You will find some real haters out there. Thanks once again. I learned several things about blogging. I will certainly put your web site on my speed dial.

  35. I lately ran into your blog and get been studying along. I assumed I’d leave my primary remark. I don???ê?èt understand what to say but I’ve relished reading. Pleasant blog. I will maintain visiting this site extremely typically.

  36. I keep listening to the reports lecture about receiving boundless online grant applications so I have been looking around for the top site to get one. Could you advise me please, where could i acquire some?

  37. Thank you for another informative blog. The place else may I get that kind of info written in such a perfect method? I’ve a mission that I am simply now running on, and I have been on the look out for such info.

  38. I’ve been browsing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the internet will be much more useful than ever before.

  39. Its such as you read my mind! You appear to know so much about this, such as you wrote the e-book in it or something. I feel that you simply could do with a few percent to power the message home a little bit, however other than that, this is excellent blog. An excellent read. I will definitely be back.

  40. Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something. I think that you can do with some pics to drive the message home a little bit, but instead of that, this is wonderful blog. A fantastic read. I will definitely be back.

  41. I think that is one of the most important information for me. And i am happy reading your article. However should commentary on some basic issues, The site taste is great, the articles is in point of fact nice :D. Excellent process, cheers.

  42. I genuinely enjoy examining on this website, it has excellent content. “The living is a species of the dead and not a very attractive one.” by Friedrich Wilhelm Nietzsche.

  43. I got what you mean , thanks for putting up.Woh I am thankful to find this website through google. “No one can earn a million dollars honestly.” by William Jennings Bryan.

  44. Admiring the commitment you put into your site and in depth information you provide. It’s nice to come across a blog every once in a while that isn’t the same out of date rehashed material. Wonderful read! I’ve saved your site and I’m including your RSS feeds to my Google account.

  45. Fantastic goods from you, man. I have understand your stuff previous to and you are just too wonderful. I actually like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it enjoyable and you still take care of to keep it sensible. I can not wait to read much more from you. This is actually a wonderful website.

  46. Hi just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading correctly. I’m not sure why but I think its a linking issue. I’ve tried it in two different internet browsers and both show the same results.

  47. I wanted to compose a small message to say thanks to you for all the stunning solutions you are posting at this site. My time consuming internet lookup has at the end been paid with reasonable information to talk about with my partners. I ‘d state that that we readers actually are unquestionably endowed to be in a wonderful site with very many awesome individuals with helpful plans. I feel extremely lucky to have used your entire weblog and look forward to some more cool times reading here. Thanks a lot once more for everything.

  48. Your web page is taking forever to load. This is the second page I tried loading and it took pretty much 30 seconds to load the page. And there is hardly something on it! I know it’s not my net connection speed for the reason that anything else that I attempt works fine. Maybe your server is beneath attack or something? Or do you just have THAT lots of guests at your web site ideal now?? Just believed I’d give you a heads up!

  49. Aw, this has been a really nice post. In plan I would like to put in writing similar to this additionally * taking time plus actual work to make a very good article… but exactly what do I say… My spouse and i procrastinate a large amount and by virtually no means apparently get something perfo

  50. Thanks for all your efforts that you have put in this. Very interesting info. “As a rule, what is out of sight disturbs men’s minds more seriously than what they see.” by Richard Bach.

  51. Nice read, I just passed this onto a colleague who was doing a little research on that. And he just bought me lunch as I found it for him smile Therefore let me rephrase that: Thank you for lunch! “England and America are two countries separated by the same language.” by George Bernard Shaw.

  52. I simply needed to say thanks once again. I do not know what I would’ve used in the absence of the entire aspects contributed by you over such a theme. It actually was the daunting dilemma in my circumstances, however , being able to view your professional technique you handled it forced me to weep over fulfillment. I will be happy for this information and have high hopes you comprehend what a great job you happen to be putting in instructing many people through your blog. Most likely you have never encountered all of us.

  53. Magnificent beat ! I would like to apprentice even as you amend your site, how can i subscribe for a weblog site? The account helped me a acceptable deal. I had been a little bit familiar of this your broadcast offered shiny clear idea.

  54. Many thanks for being my personal teacher on this subject. My partner and i enjoyed your article a lot and most of all liked how you really handled the aspect I regarded as being controversial. You happen to be always really kind towards readers like me and aid me in my everyday living. Thank you.

  55. Hiya, I’m really glad I’ve found this info. Nowadays bloggers publish only about gossips and web and this is really frustrating. A good blog with exciting content, that is what I need. Thanks for keeping this web-site, I will be visiting it. Do you do newsletters? Cant find it.

  56. I am only writing to let you be aware of of the fine experience my friend’s daughter went through browsing your web site. She learned so many things, most notably what it is like to have a great teaching heart to get many people clearly gain knowledge of various tortuous subject matter. You actually surpassed our expected results. Many thanks for producing these important, trusted, informative and even easy guidance on your topic to Julie.

  57. I must express my gratitude for your generosity for individuals who should have guidance on this one topic. Your real commitment to passing the solution across appeared to be astonishingly valuable and have always made others much like me to attain their pursuits. Your new warm and friendly tips and hints means much to me and especially to my mates. Many thanks; from each one of us.

  58. I wish to show my thanks to this writer just for rescuing me from this type of predicament. After looking through the world wide web and obtaining methods which were not helpful, I thought my entire life was over. Existing without the presence of strategies to the difficulties you have solved by means of your entire short post is a critical case, as well as the ones which may have adversely affected my career if I hadn’t encountered your blog post. Your actual knowledge and kindness in handling all areas was excellent. I am not sure what I would’ve done if I had not come across such a thing like this. I can now relish my future. Thanks for your time so much for this skilled and results-oriented help. I won’t be reluctant to suggest your web site to any individual who wants and needs recommendations about this area.

  59. I wanted to send you that tiny word to give thanks over again just for the gorgeous things you’ve discussed above. It is so shockingly open-handed with you to make freely just what some people could possibly have advertised as an e-book in making some profit for themselves, specifically since you might well have tried it in case you decided. These advice likewise served like the fantastic way to understand that other individuals have the same zeal the same as my own to realize somewhat more in regard to this matter. I am sure there are lots of more pleasurable situations in the future for many who look into your site.

  60. You really make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complex and extremely broad for me. I’m looking forward for your next post, I will try to get the hang of it!

  61. Thank you, I’ve recently been looking for info approximately this subject for a long time and yours is the greatest I’ve discovered till now. However, what about the bottom line? Are you certain about the supply?

  62. Last time I was right here you had a various writing style, are you utilizing content writers or do you write this all oneself? I was asking yourself simply because Ive been flipping by means of some of your other posts and it appears that its not all written by the very same person, judging by the writing style. Perhaps I’m wrong, but I am just curious to know.

  63. Wow! This could be one particular of the most helpful blogs We have ever arrive across on this subject. Basically Excellent. I’m also an expert in this topic therefore I can understand your effort.

  64. Many thanks for an incredible publish, would read your others content. many thanks for your thoughts on this, I felt a lttle bit struck by this article. Thanks again! You earn an excellent moment. Displays the beauty through wonderful info here. I believe if a greater number consideration for it that way, they’d have a better time period obtain the suspend ofing the matter.

  65. I would like to show my appreciation to this writer just for rescuing me from this particular challenge. Because of checking throughout the world-wide-web and coming across tips which are not helpful, I figured my entire life was done. Living devoid of the strategies to the issues you’ve resolved by way of your short post is a crucial case, and the ones that could have adversely affected my career if I hadn’t encountered the website. That knowledge and kindness in taking care of all areas was invaluable. I’m not sure what I would’ve done if I hadn’t encountered such a step like this. I am able to now look ahead to my future. Thank you so much for your professional and effective guide. I won’t be reluctant to propose your web blog to any person who needs and wants assistance about this problem.

  66. Thanks for an amazing put up, will study your others content. i appreciate your thoughts with this, I experienced a bit strike by this post. Many thanks again! You wanna make an excellent aspect. Has great facts here. I think that if more individuals thought about it that way, they’d have got a better moment in time have the suspend ofing the situation.

  67. Just desire to say your article is as astounding. The clarity in your submit is just spectacular and that i can suppose you are knowledgeable in this subject. Well together with your permission allow me to snatch your RSS feed to keep updated with coming near near post. Thanks one million and please continue the enjoyable work.

  68. hello!,I like your writing so much! proportion we be in contact more about your post on AOL? I need a specialist on this space to resolve my problem. Maybe that is you! Taking a look ahead to look you.

  69. I feel that is among the most significant information for me. And i am satisfied reading your article. But wanna commentary on some general things, The site taste is wonderful, the articles is in reality nice :D. Good activity, cheers.

  70. I just want to say I am all new to blogging and certainly loved you’re web blog. Likely I’m likely to bookmark your website . You actually come with outstanding posts. Thanks a bunch for sharing with us your webpage.

  71. Greetings from Colorado! I’m bored at work so I decided to check out your blog on my iphone during lunch break. I really like the knowledge you provide here and can’t wait to take a look when I get home. I’m surprised at how quick your blog loaded on my phone .. I’m not even using WIFI, just 3G .. Anyhow, wonderful site!

  72. Merely to follow up on the update of this theme on your web page and would wish to let you know just how much I liked the time you took to create this valuable post. Inside the post, you really spoke of how to actually handle this problem with all ease. It would be my own pleasure to get some more ideas from your website and come up to offer other individuals what I learned from you. I appreciate your usual terrific effort.

  73. Keep up the fantastic piece of work, I read few articles on this internet site and I think that your blog is real interesting and contains lots of great information.

  74. you’re in point of fact a good webmaster. The site loading velocity is incredible. It seems that you’re doing any unique trick. Moreover, The contents are masterwork. you’ve performed a wonderful activity in this matter!

  75. I would like to thnkx for the efforts you have put in writing this website. I’m hoping the same high-grade site post from you in the upcoming as well. In fact your creative writing skills has encouraged me to get my own blog now. Actually the blogging is spreading its wings rapidly. Your write up is a great example of it.

  76. Together with every thing that seems to be developing inside this particular subject material, many of your opinions happen to be very stimulating. Nevertheless, I appologize, but I do not give credence to your whole suggestion, all be it exciting none the less. It appears to me that your remarks are actually not totally justified and in actuality you are yourself not really fully convinced of the argument. In any case I did appreciate examining it.

  77. I just want to say I am all new to blogging and certainly loved you’re web blog. Likely I’m likely to bookmark your website . You actually come with outstanding posts. Thanks a bunch for sharing with us your webpage.

  78. Nice weblog right here! Additionally your website a lot up very fast! What web host are you the usage of? Can I am getting your affiliate hyperlink to your host? I wish my site loaded up as fast as yours lol

  79. This is the correct weblog for anyone who wants to find out about this topic. You understand a lot its nearly onerous to argue with you (not that I really would want…HaHa). You undoubtedly put a brand new spin on a subject thats been written about for years. Great stuff, just nice!

  80. I am extremely impressed together with your writing skills and also with the structure on your blog. Is that this a paid subject or did you customize it yourself? Either way stay up the nice quality writing, it’s rare to look a great weblog like this one nowadays..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s