Skip to content

Commit 727f86c

Browse files
authored
Merge pull request #1 from Azure/master
merge from Master
2 parents f73657d + 9d0fe80 commit 727f86c

File tree

355 files changed

+37478
-15461
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

355 files changed

+37478
-15461
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Installing AutoRest version: latest
2+
AutoRest installed successfully.
3+
Commencing code generation
4+
Generating CSharp code
5+
Executing AutoRest command
6+
cmd.exe /c autorest.cmd https://github.com/Azure/azure-rest-api-specs/blob/master/specification/logic/resource-manager/readme.md --csharp --version=latest --reflect-api-versions --csharp-sdks-folder=C:\dev\azure-sdk-for-net\sdk
7+
2019-10-15 18:51:23 UTC
8+
Azure-rest-api-specs repository information
9+
GitHub fork: Azure
10+
Branch: master
11+
Commit: 411d4cf447639289e4ee1efa4d066df2773ebccd
12+
AutoRest information
13+
Requested version: latest
14+
Bootstrapper version: [email protected]

eng/mgmt/mgmtmetadata/recoveryservicessiterecovery_resource-manager.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ AutoRest installed successfully.
33
Commencing code generation
44
Generating CSharp code
55
Executing AutoRest command
6-
cmd.exe /c autorest.cmd https://github.com/Azure/azure-rest-api-specs/blob/master/specification/recoveryservicessiterecovery/resource-manager/readme.md --csharp --version=latest --reflect-api-versions --csharp-sdks-folder=D:\ASROneSDK\azure-sdk-for-net\sdk
7-
2019-12-20 05:05:29 UTC
6+
cmd.exe /c autorest.cmd https://github.com/Azure/azure-rest-api-specs/blob/master/specification/recoveryservicessiterecovery/resource-manager/readme.md --csharp --version=latest --reflect-api-versions --csharp-sdks-folder=D:\Repos\AsrOneSdk-2\azure-sdk-for-net\sdk
7+
2020-01-02 07:34:00 UTC
88
Azure-rest-api-specs repository information
99
GitHub fork: Azure
1010
Branch: master
11-
Commit: cb5fe2f993e08939a712c20f26ff9cbab7e98c82
11+
Commit: 4676d9187988aa6793ff704aa39c5f495b40f6e0
1212
AutoRest information
1313
Requested version: latest
1414
Bootstrapper version: [email protected]
+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
using Azure.Messaging.EventHubs;
11+
using Azure.Messaging.EventHubs.Processor;
12+
using Azure.Storage.Blobs;
13+
using Microsoft.Extensions.Azure;
14+
using Microsoft.Extensions.Configuration;
15+
using Microsoft.Extensions.Hosting;
16+
using Microsoft.Extensions.Logging;
17+
18+
namespace LineCounter
19+
{
20+
/// <summary>
21+
/// Hosted service that listens to uploads events hub and calculates line counts for the blobs in background updating blob metadata and posting a message into results event hub
22+
/// </summary>
23+
public class LineCounterService : IHostedService
24+
{
25+
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
26+
private readonly ILogger<LineCounterService> _logger;
27+
28+
private readonly BlobContainerClient _blobContainerClient;
29+
private readonly EventHubProducerClient _resultsClient;
30+
31+
private readonly EventProcessorClient _processor;
32+
33+
public LineCounterService(
34+
IAzureClientFactory<EventHubProducerClient> producerFactory,
35+
BlobServiceClient blobServiceClient,
36+
ILogger<LineCounterService> logger,
37+
IConfiguration configuration)
38+
{
39+
_logger = logger;
40+
_blobContainerClient = blobServiceClient.GetBlobContainerClient("uploads");
41+
_resultsClient = producerFactory.CreateClient("Results");
42+
_processor = new EventProcessorClient(
43+
_blobContainerClient,
44+
EventHubConsumerClient.DefaultConsumerGroupName,
45+
configuration["Uploads:connectionString"],
46+
configuration["Uploads:eventHubName"]);
47+
}
48+
49+
public async Task StartAsync(CancellationToken cancellationToken)
50+
{
51+
_logger.LogInformation("Starting processor host");
52+
_processor.ProcessEventAsync += ProcessEvent;
53+
_processor.ProcessErrorAsync += (eventArgs) =>
54+
{
55+
_logger.LogError(eventArgs.Exception, "Error in " + eventArgs.PartitionId);
56+
return Task.CompletedTask;
57+
};
58+
await _processor.StartProcessingAsync();
59+
}
60+
61+
private async Task ProcessEvent(ProcessEventArgs eventArgs)
62+
{
63+
// Note there is a bug with the HasEvent property in Preview 6, so check if Data is not null as work around.
64+
// https://github.com/Azure/azure-sdk-for-net/blob/master/sdk/eventhub/Azure.Messaging.EventHubs.Processor/samples/Sample08_EventProcessingHeartbeat.cs#L85
65+
if (eventArgs.Data != null)
66+
{
67+
var totalCount = 0;
68+
var cancellationToken = _cancellationTokenSource.Token;
69+
70+
var blobName = Encoding.UTF8.GetString(eventArgs.Data.Body.Span);
71+
72+
var blobClient = _blobContainerClient.GetBlobClient(blobName);
73+
var downloadInfo = await blobClient.DownloadAsync(cancellationToken);
74+
75+
_logger.LogInformation("Processing {blob}", blobName);
76+
77+
var newLineCountingStream = new NewLineCountingStream();
78+
await downloadInfo.Value.Content.CopyToAsync(newLineCountingStream, cancellationToken);
79+
var newLineCount = newLineCountingStream.NewLineCount;
80+
81+
await blobClient.SetMetadataAsync(
82+
new Dictionary<string, string>()
83+
{
84+
{ "whitespacecount", newLineCount.ToString()}
85+
},
86+
cancellationToken: cancellationToken);
87+
88+
_logger.LogInformation("{blob} had {lines} lines", blobName, newLineCount);
89+
90+
totalCount += newLineCount;
91+
92+
using EventDataBatch eventBatch = await _resultsClient.CreateBatchAsync();
93+
eventBatch.TryAdd(new EventData(BitConverter.GetBytes(totalCount)));
94+
await _resultsClient.SendAsync(eventBatch, cancellationToken);
95+
await eventArgs.UpdateCheckpointAsync();
96+
}
97+
}
98+
99+
public async Task StopAsync(CancellationToken cancellationToken)
100+
{
101+
_cancellationTokenSource.Cancel();
102+
await _processor.StopProcessingAsync();
103+
}
104+
105+
private class NewLineCountingStream : Stream
106+
{
107+
public int NewLineCount { get; private set; }
108+
109+
public override void Flush()
110+
{
111+
}
112+
113+
public override int Read(byte[] buffer, int offset, int count)
114+
{
115+
throw new NotImplementedException();
116+
}
117+
118+
public override long Seek(long offset, SeekOrigin origin)
119+
{
120+
throw new NotImplementedException();
121+
}
122+
123+
public override void SetLength(long value)
124+
{
125+
throw new NotImplementedException();
126+
}
127+
128+
public override void Write(byte[] buffer, int offset, int count)
129+
{
130+
foreach (var b in buffer)
131+
{
132+
if (b == (byte)'\n')
133+
{
134+
NewLineCount++;
135+
}
136+
}
137+
}
138+
139+
public override bool CanRead { get; }
140+
public override bool CanSeek { get; }
141+
public override bool CanWrite => true;
142+
public override long Length { get; }
143+
public override long Position { get; set; }
144+
}
145+
}
146+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+

2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Azure.Messaging.EventHubs;
7+
using Azure.Storage.Blobs;
8+
using Microsoft.AspNetCore.Mvc;
9+
using Microsoft.Extensions.Azure;
10+
using Microsoft.Extensions.Logging;
11+
12+
namespace LineCounter.Controllers
13+
{
14+
[Route("/")]
15+
public class HomeController : Controller
16+
{
17+
private readonly ILogger<HomeController> _logger;
18+
private readonly BlobContainerClient _blobContainerClient;
19+
private readonly EventHubProducerClient _uploadsProducer;
20+
21+
public HomeController(ILogger<HomeController> logger, BlobServiceClient blobServiceClient, IAzureClientFactory<EventHubProducerClient> clientFactory)
22+
{
23+
_logger = logger;
24+
_blobContainerClient = blobServiceClient.GetBlobContainerClient("uploads");
25+
_uploadsProducer = clientFactory.CreateClient("Uploads");
26+
}
27+
28+
public IActionResult Index()
29+
{
30+
return View();
31+
}
32+
33+
[HttpPost("upload")]
34+
public async Task<IActionResult> Upload()
35+
{
36+
var names = new List<string>();
37+
38+
foreach (var file in HttpContext.Request.Form.Files)
39+
{
40+
var fileName = Guid.NewGuid().ToString("n") + " " + file.FileName;
41+
42+
var stream = file.OpenReadStream();
43+
_logger.LogInformation("Uploading {fileName}", fileName);
44+
await _blobContainerClient.CreateIfNotExistsAsync();
45+
await _blobContainerClient.UploadBlobAsync(fileName, stream);
46+
47+
using EventDataBatch eventBatch = await _uploadsProducer.CreateBatchAsync();
48+
eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(fileName)));
49+
await _uploadsProducer.SendAsync(eventBatch);
50+
51+
names.Add(fileName);
52+
}
53+
54+
return View("Index", names.ToArray());
55+
}
56+
57+
[HttpGet("status/{name}.html")]
58+
public async Task<string> Status(string name)
59+
{
60+
var properties = await _blobContainerClient.GetBlobClient(name).GetPropertiesAsync();
61+
properties.Value.Metadata.TryGetValue("whitespacecount", out var count);
62+
63+
return count ?? "-1";
64+
}
65+
}
66+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>netcoreapp3.0</TargetFramework>
4+
<LangVersion>preview</LangVersion>
5+
<UserSecretsId>67ca5d37-051b-4ab1-b013-c74a2d4edb76</UserSecretsId>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Azure.Messaging.EventHubs" Version="5.0.0-preview.6" />
9+
<PackageReference Include="Azure.Messaging.EventHubs.Processor" Version="5.0.0-preview.6" />
10+
<PackageReference Include="Azure.Storage.Blobs" Version="12.1.0" />
11+
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.12.0" />
12+
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.0.0" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<Folder Include="Connected Services\" />
16+
</ItemGroup>
17+
</Project>

