Skip to content

Commit c8b1558

Browse files
author
RahulMule
committed
Adding Azure Service Bus Connectivity
1 parent 17dcd3c commit c8b1558

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

DotNetCore-WebAPI-AzureRedisCache/Controllers/ProductsController.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DotNetCore_WebAPI_AzureRedisCache.IRepository;
22
using DotNetCore_WebAPI_AzureRedisCache.Models;
33
using DotNetCore_WebAPI_AzureRedisCache.Repository;
4+
using DotNetCore_WebAPI_AzureRedisCache.Services;
45
using Microsoft.AspNetCore.Http;
56
using Microsoft.AspNetCore.Mvc;
67

@@ -12,10 +13,12 @@ public class ProductsController : ControllerBase
1213
{
1314
IProduct _product;
1415
ICache _cache;
15-
public ProductsController(IProduct product, ICache cache)
16+
AzureServiceBusService _azureservicebusservice;
17+
public ProductsController(IProduct product, ICache cache, AzureServiceBusService service)
1618
{
1719
_product = product;
1820
_cache = cache;
21+
_azureservicebusservice = service;
1922
}
2023
[HttpGet]
2124
public async Task<IActionResult> GetProducts()
@@ -34,9 +37,11 @@ public async Task<IActionResult> GetProducts()
3437
return Ok(products) ;
3538
}
3639
[HttpPost]
37-
public Product AddProducts(Product product)
40+
public async Task<ActionResult> AddProducts(Product product)
3841
{
39-
return _product.AddProduct(product);
42+
var response = _product.AddProduct(product);
43+
await _azureservicebusservice.SendProductToQueueAsync(product);
44+
return Ok(response);
4045
}
4146

4247
[HttpGet("{id}")]

DotNetCore-WebAPI-AzureRedisCache/DotNetCore-WebAPI-AzureRedisCache.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.17.3" />
1112
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
13+
<PackageReference Include="Microsoft.Azure.ServiceBus" Version="5.2.0" />
1214
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.21" />
1315
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.21" />
1416
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />

DotNetCore-WebAPI-AzureRedisCache/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DotNetCore_WebAPI_AzureRedisCache.IRepository;
22
using DotNetCore_WebAPI_AzureRedisCache.ProductContext;
33
using DotNetCore_WebAPI_AzureRedisCache.Repository;
4+
using DotNetCore_WebAPI_AzureRedisCache.Services;
45
using Microsoft.EntityFrameworkCore;
56
using StackExchange.Redis;
67

@@ -17,6 +18,8 @@
1718
options.UseInMemoryDatabase("Products"));
1819
builder.Services.AddScoped<IProduct,ProductRepository>();
1920
builder.Services.AddScoped<ICache, AzureRedisCache>();
21+
string connstring = builder.Configuration.GetConnectionString("asb");
22+
builder.Services.AddSingleton(new AzureServiceBusService(connstring, "products"));
2023

2124
builder.Services.AddSwaggerGen();
2225

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.Azure.ServiceBus;
2+
using Newtonsoft.Json;
3+
using System.Text;
4+
using System.Text.Json.Serialization;
5+
6+
namespace DotNetCore_WebAPI_AzureRedisCache.Services
7+
{
8+
public class AzureServiceBusService
9+
{
10+
private readonly string _connectionstring;
11+
private readonly string _queuename;
12+
public AzureServiceBusService(string connstring, string queuename)
13+
{
14+
_connectionstring = connstring;
15+
_queuename = queuename;
16+
}
17+
public async Task SendProductToQueueAsync<T>(T messageobject)
18+
{
19+
IQueueClient queueClient = new QueueClient(_connectionstring, _queuename);
20+
string jsonmessage = JsonConvert.SerializeObject(messageobject);
21+
var message = new Message(Encoding.UTF8.GetBytes(jsonmessage));
22+
await queueClient.SendAsync(message);
23+
await queueClient.CloseAsync();
24+
}
25+
}
26+
}

DotNetCore-WebAPI-AzureRedisCache/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"AllowedHosts": "*",
99
"ConnectionStrings": {
10-
"connstring": "sdasda.redis.cache.windows.net:6380,password=QKieBdeqRuZaXc83MlbYFGLCz88YDYg9pAzCaEgKYXQ=,ssl=True,abortConnect=False"
10+
"connstring": "sdasda.redis.cache.windows.net:6380,password=QKieBdeqRuZaXc83MlbYFGLCz88YDYg9pAzCaEgKYXQ=,ssl=True,abortConnect=False",
11+
"asb": "Endpoint=sb://duausdudasda.servicebus.windows.net/;SharedAccessKeyName=sda;SharedAccessKey=xx7xQGWv72lqePuj2mFK8yEC0u6P/P2or+ASbAvwKw8=;"
1112
}
1213
}

0 commit comments

Comments
 (0)