✅ Web Services in .NET
🔷 What is a Web Service?
A Web Service is a software application that:
- Is accessible over a network (such as the Internet)
- Uses standard protocols like HTTP and XML
- Allows interoperability between different systems and applications
- Can be consumed by clients written in any language or platform
✅ It helps different systems communicate and exchange data, even if they are built on different technologies.
🔷 Features of Web Services
| Feature |
Description |
| 🌐 Platform Independent |
Can be used across different platforms (Windows, Linux, etc.) |
| 🔤 Language Independent |
Can be created and consumed in any language (C#, Java, Python, etc.) |
| 🔁 Reusable |
Exposed functions can be reused by multiple clients |
| 🌍 Internet-Based |
Accessible over standard web protocols (HTTP) |
| 📤 Uses XML |
Data is exchanged using XML or SOAP format |
🔷 How Web Services Work
- Client sends a request to the web service using HTTP
- Web service processes the request
- Response is returned in XML or JSON format
🔷 Technologies Used
- SOAP (Simple Object Access Protocol) – XML-based messaging protocol
- WSDL (Web Services Description Language) – Describes what the service does
- UDDI (Universal Description, Discovery, and Integration) – Directory for listing services
✅ Creating a Simple Web Service in ASP.NET
✅ Step 1: Create .asmx File
Example: CalculatorService.asmx
<%@ WebService Language="C#" Class="CalculatorService" %>
✅ Step 2: Create the Web Service Code
using System.Web.Services;
[WebService(Namespace = "http://example.com/")]
public class CalculatorService : WebService
{
[WebMethod]
public int Add(int a, int b)
{
return a + b;
}
}
[WebMethod]: Exposes the method to external users
- Web service can now be accessed using its URL
🔷 Consuming a Web Service
- In Visual Studio, add a Web Reference
- Enter the
.asmx URL
- Call the method like a normal class:
CalculatorService service = new CalculatorService();
int result = service.Add(5, 3);
🔷 Advantages of Web Services
- 🔁 Reusability of business logic
- 🌐 Cross-platform compatibility
- 🔒 Secure communication (via HTTPS, SOAP headers)
- 📊 Useful for enterprise integration (ERP, CRM, etc.)
🔷 Limitations of Web Services
| Limitation |
Explanation |
| ❌ Slower Performance |
XML-based SOAP messages are bulky |
| ❌ Requires More Bandwidth |
Due to verbose XML format |
| ❌ Stateless |
No memory of previous client interactions |
✅ Summary
| Feature |
Detail |
| 🧱 Definition |
Application component exposed over network |
| 🔗 Protocols |
HTTP, SOAP, WSDL |
| 🧰 Example Use |
Calculator, Weather Service, Stock Prices |
| 🖥 Access |
Through URL or service reference |
| 🔐 Security |
Supports HTTPS, authentication |
✅ Windows Communication Foundation (WCF)
🔷 What is WCF?
Windows Communication Foundation (WCF) is a framework by Microsoft for building service-oriented applications.
WCF enables applications to send and receive data as messages, across different platforms and protocols (HTTP, TCP, MSMQ, etc.).
🔷 Why Use WCF?
| Benefit |
Description |
| 🔄 Interoperability |
Communicates across different systems and languages |
| 🔧 Multiple Protocols |
Supports HTTP, TCP, Named Pipes, MSMQ |
| 📦 Serialization |
Sends data in XML, JSON, binary formats |
| 🔐 Security |
Built-in support for encryption and authentication |
| 💡 Service Orientation |
Encourages loosely coupled services |
🔷 WCF vs Web Services (ASMX)
| Feature |
WCF |
Web Services (ASMX) |
| ✅ Protocol Support |
HTTP, TCP, MSMQ, etc. |
Only HTTP |
| ✅ Data Format |
XML, JSON, Binary |
Only XML (SOAP) |
| ✅ Security |
Advanced (WS-* standards) |
Basic (SSL, custom) |
| ✅ Hosting Options |
IIS, Windows Services, Self-hosted |
Only IIS |
🔷 WCF Architecture
WCF services use a contract-based architecture:
1. Service Contract
Defines what the service does.
[ServiceContract]
public interface ICalculator
{
[OperationContract]
int Add(int a, int b);
}
2. Service Implementation
public class CalculatorService : ICalculator
{
public int Add(int a, int b)
{
return a + b;
}
}
3. Hosting the Service
WCF services can be hosted in:
- ✅ IIS
- ✅ Windows Services
- ✅ Console applications (self-hosting)
Example (Console Hosting):
ServiceHost host = new ServiceHost(typeof(CalculatorService));
host.Open();
Console.WriteLine("Service is running...");
4. Endpoint Configuration
A WCF service has three core components, known as ABC:
| Part |
Description |
| A – Address |
Where the service is located (URL) |
| B – Binding |
How the service can be accessed (HTTP, TCP) |
| C – Contract |
What the service does (methods) |
🔷 Sample web.config for WCF
<system.serviceModel>
<services>
<service name="MyNamespace.CalculatorService">
<endpoint address="" binding="basicHttpBinding" contract="MyNamespace.ICalculator" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/CalcService"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
🔷 Built-in Bindings in WCF
| Binding |
Use Case |
basicHttpBinding |
SOAP over HTTP (like web services) |
netTcpBinding |
Fast TCP communication (intranet) |
wsHttpBinding |
Secure, reliable SOAP communication |
netNamedPipeBinding |
Local machine communication |
🔷 Hosting Options in WCF
| Hosting Type |
Description |
| IIS Hosting |
Works like ASP.NET Web Services |
| Self-Hosting |
Run inside a console or WinForms app |
| Windows Service |
Host as a long-running background app |
✅ Summary
| Feature |
Description |
| 📌 What is WCF? |
A framework to build service-based apps |
| 🔧 Core Parts |
Service, Contract, Endpoint, Binding, Address |
| 🌐 Protocols |
HTTP, TCP, MSMQ, Named Pipes |
| 🧩 Advanced Features |
Security, Reliability, Transactions |
| ✅ Replaces |
Traditional ASMX web services |
✅ Asynchronous JavaScript and XML (AJAX)
🔷 What is AJAX?
AJAX stands for:
Asynchronous JavaScript And XML
It is a web development technique used to create fast and dynamic web pages by exchanging small amounts of data with the server asynchronously, without refreshing the entire page.
🔷 Key Features of AJAX
| Feature |
Description |
| ⚡ Fast interactions |
Only updates part of the page, not the whole page |
| 🔁 Asynchronous calls |
Web page doesn’t freeze while waiting for the server response |
| 🧩 JavaScript-based |
Uses JavaScript and XMLHttpRequest or modern fetch() API |
| 📤 Data exchange |
Can send/receive XML, JSON, or plain text |
| 🌐 Server communication |
Works with HTTP requests/responses behind the scenes |
🔷 How AJAX Works – Workflow
- User action triggers JavaScript (e.g., button click)
- JavaScript sends an HTTP request to the server using
XMLHttpRequest or fetch()
- Server processes the request and sends back data
- JavaScript receives the data and updates the page dynamically
🔷 Technologies Involved in AJAX
| Component |
Role |
| HTML/XHTML |
Structure of the page |
| CSS |
Style and layout |
| JavaScript |
Client-side logic to send/receive requests |
| XML/JSON |
Format used to transfer data |
| DOM |
Allows JavaScript to update content dynamically |
| XMLHttpRequest |
Object used to communicate with the server |
✅ Basic Example using XMLHttpRequest
<button onclick="loadData()">Click Me</button>
<div id="output"></div>
<script>
function loadData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
document.getElementById("output").innerHTML = this.responseText;
}
};
xhttp.open("GET", "data.txt", true); // Asynchronous request
xhttp.send();
}
</script>
✅ Modern AJAX using fetch()
fetch('data.txt')
.then(response => response.text())
.then(data => {
document.getElementById("output").innerHTML = data;
});
🔷 Common Use Cases of AJAX
- ✅ Live search suggestions (like Google Search)
- ✅ Form validation without reloading
- ✅ Auto-refreshing news feeds
- ✅ Dynamic loading of user comments or data
- ✅ Pagination or infinite scrolling
🔷 Advantages of AJAX
| Advantage |
Description |
| ⚡ Faster performance |
Loads data without refreshing the whole page |
| ✅ Improved user experience |
Smooth and dynamic interactions |
| 🔁 Reduces bandwidth usage |
Transfers only required data |
🔷 Disadvantages of AJAX
| Disadvantage |
Description |
| ❌ SEO issues |
Search engines may not index AJAX-loaded content |
| ❌ JavaScript dependency |
Requires JavaScript to be enabled in the browser |
| ❌ Complex debugging |
Harder to trace dynamic errors in the browser |
✅ Summary
| Term |
Description |
| 💡 AJAX |
Tech to update web pages asynchronously using JavaScript |
| 🔄 Core |
JavaScript + XMLHttpRequest or fetch() + JSON/XML |
| 🧠 Benefit |
No full-page reload, better performance & UX |
| ⚠️ Limitation |
Not ideal for SEO-heavy sites without fallback |
✅ ASP.NET and WPF (Windows Presentation Foundation)
Part 1: ✅ ASP.NET Overview
🔷 What is ASP.NET?
ASP.NET is a web application framework developed by Microsoft to build dynamic websites, web applications, and web services using .NET languages like C# or VB.NET.
🔷 Features of ASP.NET
| Feature |
Description |
| 🌐 Server-Side |
Code runs on the server and sends HTML to the browser |
| 🔧 Rich Toolbox |
Built-in controls (GridView, TextBox, Button, etc.) |
| 🎯 Event-Driven |
Uses events like Click, Load, Init, etc. |
| 🔒 Built-in Security |
Authentication and authorization support |
| 🧱 Code Behind |
Separates HTML (design) and code (logic) |
🔷 ASP.NET Page Life Cycle
- Page Request
- Start
- Initialization
- Load
- Postback Event Handling
- Rendering
- Unload
🔷 Common ASP.NET Controls
| Control |
Purpose |
| TextBox |
User input |
| Button |
Triggers events |
| DropDownList |
Drop-down menu |
| GridView |
Display tabular data |
| Label |
Display text |
🔷 Hosting ASP.NET Apps
- ✅ IIS (Internet Information Services)
- ✅ Azure Web Apps
- ✅ Self-hosted using Kestrel (.NET Core)
Part 2: ✅ WPF (Windows Presentation Foundation)
🔷 What is WPF?
WPF is a desktop UI framework for building Windows-based applications with rich graphics, animations, and styles, using XAML (Extensible Application Markup Language) and C#.
🔷 Features of WPF
| Feature |
Description |
| 🎨 Rich UI |
Support for vector graphics, styles, themes |
| 🧩 Separation of Concerns |
XAML for design, C# for logic |
| 📐 Layout Management |
Controls like StackPanel, Grid, Canvas |
| 📦 Data Binding |
Binds UI controls to data sources automatically |
| 🔄 Event-Driven |
Handles user input with event handlers |
🔷 XAML Basics
<Button Content="Click Me" Width="100" Height="30" Click="Button_Click"/>
Content – Text shown on button
Click – Event handler in code-behind
🔷 Common WPF Controls
| Control |
Description |
| Button |
Clickable action |
| TextBox |
Input field |
| Label |
Display static text |
| ListBox |
List of selectable items |
| DataGrid |
Table for displaying data |
🔷 WPF Layout Panels
| Panel |
Description |
| StackPanel |
Stacks controls vertically/horizontally |
| Grid |
Divides layout into rows and columns |
| Canvas |
Absolute positioning |
| DockPanel |
Aligns elements to left, right, top, bottom |
🔷 Data Binding Example
<TextBox Text="{Binding Name}" />
public class Person {
public string Name { get; set; }
}
| Feature |
WPF |
WinForms |
ASP.NET |
| UI Type |
Desktop (modern) |
Desktop (basic) |
Web |
| Markup Used |
XAML |
No |
HTML, ASP.NET markup |
| Styling |
Powerful templates |
Limited |
CSS + Themes |
| Deployment |
EXE, MSIX |
EXE |
IIS, Web Server |
✅ Summary Table
| Framework |
Type |
Language |
Markup |
Use Case |
| ASP.NET |
Web |
C#/VB |
HTML |
Web apps, APIs |
| WPF |
Desktop |
C#/VB |
XAML |
Windows desktop apps |