samples/linecounter/Program.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
8+
9+
namespace LineCounter
10+
{
11+
public class Program
12+
{
13+
public static void Main(string[] args)
14+
{
15+
CreateHostBuilder(args).Build().Run();
16+
}
17+
18+
public static IHostBuilder CreateHostBuilder(string[] args) =>
19+
Host.CreateDefaultBuilder(args)
20+
.ConfigureServices(
21+
services =>
22+
{
23+
services.AddSingleton<IHostedService, LineCounterService>();
24+
})
25+
.ConfigureAppConfiguration(builder => builder.AddUserSecrets(typeof(Program).Assembly))
26+
.ConfigureWebHostDefaults(webBuilder =>
27+
{
28+
webBuilder.UseStartup<Startup>();
29+
});
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:55215/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"environmentVariables": {
15+
"ASPNETCORE_ENVIRONMENT": "Development"
16+
}
17+
},
18+
"LineCounter": {
19+
"commandName": "Project",
20+
"launchBrowser": true,
21+
"environmentVariables": {
22+
"ASPNETCORE_ENVIRONMENT": "Development"
23+
},
24+
"applicationUrl": "http://localhost:55217/"
25+
}
26+
}
27+
}

samples/linecounter/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# README
2+
3+
Sample that uses illustrates using blob and event hub clients along with ASP.NET Core integration, distributed tracing and hosted services.
4+
5+
# Configuration
6+
7+
To run the sample set the following configuration properties using manage user secrets command in VS or user secrets command line https://docs.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-2.2&tabs=windows#set-a-secret
8+
9+
``` json
10+
{
11+
"Blob": {
12+
"connectionString": "..."
13+
},
14+
"Uploads": {
15+
"connectionString": "...",
16+
"eventHubName": "..."
17+
},
18+
"Results": {
19+
"connectionString": "...",
20+
"eventHubName": "..."
21+
}
22+
}
23+
```
24+
25+
To light up App Insights, add the InstrumentationKey key and value to the ApplicationInsights node in appsettings.json
26+
27+
``` json
28+
{
29+
"ApplicationInsights": {
30+
"InstrumentationKey": "..."
31+
}
32+
}
33+
```

0 commit comments

Comments
 (0)