Introduction To ASP.NET Web Services
Technologies related to Web Services
HTTP Technology
Looking at the HTTP
Note: Fiddler is excellent tool which can be used to trace the HTTP communication over the network.
XML Technology
Note: XML is basic format medium to communicate over the network to other systems
SOAP Technology
Looking at the SOAP
Note: This is common Request and Response format for SOAP request.
WSDL Technology
Looking at the WSDL
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
Hello World for Web Services
Creating A Simple Web Service
Creating the Projects
Creating Add Protein method
Web Method parameters
Enabling Session
Add User
Creating Add User
Adding List Users
Fixing Add protein
The Repository Pattern
Using the Repository Pattern
Testing Our Web Services
Consuming Web Services
Creating Project for Web Service Client
Creating UI
Adding a Service Reference
Calling List Users
Adding a User
Updating total and Goal
Calling Add Protein
Adding Delay and use New Async in Web Services
Note: If you use Async and Await keyword then Web services call will not block the UI thread.
Old Async Pattern
Exceptions in Web Services
Custom SOAP header
Manually Generating Proxy Classes
Note: This WSDL tool will generate the proxy classes for our Web services Client call.
Web Services and AJAX
Setting up the project
Creating Select UI
How AJAX Works
Getting ready to call web Services
Populating Select List
Testing the UI dummy Services
Using Real Web Services
Adding New User UI
Implementing Add New User
Using Page Methods
Using JQuery for Web Services Call
Migrating to Newer Technologies
Technology Choices
Preparing for WCF
Creating WCF Services
Running WCF
Creating the WEB API Project
Creating USER Controller
Adding Methods
Adding GET and POST
GET
POST
Testing the API
Implementing PUT
Introducing WCF
Difference between WCF and Web service
Sample SOAP message
Introducing WCF
WCF Architecture
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.
- Error Behavior - Specifies what occurs, when internal error occurs on the service.
- 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 ChannelsHandles sending and receiving message from network. Protocols like HTTP, TCP, name pipes and MSMQ.
- Protocol ChannelsImplements 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
- IISInternet 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-HostingWCF service can be self hosted as console application, Win Forms or WPF application with graphical UI.
- Windows ServiceWCF can also be hosted as a Windows Service, so that it is under control of the Service Control Manager (SCM).
WCF Experience
Demo of Simple WCF services Host
Services and End Points
Some Build in Bindings
Demo of Simple WCF Client application
Programming Services
WCF Service Model Architecture
Contracts
Define Data Contract
Define Service Contracts
Message Contract
MessageHeaderArray Attribute
Message Contract Properties
Fault Contract
Instancing and Threading
Instance Deactivation
Durable Service
Throttling
Programming Model
Operation Services on WCF
WCF Hosting Services
Address and Bindings
WCF Address
WCF Bindings
Types of Bindings
Metadata Exchange
Service Exceptions
Transfer mode
Events
Transaction
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.
Transaction Propagation
Transaction Protocols
Transaction Mode
How to Create WCF Transaction
Demo of SELF Hosting Services
Demo of IIS Hosting Services
Windows Activation Service
Windows Service Hosting
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
Demo of Using ServiceUtil.exe
This is one way to get the WSDL information via IE
This is another way to get WSDL information via ServiceUtil with META data
This is another way to get WSDL information via ServiceUtil with MEX protocol
Demo of using Service Reference
Programming WCF Channels
Demo of Creating, Using, and Closing Channels
Demo of Using Proxy Class
Handling Exceptions at Client Side
Demo of Handling Exceptions
Client Code:
Service Code:
Demo of Using Asynchronous
Demo of Sharing Service Contract Assemblies
Demo of Using Meta Data Resolver
RESTful Services
Introduction to RESTful service
Demo of REST-ful Service implementation
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();
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(); }
JSON using WCF service
[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); }
No comments:
Post a Comment