Tuesday, August 20, 2013

Consume Third Party Web Service Using Auto Generated Early Bound Proxy Object - C# - Part 1

Modern software solutions provides bunch of services to their users. Also most of current software solutions are not a stand-alone systems. They have lots of integration points with other systems to interact with each other. WCF web services are the most widely used service providing interface to software systems to interact with each other. There are several ways to consume third party WCF web service. Following two methods are the most commonly used pathways to consume third party web service.
  1. Use generated early bound service proxy classes
  2. Use service reference
In this post we discuss how to generate early bound proxy classes and use these proxy classes to consume third party web service.

Creating WCF web service

We use very similar WCF web service which used to consume C# web service using Android application ("C# Web Services and Android").
WCF web service provides functionality to retrieve person details based on the person Id. Use same person business object class to create person object which used in "C# Web Services and Android". Create "IPersonOperations" interface to define person operations and create "PersonOperations" concrete class to implements the services already defined in " IPersonOperations " interface class.

    interface IPersonOperations
    {
        /// 
        /// Creates the person list.
        /// 
        /// list of person
        List CreatePersonList();

        /// 
        /// Gets the person by unique identifier.
        /// 
        /// The user unique identifier.
        /// person
        Person GetPersonById(string userId);
    }


    public class PersonOperations:IPersonOperations
    {
        /// 
        /// Creates the person list.
        /// 
        /// list of person
        public List CreatePersonList()
        {
            List personList = new List();

            Person person1 = new Person();

            person1.FirstName = "Iron";
            person1.LastName = "Man";
            person1.Id = "IM01";

            Person person2 = new Person();

            person2.FirstName = "Avatar";
            person2.LastName = "Aang";
            person2.Id = "AA02";

            Person person3 = new Person();

            person3.FirstName = "Avatar";
            person3.LastName = "Korra";
            person3.Id = "AA03";

            personList.Add(person1);
            personList.Add(person2);
            personList.Add(person3);

            return personList;
        }

        /// 
        /// Gets the person by unique identifier.
        /// 
        /// The user unique identifier.
        /// person
        public Person GetPersonById(string userId)
        {
            List personList = this.CreatePersonList();

            if (string.IsNullOrEmpty(userId) || personList == null || personList.Count == 0)
            {
                return null;
            }

            Person result = personList.Where(x => x.Id.Equals(userId, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();

            if (result == null)
            {
                return null;
            }

            return result;
        }
    }


Creating WCF operation contract and service contract

To access the WCF web service we need to create service contract and operation contract. Service contract create service interface to outside of the web service. Operation contract defines the operations which can perform under the relevant service contract. Namespace of the service contract is very important. Accessing web service and operations are totally depends on the service contract namespace. If you didn't provide namespace for service contract then it use default namespace " http://tempuri.org/". In this scenario we use namespace called " http://DemoWebService.com/ ". " IDemoService " is the service which provided by WCF web service and " GetPersonById " is the operation which provided under previously mentioned service.



    [ServiceContract(Namespace="http://DemoWebService.com/")]
    public interface IDemoService
    {
        /// 
        /// Gets the person by unique identifier.
        /// 
        /// The user unique identifier.
        /// person
        [OperationContract]
        Person GetPersonById(string userId);
    }


    public class DemoService : IDemoService
    {
        /// 
        /// Gets the person by unique identifier.
        /// 
        /// The user unique identifier.
        /// 
        /// person
        /// 
        public Person GetPersonById(string userId)
        {
            if (string.IsNullOrEmpty(userId))
            {
                return null;
            }
            else
            {
                PersonOperations result = new PersonOperations();

                return result.GetPersonById(userId);
            }
        }
    }


Important: before add the namespace for the WCF service please add " System.ServiceModel " reference. Otherwise it will give compile time errors.

Configure WCF web service

This is the one of the important step of the hosting WCF service. Following web config shows the important configurations we need to set before host WCF web service.


  
    
  
  
    
    
  
  
    
      
        
          
          
        
      
    
    
      
        
          
        
      
    
    
    
      
        
          
            
          
        
        
      
    
  
  
    
    
  



I add the behavior called " WcfService.DemoService " under service behaviors to register Demo Service behavior in http context. In this example I only used basic http bindings not use secure https bindings. Due to that reason need to set http binding to true. Set basichttpBindings with none security mode.
To avoid following error set " multipleSiteBindingsEnabled " to false. " When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true in configuration, the endpoints are required to specify a relative address. If you are specifying a relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://localhost:8080/DemoService.svc'. " Then finally add service end point like following.