Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
This movie shows how to use an existing WCF service from a client application. For debugging purposes, I will keep the service project and the client project in the same solution. Remember, it's important that you run Visual Studio as an administrator if you want to test the service. I am inside Visual Studio, and I've opened a solution called UseWcfServices. If you look at the top of my Visual Studio, you'll see the word "Administrator", which means I am running in Administrator mode. There is a BookService available for my consumption inside this project.
This is similar to the one in the other movie in this chapter. And I have this WPF application, which is going to serve as my user interface. Let's review the user interface. I am going to come down here and look at MainWindow.xaml. Double-click. There is a list box on the left side of the screen that I wish to populate with the names of the books that are available from the service. Then when I click on this Button, I want to add a new book to the collection. The first thing I need to do is tell Visual Studio where to find my service.
I do that by going to my project and choosing Add Service Reference. Next, I need to provide the location where the address lives. In my case, it's living on the same server as my client application. It might also be out there on the Internet somewhere. I can type a URL here. Or since I know that my service lives on the same computer, I can click Discover, and wait for it to define the service. Then when I click on this expand node, it's going to launch the service-- notice down here it says WcfSvcHost-- and then it shows here is the service, there is my interface, and there are the tree operation contracts.
There is a method called AddBook, a method called GetBooks, and a method called UpdatePrices. I want to be sure that I can call this service asynchronously, so I am going to click on the Advanced button and then check this Generate asynchronous operations check box before I continue. I also need to give my Namespace a decent name. I am going to call this one BookService. Everything looks okay, so I will click OK.
At this point, I can call a service as if it were living in a local DLL. So I am going to switch over to my Code view by clicking F7, and I am going to instantiate an instance of my service right here. Then inside my Windows Loaded event, I am going to grab the data from the GetBooks method.
Here it is, GetBooks, open and close parenthesis. Then I need to tell the list box which property to show in the list. So I'll do that here by saying DisplayMemberPath= and the name of field or the name of the property. In my case, it's called Title. I am making this call on the same thread that's running inside this procedure, which in my case is the UI thread. So what's going to happen is when the application starts up, it's going to go out and call the service. And since I am doing it on the UI thread, it's going to block and wait until the service returns before it runs the next line of code, which will cause a small delay at my start up time.
I am going to press F5 to test my application. The service host starts up. Notice that I have no UI for about two seconds, and then once the service has returned, this list box now contains the data. It would be better if I didn't make this call on the UI thread, but to make it on a worker thread. So when I click on this button to add a book to the collection, I'll do that on a background thread. I'll shut this application down. Return back to my code.
On this line of code, I am going to declare a variable of my BookData type, and then my last one, I am going to choose a title, since that's what we were looking for in the list box. Say Title=. Next, I want to add the book, so I am going to call my service.
You might remember there was a method called AddBook. Because I did the asynchronous service, you see that I have an AddBook method, an AddBookAsync, and I also have this event that fires when that method returns, AddBookCompleted. So I am going to start by setting up my event procedure. I'll choose this item and choose a plus, equal. Visual Studio is offering to stub some code in for me. I am going to press Tab once to complete that line of code, and then I am going to press Tab a second time to stub in the method.
Now I am going to invoke the service, AddBookAsync, passing in the instances of my book as a property, and then I am going to go down here and update my list box when it's done. This is the same code I had up here, ItemsSource = _service, I gave this. I think it might be faster if I just copied this and pasted that in down here.
If I did everything right, this service should add a book. Let's try it out. I am going to press F5, and I will click on the book one time. There is our new book added. And just for good measure, I'll click on the book a second time to verify that it's still working. As you can see, it is fairly simple to add a reference to the service. Before I end this movie, I want to point out that there are other layers built on top of WCF, including WCF RIA Services and WCF Data Services.
Both are beyond the scope of this course, but I encourage you to check them out as you become more familiar with .NET.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92883 Viewers
82 Video lessons · 104244 Viewers
71 Video lessons · 75964 Viewers
56 Video lessons · 107315 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.