C# .
NET Web Services Exam Preparation Guide
1. Creating Web Services in C# .NET
Definition:
Web services allow applications to communicate with each other over
the web, using standard protocols like HTTP, XML, and SOAP.
Steps to Create a Web Service:
1. Create a New ASP.NET Web Application in Visual Studio.
2. Add a Web Service (ASMX) File:
- Right-click the project > Add New Item > Web Service (ASMX).
3. Define Methods:
- Use [WebMethod] attribute to expose methods as web services.
4. Build and Run:
- Access the service via the auto-generated WSDL file.
Code Example:
[WebService(Namespace = "http://example.org/")]
public class MyWebService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello, World!";
}
}
Hosting:
Host using IIS or self-host within a .NET Core Web API.
2. Extending Web Services in C# .NET
Enhancements Through:
1. Additional Features:
- Add more [WebMethod] functions.
2. Advanced Data Handling:
- Support complex data types using DataContractSerializer or JSON
serialization.
3. Versioning:
- Create new endpoints to handle backward compatibility.
4. Integration with Databases:
- Extend services to fetch/store data from a database using
ADO.NET or Entity Framework.
5. Service Composition:
- Integrate with other web services for enhanced functionality.
Example:
Extending the service to fetch data from a database.
[WebMethod]
public List<string> GetEmployeeNames()
{
// Fetch names from a database
return new List<string> { "Alice", "Bob", "Charlie" };
}
3. Messaging Protocols in Web Services
SOAP (Simple Object Access Protocol):
- XML-based protocol.
- Used for structured messaging.
- Provides a standard for security and transactions.
- Example: WSDL describes the service.
REST (Representational State Transfer):
- Lightweight, uses JSON or XML.
- HTTP Methods: GET, POST, PUT, DELETE.
- No strict standards like SOAP.
HTTP/HTTPS:
- Core transport protocol.
- Enables secure communication using SSL/TLS.
Examples in .NET:
// RESTful service
[HttpGet]
public IActionResult GetData()
{
return Ok(new { Id = 1, Name = "John Doe" });
}
4. Describing Web Services
WSDL (Web Services Description Language):
- XML-based standard to describe web services.
- Includes service location, operations, input/output formats.
Example WSDL Elements:
1. <types>: Data type definitions.
2. <message>: Information about data communication.
3. <portType>: Operations supported.
4. <binding>: Protocol details.
Access WSDL in .NET:
When running a service in Visual Studio, append ?WSDL to the
service URL.
5. Discovering Web Services
UDDI (Universal Description, Discovery, and Integration):
- A registry for web services.
- Helps clients discover available services.
Discovery in .NET:
- Dynamic Discovery:
Use the DISCO (Discovery) protocol to locate available web services.
Example:
// Add Service Reference in Visual Studio
// Use the generated proxy class to consume the service
MyWebService proxy = new MyWebService();
var result = proxy.HelloWorld();
6. Securing Web Services
Authentication:
1. Basic Authentication:
Use HTTP headers to send username and password.
2. Token-Based Authentication:
Use OAuth2 or JWT (JSON Web Tokens).
Encryption:
- Use SSL/TLS for secure communication (HTTPS).
Message Security in SOAP:
- WS-Security adds security headers to SOAP messages.
Example of Securing REST API in .NET Core:
[Authorize]
[HttpGet]
public IActionResult GetSecureData()
{
return Ok("Secure data accessed.");
}
Best Practices:
- Use HTTPS for all endpoints.
- Validate inputs to avoid injection attacks.
- Regularly update libraries and dependencies.