MS Web Service & WCF



Introduction To ASP.NET Web Services


image



image




image
image




image



Technologies related to Web Services


image




HTTP Technology

image




Looking at the HTTP

image


Note: Fiddler is excellent tool which can be used to trace the HTTP communication over the network.



XML Technology


image

Note: XML is basic format medium to communicate over the network to other systems




SOAP Technology

image



Looking at the SOAP

image


Note:  This is common Request and Response format for SOAP request.



WSDL Technology


image




Looking at the WSDL

image


Note: This WSDL file is kind of MENU for our web services to work with.  Basically it defines all the methods and communication mechanism for your clients who calls this services.



How it works


image




Hello World for Web Services


image




image



image



image



image




Creating A Simple Web Service


image




Creating the Projects

image



image




image




image



image



image



image



image




Creating Add Protein method

image



image



image




Web Method parameters

image


image



Enabling Session
image


image


 
Add User

image



Creating Add User

image


image



image




Adding List Users
image


image


Fixing Add protein
image



The Repository Pattern

image
image




Using the Repository Pattern

image



image




Testing Our Web Services
image


image




Consuming Web Services


image



Creating Project for Web Service Client

image



Creating UI

image



image




Adding a Service Reference

image



image



Calling List Users

image



image



Adding a User

image



image



image




Updating total and Goal

image



image



Calling Add Protein

image




Adding Delay and use New Async in Web Services

image



image



image



image


Note:  If you use Async and Await keyword then Web services call will not block the UI thread.



Old Async Pattern

image


image




Exceptions in Web Services

image




image




image




Custom SOAP header


image




image



image



image




Manually Generating Proxy Classes

image



image




image


Note:  This WSDL tool will generate the proxy classes for our Web services Client call.



Web Services and AJAX


image




Setting up the project

image



image




Creating Select UI

image


image




How AJAX Works

image




Getting ready to call web Services
image



image



image



Populating Select List
image




Testing the UI dummy Services

image


image



image



Using Real Web Services

image




image




Adding New User UI

image



image



Implementing Add New User

image



image




Using Page Methods

image



image



image



image




Using JQuery for Web Services Call

image



image




Migrating to Newer Technologies



image




Technology Choices

image




Preparing for WCF

image



image



image



image




Creating WCF Services

image



image


image




Running WCF

image



image



image




Creating the WEB API Project


image



image



image




image




image




Creating USER Controller

image



image




Adding Methods

image



Adding GET and POST

GET
image


POST
image



Testing the API

image



image



Implementing PUT

image



image










 

Introducing WCF

image


Difference between WCF and Web service
image


image



image



image



image



image



image


Sample SOAP message
image




image



image




image




image



Introducing WCF

image



image



WCF Architecture
image

Contracts
Contracts layer are next to that of Application layer. Developer will directly use this contract to develop the service. We are also going to do the same now. Let us see briefly what these contracts will do for us and we will also know that WCF is working on message system.
Service contracts
- Describe about the operation that service can provide. Example, Service provided to know the temperature of the city based on the zip code, this service we call as Service contract. It will be created using Service and Operational Contract attribute.
Data contract
- It describes the custom data type which is exposed to the client. This defines the data types, are passed to and from service. Data types like int, string are identified by the client because it is already mention in XML schema definition language document, but custom created class or datatype cannot be identified by the client e.g. Employee data type. By using DataContract we can make client aware that we are using Employee data type for returning or passing parameter to the method.
Message Contract
- Default SOAP message format is provided by the WCF runtime for communication between Client and service. If it is not meeting your requirements then we can create our own message format. This can be achieved by using Message Contract attribute.
Policies and Binding
- Specify conditions required to communicate with a service e.g security requirement to communicate with service, protocol and encoding used for binding.

Service Runtime
- It contains the behaviors that occur during runtime of service.
  • Throttling Behavior- Controls how many messages are processed.
  • Error Behavior - Specifies what occurs, when internal error occurs on the service.
  • Metadata Behavior - Tells how and whether metadata is available to outside world.
  • Instance Behavior - Specifies how many instance of the service has to be created while running.
  • Transaction Behavior - Enables the rollback of transacted operations if a failure occurs.
  • Dispatch Behavior - Controls how a message is processed by the WCF Infrastructure.
