15. Overview of WCF

About this Tutorial

Objectives

Delegates will learn to develop applications using C# 4.5. After completing this course, delegates will be able to:

  • Use Visual Studio 2012 effectively
  • Create commercial C# Web Applications
  • Develop multi-threaded applications, Use WCF and LINQ

Audience

This course has been designed primarily for programmers new to the .Net development platform. Delegates experience solely in Windows application development or earlier versions of C# will also find the content beneficial.

Prerequisites

No previous experience in C# programming is required. But any experience you do have in programming will help. Also no experience in Visual Studio is required. But again any experience you do have with programming development environments will be a valuable.

Download Solutions

HTML tutorial


Overview

  1. What is a Service?
    • A service provides a specific function – Typically a business function, such as processing a purchase order.
    • A service can provide a single discrete function – E.g. converting one type of currency into another.
    • Or it can perform a set of related business functions – E.g. handling the various operations in an airline reservations system.
  2. What is WCF?
    • WCF is Microsoft’s unified API for creating service-oriented applications:
      • WCF provides a run-time environment for services.
      • Enables you to expose and consume CLR types as services.
    • WCF implements industry standards, to promote interoperability between services:
      • Service interactions.
      • Type conversions.
      • Marshalling.
      • Protocol support.
  3. WCF Versions:
    • WCF 3.* (in .NET Framework 3.*):
      • Hosting services.
      • Service instance management.
      • Asynchronous calls.
      • Reliability.
      • Transaction management.
      • Disconnected queued calls.
      • Security.
    • WCF 4.* (in .NET Framework 4.*):
      • Simplified configuration
      • Discovery.
      • Routing service.
      • REST (representational state transfer) improvements.
      • Windows Workflow services.
  4. Endpoints: A, B, C – A locatable service is known as an “endpoint”:
    • Address – Where to find a service (i.e. a URL).
    • Binding – How to communicate with a service (e.g. HTTP).
    • Contract – What the service can do (a set of operations).
    • EndpointABC

  5. A is for Address:
    • Every service is associated with a unique address – Address defines location and transport protocol (“schema”).
    • General format of addresses: – [transport] :// [machine] [:optional port] / [optional URI].
    • WCF supports the following transport schemas.
      Address
  6. B is for Binding:
    • A binding specifies low-level communication details – Transport protocol, Message encoding, Reliability etc…
    • WCF has several predefined bindings.
    • For example –
      Binding
  7. C is for Contract:
    • A contract defines the callable operations for a service:
      • [ServiceContract] – Exposes a .NET type (class or interface) as a WCF contract.
      • [OperationContract] – Exposes a method in a WCF service.
      • [DataContract] – Designates a type as being usable as a service param/return.
      • Simple example.
        [ServiceContract(Namespace="urn:myNamespace")]
        interface ITempConverter
        {
          [OperationContract]
          double CtoF(double c);
          [OperationContract]
          double FtoC(double f);
        }

Estimated Time – 1 Hour

Not what you are looking? Try the next tutorial – Multithreading and Parallelization

Lab 1: Implementing a WCF Service

Lab 1: Implementing a WCF Service

Overview

  1. Visual Studio provides various WCF project templates – To help you create server-side WCF applications.
  2. We’ll use the following project templates in this chapter:
    • For the service itself: WCF | WCF Service Library.
    • To host the service in IIS: WCF | WCF Service Application.
  3. To consume a WCF service, you can generate a proxy type based on metadata for a service:
    • Within Visual Studio: “Add Service Reference”.
    • Or at VS command line: Use svcutil.exe.
Lab
  1. Creating a WCF Service Library:
    • To create a new WCF Service Library:
      • Project type: WCF Service Library.
      • Project name: TempConverterServiceLibrary.
      • Solution name: IISHostedDemo.
        CreateWCF
  2. Modifying the BoilerPlate code:
    • Visual Studio generates boilerplate code for a service:
      • Service interface and implementation class.
      • Let’s modify the code as follows:Let’s refactor the generated service interface and class.
        [ServiceContract(Namespace="urn:myNamespace")]
        public interface ITempConverter
        {
          [OperationContract]
          double CtoF(double c);
          [OperationContract]
          double FtoC(double f);
        }


        public class TempConverter : ITempConverter
        {
          public double CtoF(double c)
          { return (c * 9.0 / 5.0) + 32; }
          public double FtoC(double f)
          { return (f - 32) * 5.0 / 9.0; }
        }

        Default namespace: https://tempuri.org.
        Default svc name: interface name.
        Only methods annotated with [OperationContract] are visible to clients, default operation name is method name
        Implementation class implements service interface (implicit or explicit).
      • View code file.
      • View code file.
  3. Testing the service at design time:
    • The project contains a config file that can describe the service – Note: WCF 4.* also has lots of sensible defaults to simplify config.
    • This allows us to test the service at design time:
      • Hit Ctrl+F5 to build and run the project.
      • Launches the WCF Test Client.
        TestWCF

