forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update LineCounter to .NET 7 and use OTel instead of AppInsights (Azu…
…re#39913) * Update LineCounter to .NET and use OTel instead of AppInsights
- Loading branch information
Showing
16 changed files
with
406 additions
and
317 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,69 @@ | ||
| ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Text; | ||
using Azure.Messaging; | ||
using Azure.Messaging.EventGrid; | ||
using Azure.Messaging.EventHubs; | ||
using Azure.Messaging.EventHubs.Producer; | ||
using Azure.Storage.Blobs; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Azure; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace LineCounter.Controllers | ||
namespace LineCounter.Controllers; | ||
|
||
[Route("/")] | ||
public class HomeController : Controller | ||
{ | ||
[Route("/")] | ||
public class HomeController : Controller | ||
private readonly ILogger<HomeController> _logger; | ||
private readonly BlobContainerClient _blobContainerClient; | ||
private readonly EventHubProducerClient _uploadsProducer; | ||
private readonly EventGridPublisherClient _publisherClient; | ||
|
||
public HomeController( | ||
ILogger<HomeController> logger, | ||
BlobServiceClient blobServiceClient, | ||
IAzureClientFactory<EventHubProducerClient> clientFactory, | ||
EventGridPublisherClient publisherClient) | ||
{ | ||
private readonly ILogger<HomeController> _logger; | ||
private readonly BlobContainerClient _blobContainerClient; | ||
private readonly EventHubProducerClient _uploadsProducer; | ||
private readonly EventGridPublisherClient _publisherClient; | ||
_logger = logger; | ||
_blobContainerClient = blobServiceClient.GetBlobContainerClient("uploads"); | ||
_uploadsProducer = clientFactory.CreateClient("Uploads"); | ||
_publisherClient = publisherClient; | ||
} | ||
|
||
public HomeController( | ||
ILogger<HomeController> logger, | ||
BlobServiceClient blobServiceClient, | ||
IAzureClientFactory<EventHubProducerClient> clientFactory, | ||
EventGridPublisherClient publisherClient) | ||
{ | ||
_logger = logger; | ||
_blobContainerClient = blobServiceClient.GetBlobContainerClient("uploads"); | ||
_uploadsProducer = clientFactory.CreateClient("Uploads"); | ||
_publisherClient = publisherClient; | ||
} | ||
public IActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
|
||
public IActionResult Index() | ||
{ | ||
return View(); | ||
} | ||
[HttpPost("upload")] | ||
public async Task<IActionResult> Upload() | ||
{ | ||
var names = new List<string>(); | ||
|
||
[HttpPost("upload")] | ||
public async Task<IActionResult> Upload() | ||
foreach (var file in HttpContext.Request.Form.Files) | ||
{ | ||
var names = new List<string>(); | ||
|
||
foreach (var file in HttpContext.Request.Form.Files) | ||
{ | ||
var fileName = Guid.NewGuid().ToString("n") + " " + file.FileName; | ||
var fileName = Guid.NewGuid().ToString("n") + " " + file.FileName; | ||
|
||
var stream = file.OpenReadStream(); | ||
_logger.LogInformation("Uploading {fileName}", fileName); | ||
await _blobContainerClient.CreateIfNotExistsAsync(); | ||
await _blobContainerClient.UploadBlobAsync(fileName, stream); | ||
var stream = file.OpenReadStream(); | ||
_logger.LogInformation("Uploading {fileName}", fileName); | ||
await _blobContainerClient.CreateIfNotExistsAsync(); | ||
await _blobContainerClient.UploadBlobAsync(fileName, stream); | ||
|
||
using EventDataBatch eventBatch = await _uploadsProducer.CreateBatchAsync(); | ||
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(fileName))); | ||
await _uploadsProducer.SendAsync(eventBatch); | ||
using EventDataBatch eventBatch = await _uploadsProducer.CreateBatchAsync(); | ||
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(fileName))); | ||
await _uploadsProducer.SendAsync(eventBatch); | ||
|
||
names.Add(fileName); | ||
} | ||
|
||
return View("Index", names.ToArray()); | ||
names.Add(fileName); | ||
} | ||
|
||
[HttpGet("status/{name}.html")] | ||
public async Task<string> Status(string name) | ||
{ | ||
var properties = await _blobContainerClient.GetBlobClient(name).GetPropertiesAsync(); | ||
properties.Value.Metadata.TryGetValue("whitespacecount", out var count); | ||
await _publisherClient.SendEventsAsync(new CloudEvent[] { new CloudEvent("https://www.contoso.com/LineCounter", "LineCounter.Status.Viewed", name) }); | ||
return count ?? "-1"; | ||
} | ||
return View("Index", names.ToArray()); | ||
} | ||
|
||
[HttpGet("status/{name}.html")] | ||
public async Task<string> Status(string name) | ||
{ | ||
var properties = await _blobContainerClient.GetBlobClient(name).GetPropertiesAsync(); | ||
properties.Value.Metadata.TryGetValue("whitespacecount", out var count); | ||
await _publisherClient.SendEventsAsync(new CloudEvent[] { new CloudEvent("https://www.contoso.com/LineCounter", "LineCounter.Status.Viewed", name) }); | ||
return count ?? "-1"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<UserSecretsId>67ca5d37-051b-4ab1-b013-c74a2d4edb76</UserSecretsId> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.2.0" /> | ||
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.4.1" /> | ||
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.4.1" /> | ||
<PackageReference Include="Azure.Storage.Blobs" Version="12.13.0" /> | ||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.18.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.0.0" /> | ||
<PackageReference Include="System.Text.Encodings.Web" Version="4.7.2" /> | ||
<PackageReference Include="Azure.Messaging.EventGrid" Version="4.21.0" /> | ||
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.10.0" /> | ||
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.10.0" /> | ||
<PackageReference Include="Azure.Storage.Blobs" Version="12.19.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.1" /> | ||
<PackageReference Include="System.Text.Encodings.Web" Version="8.0.0-rc.2.23479.6" /> | ||
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.0.0-beta.8" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Folder Include="Connected Services\" /> | ||
<Folder Include="wwwroot\lib\" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.