Messaging
- Messaging layer is composed of channels. A channel is a component that processes a message in some way, for example, by authenticating a message. A set of channels is also known as a channel stack. Channels are the core abstraction for sending message to and receiving message from an Endpoint. Broadly we can categories channels as
 
  • Transport Channels
    Handles sending and receiving message from network. Protocols like HTTP, TCP, name pipes and MSMQ.
  • Protocol Channels
    Implements SOAP based protocol by processing and possibly modifying message. E.g. WS-Security and WS-Reliability.
Activation and Hosting
- Services can be hosted or executed, so that it will be available to everyone accessing from the client. WCF service can be hosted by following mechanism
  • IIS
    Internet information Service provides number of advantages if a Service uses Http as protocol. It does not require Host code to activate the service, it automatically activates service code.
  • Windows Activation Service
    (WAS) is the new process activation mechanism that ships with IIS 7.0. In addition to HTTP based communication, WCF can also use WAS to provide message-based activation over other protocols, such as TCP and named pipes.
  • Self-Hosting
    WCF service can be self hosted as console application, Win Forms or WPF application with graphical UI.
  • Windows Service
    WCF can also be hosted as a Windows Service, so that it is under control of the Service Control Manager (SCM).

WCF Experience


image



image



image




Demo of Simple WCF services Host

image



image



image



image



image



image


image



image



image



Services and End Points

image




image



Some Build in Bindings

image



image



image



Demo of Simple WCF Client application

image




image


image


image


image



image




image





image



image



image




image



image





Programming Services


image



WCF Service Model Architecture

image



image



image




image




Contracts

image

 
Define Data Contract

image

image

image


image



Define Service Contracts
image

image


image



image



Message Contract
image

image

image


MessageHeaderArray Attribute
image


Message Contract Properties
image


image

image


Fault Contract
image

image

image

image

image


Instancing and Threading

image


image


image


image

image


image

image



image


image

image



image

image

image



Instance Deactivation
image

image

image

image



Durable Service
image

image



Throttling
image


image

Programming Model

image



Operation Services on WCF
image


image



image

image

image


image

image

image



WCF Hosting Services
image

image


image



image



image



image


image



image


image


image


image



Address and Bindings

WCF Address
image



WCF Bindings
image

image


Types of Bindings
image

image


image


image


image

image



image


image


image


image



image



Metadata Exchange
image



image



image


image




Service Exceptions

image



image



image



Transfer mode
image
image
image


Events
image

image

image

image


image


image

image

image

image



Transaction
image

image


Two-phase committed protocol

Consider the scenario where I am having single client which use single service for communication and interacting with single database. In which service starts and manage the transaction, now it will be easy for the service to manage the transaction.
Consider for example client calling multiple service or service itself calling another service, this type of system are called as Distributed Service-oriented application. Now the questions arise that which service will begin the transaction? Which service will take responsibility of committing the transaction? How would one service know what the rest of the service feels about the transaction? Service could also be deployed in different machine and site. Any network failure or machine crash also increases the complexity for managing the transaction.

image

image



Transaction Propagation
image

image



Transaction Protocols
image


Transaction Mode
image

image


How to Create WCF Transaction

image

image


image


image

image


image


Demo of SELF Hosting Services
image

image

image


image

image

image


image


image

image



Demo of IIS Hosting Services
image

image


image

image

image


image

image

image

image



Windows Activation Service

image

image


image


image

image

image


image


image


image



Windows Service Hosting
image


image


image


image


image


image

image

image

image

image

Step 11: Now service is Hosted sucessfully and we can create the proxy class for the service and start using in the client applcaiton.







 

Programming Clients

image


image



image



image



image



Demo of Using ServiceUtil.exe
image


This is one way to get the WSDL information via IE

image


This is another way to get WSDL information via ServiceUtil with META data

image


image



This is another way to get WSDL information via ServiceUtil with MEX protocol

image


image


image


image



Demo of using Service Reference

image


image


image



image



