Consuming a WCF Service

You consume a WCF service from a .NET client by adding a service reference to your project. Visual Studio then generates a proxy class for calling your service. You use this proxy class to call your service from your .NET client application. Your client application could be a website, any variety of Windows application, or an application on another platform. Let’s look at an example of calling WCF from an ASP.NET MVC site.


Note

ASP.NET 5 does not currently have the concept of a Service Reference (at least at the time of writing). Therefore, the example that follows uses an MVC application that targets ASP.NET MVC 6 (and not ASP.NET 5). This allows you to see the Service Reference concept in action.

You can also consume WCF services using HttpClient and jQuery (without a Service Reference proxy). This requires additional configuration on your services/endpoints to make them work as REST-based services.


1. Open a new instance of Visual Studio and create a new web project (File, New, Project). Select the ASP.NET Web Application, name the project WcfClientMvc, and click the OK button. Select the MVC template (and not an ASP.NET 5 template; we will use that later) and click OK again.

2. Make sure your customer services application is running in another instance of Visual Studio. (You can run in debug mode.)

Inside the WcfClientMvc project, Right-click the References node in Solution Explorer and choose Add Service Reference to launch the Add Service Reference dialog box.

Type the address to your running service in the Address line of the Add Service Reference dialog. You can get the address from the WCF Test Client, right-click the service and choose Copy Address. With the address in the Add Service Reference dialog, click the Go button. Visual Studio will find your service and display the methods, as shown in Figure 19.22.

Set the Namespace to CustomerProfile and click the OK button to add the service reference to your project.

Image

FIGURE 19.22 Use the Add Service Reference dialog to set a reference from your project to an existing WCF service.

3. Visual Studio will create a Service Reference folder in your solution and add the CustomerProfile reference (as a proxy class for working with the service). It will also add a reference to your project, including System.NET.Http, for working with HttpClient, as we did in the Web API client example. The proxy class generated on your behalf uses this to work with the WCF service.

Note that if you double-click the CustomerProfile service reference, Visual Studio shows the Object Browser. Here, you can view the contents of the service reference proxy.

In addition, if you open the Web.config file, you will see a node called <system.serviceModel>; this represents your service configuration data, including bindings and client endpoints.

Right-click the Web.config file in Solution Explorer and choose Edit WCF Configuration to open the configuration (as shown in Figure 19.23). Select the Client folder, Endpoints, BasicHttpBinding_ICustomerProfile. Note the client endpoint name and address (as you use them shortly) and close the configuration editor.

Image

FIGURE 19.23 You can use the WCF service configuration editor to view and edit WCF client configurations for your web application.

4. Create the controller for handling requests to the WCF service and serving up views. Right-click the Controllers folder and choose Add, Controller. Select the MVC 5 Controller – Empty and click the Add button. When prompted, name your controller CustomerController.

5. Write the code for the Index method to call the WCF service, get a list of customers, and return the Index view with the customer data. Start by adding a using statement for System.Threading.Tasks; to the top of the CustomerController.cs file.

Remove the Index() stubbed-out method in the template class. Replace it with code similar to Listing 19.17. Notice that we first create an instance of the proxy class, CustomerProfile.CustomerProfileClient as custSrv. We then use that class to get a list of customers by calling custSrv.GetListAsync();.

LISTING 19.17 The CustomerController Index() Method Used to Call the WCF Service via the Service Reference Proxy and Return the Corresponding Index View


public async Task<ActionResult> Index()
{
  CustomerProfile.CustomerProfileClient custSrv =
    new CustomerProfile.CustomerProfileClient(
    "BasicHttpBinding_ICustomerProfile");

  CustomerProfile.Customer[] customers =
    await custSrv.GetListAsync();

  return View(customers);
}


6. Create the view for showing a list of customers. Make sure you have a Customer folder under Views in Solution Explorer (if not, add one). Right-click the Customer folder and choose Add, View.

In the Add View dialog (see Figure 19.24), set the view name to Index. Under template, choose List. Under Model class, find the Customer object that was generated when you created the service reference. Finally, select the layout page for the application and click Add.

Image

FIGURE 19.24 Use MVC scaffolding to generate a view based on the Customer model in the Service Reference proxy.

Visual Studio will generate a working view for Index. You need only to change the page title (optional).

You can now run the client application and navigate to the customer page. Make sure the WCF service application created earlier is also running (inside another instance of Visual Studio). Figure 19.25 shows the results.


Sample Code

We end the example here. However, the sample code for the book contains all the services in WCF (Get, GetList, Create, Update, and Delete) wired to the MVC client project.


Image

FIGURE 19.25 The MVC application running against the WCF services.

Other Windows .NET clients work the same way (WinForms and WPF, for example). You add a Service Reference to the project and then use the service reference to call the service from the client. Remember, WCF services can support multiple endpoint types. This includes REST-based HTTP services. The next section shows how to leverage this popular approach with WCF.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset