Skip to content

Commit be1e1e3

Browse files
committed
updates to output bindings
1 parent 8311674 commit be1e1e3

File tree

5 files changed

+41
-33
lines changed

5 files changed

+41
-33
lines changed

event-sourcing/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ While event sourcing can be implemented with various types of databases, this pa
2323

2424
1. Flexible schema: NoSQL databases generally allow for schema flexibility. Easy support for unstructured event data formats that are often in JSON formats align perfectly with the needs of event sourcing architectures.
2525

26-
1. Scalability: NoSQL databases are typically designed for high scale. Data volumes in event sourcing patterns can range from the thousands to millions of messages per second. An underlying database needs to scale and do so seemlessly. Azure Cosmos DB's scale-out architecture is well-suited here with highly elastic throughput and storage.
26+
1. Scalability: NoSQL databases are typically designed for high scale. Data volumes in event sourcing patterns can range from the thousands to millions of messages per second. An underlying database needs to scale and do so seamlessly. Azure Cosmos DB's scale-out architecture is well-suited here with highly elastic throughput and storage.
2727

2828
This sample demonstrates:
2929

event-sourcing/source/CartEvent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class CartEvent
1111
public int UserId { get; set; }
1212
public string EventType { get; set; } = "";
1313
public string? Product { get; set; }
14-
public int? QuantityChange { get; set; }
14+
public int? QuantityChange { get; set; } = 0;
1515
public List<CartItem>? ProductsInCart { get; set; }
1616
public string EventTimestamp { get; set; } = DateTime.UtcNow.ToString();
1717
}

event-sourcing/source/EventSourceFunction.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace EventSourcing
1111
{
1212
public class EventSourceFunction
1313
{
14-
private readonly ILogger _logger;
14+
private readonly ILogger<EventSourceFunction> _logger;
1515

16-
public EventSourceFunction(ILoggerFactory loggerFactory)
16+
public EventSourceFunction(ILogger<EventSourceFunction> logger)
1717
{
18-
_logger = loggerFactory.CreateLogger<EventSourceFunction>();
18+
_logger = logger;
1919
}
2020

2121
[Function("EventSourcing")]
@@ -25,22 +25,22 @@ public EventSourceFunction(ILoggerFactory loggerFactory)
2525
Connection = "CosmosDBConnection",
2626
CreateIfNotExists = true,
2727
PartitionKey = "/CartId")]
28-
public static async Task<IActionResult> Run(
29-
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
30-
IAsyncCollector<CartEvent> cartEventOut,
31-
ILogger log)
28+
public async Task<IActionResult> Run(
29+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
30+
FunctionContext context)
3231
{
33-
log.LogInformation("C# HTTP trigger function processed a request.");
32+
_logger.LogInformation("C# HTTP trigger function processed a request.");
3433

3534
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
3635
string responseMessage;
3736

3837
if (requestBody != null)
3938
{
4039
CartEvent cartEvent = JsonConvert.DeserializeObject<CartEvent>(requestBody) ?? throw new ArgumentException("Request body is empty");
41-
await cartEventOut.AddAsync(cartEvent);
40+
_logger.LogInformation(JsonConvert.SerializeObject(cartEvent, Formatting.Indented));
4241

43-
responseMessage = $"HTTP function successful for event {cartEvent.EventType} for cart {cartEvent.CartId}.";
42+
//responseMessage = $"HTTP function successful for event {cartEvent.EventType} for cart {cartEvent.CartId}.";
43+
return new OkObjectResult(cartEvent);
4444
}
4545
else
4646
{

event-sourcing/source/EventSourcing.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
9+
<ItemGroup>
10+
<None Update="local.settings.json">
11+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
12+
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
13+
</None>
14+
</ItemGroup>
915
<ItemGroup>
1016
<FrameworkReference Include="Microsoft.AspNetCore.App" />
1117
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
1218
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.CosmosDB" Version="4.6.0" />
1319
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
14-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.0" />
15-
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
20+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
21+
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.4" />
1622
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
1723
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
1824
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="4.7.0" />

event-sourcing/source/Program.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
using Microsoft.Extensions.Hosting;
44
using Newtonsoft.Json;
55

6-
var host = new HostBuilder()
7-
.ConfigureFunctionsWebApplication()
8-
.ConfigureServices(services =>
9-
{
10-
services.AddApplicationInsightsTelemetryWorkerService();
11-
services.ConfigureFunctionsApplicationInsights();
12-
})
13-
.Build();
14-
15-
host.Run();
166

177
namespace EventSourcing
188
{
@@ -23,10 +13,11 @@ internal class Program
2313

2414
public static async Task<string> CreateCartEvent(HttpClient client, CartEvent cartEvent)
2515
{
26-
var url = $"{urlBase}/api/EventSourceFunction";
16+
var url = $"{urlBase}/api/EventSourcing";
2717

2818
string jsonBody = JsonConvert.SerializeObject(cartEvent);
2919
var body = new StringContent(jsonBody, System.Text.Encoding.UTF8, "application/json");
20+
Console.WriteLine(jsonBody);
3021

3122
var response = await client.PostAsync(url, body);
3223
string result = await response.Content.ReadAsStringAsync();
@@ -108,7 +99,7 @@ public static List<CartEvent> GenerateCartEvents()
10899
cartEvent.SessionId = sessionId;
109100
cartEvent.UserId = userId;
110101
cartEvent.EventType = action;
111-
cartEvent.ProductsInCart = null;
102+
cartEvent.ProductsInCart = new List<CartItem>();
112103
cartEvents.Add(cartEvent);
113104
}
114105
}
@@ -118,15 +109,25 @@ public static List<CartEvent> GenerateCartEvents()
118109
static async Task Main(string[] args)
119110
{
120111

121-
HttpClient httpClient = new HttpClient();
112+
if (args.Length > 0 && args[0] != "console")
113+
{
114+
var host = new HostBuilder()
115+
.ConfigureFunctionsWebApplication()
116+
.ConfigureServices(services =>
117+
{
118+
services.AddApplicationInsightsTelemetryWorkerService();
119+
services.ConfigureFunctionsApplicationInsights();
120+
})
121+
.Build();
122+
123+
await host.RunAsync();
124+
}
125+
126+
else
127+
{
128+
HttpClient httpClient = new HttpClient();
122129

123-
//var services = new ServiceCollection();
124-
//services.AddHttpClient(); // Add the HttpClientFactory service
125-
//services.AddLogging(); // Add the logging service
126-
//var serviceProvider = services.BuildServiceProvider();
127130

128-
//// Resolve the HttpClient from the service provider
129-
//var httpClient = serviceProvider.GetRequiredService<HttpClient>();
130131
httpClient.Timeout = TimeSpan.FromMinutes(10);
131132

132133
Console.WriteLine("This code will demonstrate the Event Sourcing pattern by saving shopping cart events to Azure Cosmos DB for NoSQL account.");
@@ -151,6 +152,7 @@ static async Task Main(string[] args)
151152

152153
System.Console.WriteLine($"Function completed generation of shopping cart events");
153154
Console.WriteLine($"Check CartEventContainer for new shopping cart events");
155+
}
154156
}
155157
}
156158
}

0 commit comments

Comments
 (0)