image


image




Programming WCF Channels

image



image



image


image



image



image



image



Demo of Creating, Using, and Closing Channels

image


image


image



image



Demo of Using Proxy Class

image




image



image



image



Handling Exceptions at Client Side

image



image



Demo of Handling Exceptions

Client Code:

image

image

Service Code:

image


image


image



Demo of Using Asynchronous
image


image


image


image



image



Demo of Sharing Service Contract Assemblies

image


image



Demo of Using Meta Data Resolver

image


image



RESTful Services


Introduction to RESTful service
image

image


image


image



image



image



image



image


image



image


image


image



image



image



image



image



image



image



image



Demo of REST-ful Service implementation
image

image

image

image



image
image
image


image


image

Uri httpUrl = new Uri("http://localhost:8090/MyService/EmployeeService");
                WebServiceHost host = new WebServiceHost(typeof(MyFirstRESTfulService.EmployeeService), httpUrl);
                host.Open();
            
                foreach (ServiceEndpoint se in host.Description.Endpoints)
                        Console.WriteLine("Service is host with endpoint " + se.Address);
                //Console.WriteLine("ASP.Net : " + ServiceHostingEnvironment.AspNetCompatibilityEnabled);
                Console.WriteLine("Host is running... Press < Enter > key to stop");
                Console.ReadLine();





image



image





Step 8:Now we can start with client application. For this example we can create a console application to read the employee information and add new employee to the server resource. Below code first read the employee details from the server and add new employee and once again it read the employee details to confirm the added resource.



WebChannelFactory < IEmployeeService > cf =
               new WebChannelFactory< IEmployeeService >(
                   new Uri("http://localhost:8090/MyService/EmployeeService"));
            IEmployeeService client = cf.CreateChannel();
            var d = client.GetEmployee(1);
            //Load all the Employee from the server and display
           foreach (Employee e in   client.GetAllEmployeeDetails() )
           {
           Console.WriteLine(string.Format("EmpID:{0}, Name:{1} {2}",e.EmpId ,e.Fname ,e.Lname ));
           }

            //Add new user
           client.AddEmployee(new Employee() { EmpId = 11, Fname = "John", Lname = "J", JoinDate = new DateTime(2010, 7, 24), Age = 34, Salary = 10000, Designation = "Software Engineer" });
        
            Console.WriteLine("******************After adding new user ****************");

        //Load all the Employee from the server and display
         foreach (Employee e in   client.GetAllEmployeeDetails() )
           {
           Console.WriteLine(string.Format("EmpID:{0}, Name:{1} {2}",e.EmpId ,e.Fname ,e.Lname ));
           }
            Console.ReadLine();
        }



image





JSON using WCF service

image






[ServiceContract()]
    public interface IEmployeeService
    {
        [WebGet(UriTemplate = "Employee", ResponseFormat=WebMessageFormat.Json )]
        [OperationContract]
        List < Employee > GetAllEmployeeDetails();

        [WebGet(UriTemplate = "Employee?id={id}", ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        Employee GetEmployee(int Id);

        [WebInvoke(Method = "POST", UriTemplate = "EmployeePOST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        [OperationContract]
        void AddEmployee(Employee newEmp);

        [WebInvoke(Method = "PUT", UriTemplate = "EmployeePUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        [OperationContract]
        void UpdateEmployee(Employee newEmp);

        [WebInvoke(Method = "DELETE", UriTemplate = "Employee/{empId}", ResponseFormat = WebMessageFormat.Json)]
        [OperationContract]
        void DeleteEmployee(string empId);
    }





image





image





image





image







WEB Operation Context



image







Syndication Programming Model



image





image







AJAX Integration via JSON



image





image





image





image







image







image







image





image





image







image









WCF Security


image





image





Security Mode

image



image



image





Transport Security Protection Level

image



image





image







WCF Service Impersonation


image







WCF Windows Authentication


image



image



image







What is WCF RIA service?


image





image



image





image



image



image







How to Create WCF RIA Service

image





image





image



image





image





image





image



image



image





image





What's new in WCF 4.5 - Part 4




image



image




No comments:

Post a Comment