SCM_Lecture06_Lab
SCM_Lecture06_Lab
```csharp
[ServiceContract]
public interface ICustomerService
{
[OperationContract]
string GetCustomerDetails(int customerId);
}
```
```csharp
public class CustomerService : ICustomerService
{
public string GetCustomerDetails(int customerId)
{
return $"Customer details for ID {customerId}";
}
}
```
```xml
<%@ ServiceHost Service="SOAPService.CustomerService" %>
```
```bash
dotnet new webapi -n RESTApi
```
```json
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=AdventureWorks;User
Id=sa;Password=YourPassword;"
}
```
```csharp
public class AdventureWorksDbContext : DbContext
{
public AdventureWorksDbContext(DbContextOptions<AdventureWorksDbContext>
options) : base(options) { }
public DbSet<Product> Products { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
```
```csharp
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly AdventureWorksDbContext _context;
[HttpGet("{id}")]
public async Task<IActionResult> GetProduct(int id)
{
var product = await _context.Products.FindAsync(id);
return product != null ? Ok(product) : NotFound();
}
}
```
```bash
dotnet add package Swashbuckle.AspNetCore
```
Configure in `Startup.cs`:
```csharp
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1"));
```
```bash
dotnet new grpc -n GrpcService
```
```proto
syntax = "proto3";
service ProductService {
rpc GetProductDetails (ProductRequest) returns (ProductResponse);
}
message ProductRequest {
int32 productId = 1;
}
message ProductResponse {
string productDetails = 1;
}
```
```csharp
public class ProductService : ProductService.ProductServiceBase
{
public override Task<ProductResponse> GetProductDetails(ProductRequest request,
ServerCallContext context)
{
return Task.FromResult(new ProductResponse
{
ProductDetails = $"Details for Product ID: {request.ProductId}"
});
}
}
```
```csharp
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<ProductService>();
});
```
```groovy
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'dotnet build SOAPService.sln'
sh 'dotnet build RESTApi.sln'
sh 'dotnet build GrpcService.sln'
}
}
stage('Test') {
steps {
sh 'dotnet test'
}
}
stage('Docker Build') {
steps {
sh 'docker-compose build'
}
}
stage('Deploy') {
steps {
sh 'docker-compose up -d'
}
}
}
}
```
```yaml
version: '3.8'
services:
soap:
build: ./SOAPService
ports:
- "8001:80"
rest:
build: ./RESTApi
ports:
- "8002:80"
grpc:
build: ./GrpcService
ports:
- "8003:80"
```