Lab 2: Hosting a WCF Service

Lab 2: Hosting a WCF Service

Overview

  1. There are 3 ways to host a WCF service:
    • IIS.
    • Windows Activation Service (WAS) in IIS 7/8.
    • Self-hosted.
  2. We’ll explore the IIS-hosting option…
Lab
  1. Creating a WCF Service Application:
    • Add a WCF Service Application project to host WCF service:
      • Project type: WCF Service Application.
      • Project name: MyHostApp.
        CreateWCFHost
  2. Configuring the host application:
    • Configure the host application as follows: In Properties window, use specific port for VS Development Server
      ConfigHost
    • Delete unnecessary starter code:
      • Delete IService1.cs and Service1.svc.cs.
      • (You could have defined/implemented the service here, but don’t!).
    • Modify the .svc file (which identifies the target service):
      • Rename Service1.svc to TempConverterService.svc.
      • Modify the file as follows.
        <%@ ServiceHost Language="C#"
               Service="TempConverterServiceLibrary.TempConverter" %>
      • View code file.
    • Add a reference to the service library assembly:
      • The service host need this, so it can instantiate the service.
    • In Web.config, you can explicitly specify service endpoints:
      • Define a <service> element for the service.
      • Add <endpoint> element(s) for the service.
    • Optionally, expose a Metadata Exchange (MEX) endpoint:
      • Allows client to auto-generate proxy types.
        <system.serviceModel>
         <services>
          <service name="TempConverterServiceLibrary.TempConverter" >
           <endpoint address=""
                binding="wsHttpBinding"
                contract="TempConverterServiceLibrary.ITempConverter" />
           <endpoint address="mex"
                binding="mexHttpBinding"
                contract="IMetadataExchange"/>
          </service>
         </services>
         ...
        </system.serviceModel>
  3. Testing the Hosted Service:
    • To test the hosted service:
      • Set the host application as the startup project.
      • Run the solution, and open the .svc file in the browser.
      • Visual Studio creates a test Web page for the .svc file.
        TestingTheHost
  4. Viewing the WSDL for the Service – Click the link on the test page, to view the WSDL for the service –
    WSDL

Lab 3: Consuming a WCF Service

Lab 3: Consuming a WCF Service

Overview

  1. To access a service from a client – You need a proxy object.
  2. If the service publishes its metadata:
    • Generate a proxy type (Add Service Reference, or run SvcUtil).
    • In code, create proxy object and invoke methods.
  3. If the service doesn’t publish its metadata:
    • Add a client-side version of service contract.
    • In code, create proxy object using WCF APIs and invoke methods.
  1. Generating a proxy class using VS:
    • Add a client application to your solution:
      • E.g. add a Console Application named MyClientApp.
    • In your client application:
      • Add a Service Reference.
      • Specify service address.
      • Generates client-side
        proxy class
        and interface
        for a particular service
        contract
        .
        ProxyClass
  2. Implementing the client application:
    • In the client code:
      • Create a proxy object:
        • If config file only has 1 service endpoint, you can omit endpoint name in ctor.
        • Otherwise, you must specify service endpoint in ctor.
      • Invoke methods:
        • Synchronous, blocking calls.
      • Close proxy object when you’re finished (why?):
        • Via Close() or Dispose() method.
      • using MyClientApp.TempConverterServiceReference;
        namespace MyClientApp
        {
         class Program
         {
          static void Main(string[] args)
          {
           using (TempConverterClient proxy = new TempConverterClient())
           {
            double f = proxy.CtoF(100);
            Console.WriteLine("100C = {0}F", f);
           }
           ...

      • View code file.
  3. Running the applications:
    • Set the start-up projects for your solution as follows –
      RunSolution
    • Then run your solution:
      • Runs the host program, to host your service.
      • Also runs the client program, to consume the service.

 

Well done. You have completed the tutorial in the C# course. The next tutorial is

16. Multithreading and Parallelization


Back to beginning
Copyright © 2016 TalkIT®






If you liked this post, please comment with your suggestions to help others.
If you would like to see more content like this in the future, please fill-in our quick survey.
Scroll to Top