0% found this document useful (0 votes)
24 views

SCM_Lecture06_Lab

Uploaded by

merola.g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

SCM_Lecture06_Lab

Uploaded by

merola.g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Complete SOAP, REST, and gRPC API Implementation Steps

1. SOAP API Implementation

Step 1: Create a New WCF Project


In Visual Studio, go to File → New → Project. Choose 'WCF Service Library' and name it
SOAPService.

Step 2: Define the Service Contract


Add an interface `ICustomerService.cs`:

```csharp
[ServiceContract]
public interface ICustomerService
{
[OperationContract]
string GetCustomerDetails(int customerId);
}
```

Step 3: Implement the Service


Add `CustomerService.cs`:

```csharp
public class CustomerService : ICustomerService
{
public string GetCustomerDetails(int customerId)
{
return $"Customer details for ID {customerId}";
}
}
```

Step 4: Host the Service


Add `Service.svc`:

```xml
<%@ ServiceHost Service="SOAPService.CustomerService" %>
```

Step 5: Configure Web.config


Add the following to `Web.config`:
```xml
<system.serviceModel>
<services>
<service name="SOAPService.CustomerService">
<endpoint address="" binding="basicHttpBinding"
contract="SOAPService.ICustomerService" />
</service>
</services>
</system.serviceModel>
```

Step 6: Test the Service


Run the service in Visual Studio. Access the WSDL at `http://localhost:<port>/Service.svc?
wsdl`. Use SoapUI to import the WSDL and test the service.

2. REST API Implementation

Step 1: Create a New ASP.NET Core Web API Project


Run the following command or create the project in Visual Studio:

```bash
dotnet new webapi -n RESTApi
```

Step 2: Configure the Database Connection


Update `appsettings.json`:

```json
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=AdventureWorks;User
Id=sa;Password=YourPassword;"
}
```

Step 3: Create Database Context and Models


Add `AdventureWorksDbContext`:

```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; }
}
```

Step 4: Create the Controller


Add `ProductsController.cs`:

```csharp
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly AdventureWorksDbContext _context;

public ProductsController(AdventureWorksDbContext context)


{
_context = context;
}

[HttpGet("{id}")]
public async Task<IActionResult> GetProduct(int id)
{
var product = await _context.Products.FindAsync(id);
return product != null ? Ok(product) : NotFound();
}
}
```

Step 5: Add Swagger for API Documentation


Add `Swashbuckle`:

```bash
dotnet add package Swashbuckle.AspNetCore
```

Configure in `Startup.cs`:

```csharp
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1"));
```

Step 6: Test the API


Run the project and test endpoints using Postman or Swagger UI at `/swagger/index.html`.

3. gRPC API Implementation

Step 1: Create a New gRPC Project


Run the following command:

```bash
dotnet new grpc -n GrpcService
```

Step 2: Define the Protobuf File


Create `product.proto`:

```proto
syntax = "proto3";

service ProductService {
rpc GetProductDetails (ProductRequest) returns (ProductResponse);
}

message ProductRequest {
int32 productId = 1;
}

message ProductResponse {
string productDetails = 1;
}
```

Step 3: Implement the gRPC Service


Add `ProductService.cs`:

```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}"
});
}
}
```

Step 4: Configure gRPC in Startup.cs


Add the gRPC middleware:

```csharp
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<ProductService>();
});
```

Step 5: Test the gRPC API


Use a gRPC client like BloomRPC or grpcurl to test the service.

4. CI/CD Pipeline with Jenkins


Add a `Jenkinsfile`:

```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'
}
}
}
}
```

Add services in `docker-compose.yml`:

```yaml
version: '3.8'
services:
soap:
build: ./SOAPService
ports:
- "8001:80"

rest:
build: ./RESTApi
ports:
- "8002:80"

grpc:
build: ./GrpcService
ports:
- "8003:80"
```

You might also like