// ------------------------------------------------------------------------
// Copyright 2021 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
namespace Dapr.E2E.Test
{
using Dapr.E2E.Test.Actors.Reminders;
using Dapr.E2E.Test.Actors.Timers;
using Dapr.E2E.Test.Actors.State;
using Dapr.E2E.Test.Actors.ExceptionTesting;
using Dapr.E2E.Test.Actors.Serialization;
using Dapr.E2E.Test.Actors.WeaklyTypedTesting;
using Dapr.E2E.Test.App.ErrorTesting;
using Dapr.Workflow;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Serilog;
using Grpc.Net.Client;
///
/// Startup class.
///
public class Startup
{
bool JsonSerializationEnabled =>
System.Linq.Enumerable.Contains(System.Environment.GetCommandLineArgs(), "--json-serialization");
///
/// Initializes a new instance of the class.
///
/// Configuration.
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
///
/// Gets the configuration.
///
public IConfiguration Configuration { get; }
///
/// Configures Services.
///
/// Service Collection.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication().AddDapr();
services.AddAuthorization(o => o.AddDapr());
services.AddControllers().AddDapr();
services.AddLogging(builder =>
{
builder.AddConsole();
});
// Register a workflow and associated activity
services.AddDaprWorkflow(options =>
{
// Example of registering a "PlaceOrder" workflow function
options.RegisterWorkflow("PlaceOrder", implementation: async (context, input) =>
{
var itemToPurchase = input;
// There are 5 of the same event to test that multiple similarly-named events can be raised in parallel
await context.WaitForExternalEventAsync("ChangePurchaseItem");
await context.WaitForExternalEventAsync("ChangePurchaseItem");
await context.WaitForExternalEventAsync("ChangePurchaseItem");
await context.WaitForExternalEventAsync("ChangePurchaseItem");
itemToPurchase = await context.WaitForExternalEventAsync("ChangePurchaseItem");
// In real life there are other steps related to placing an order, like reserving
// inventory and charging the customer credit card etc. But let's keep it simple ;)
await context.CallActivityAsync("ShipProduct", itemToPurchase);
return itemToPurchase;
});
// Example of registering a "ShipProduct" workflow activity function
options.RegisterActivity("ShipProduct", implementation: (context, input) =>
{
return Task.FromResult($"We are shipping {input} to the customer using our hoard of drones!");
});
});
services.AddDaprWorkflow(options =>
{
// Example of registering a "StartOrder" workflow function
options.RegisterWorkflow("StartLargeOrder", implementation: async (context, input) =>
{
var itemToPurchase = input;
itemToPurchase = await context.WaitForExternalEventAsync("FinishLargeOrder");
return itemToPurchase;
});
options.RegisterActivity("FinishLargeOrder", implementation: (context, input) =>
{
return Task.FromResult($"We are finishing, it's huge!");
});
options.UseGrpcChannelOptions(new GrpcChannelOptions
{
MaxReceiveMessageSize = 32 * 1024 * 1024,
MaxSendMessageSize = 32 * 1024 * 1024
});
});
services.AddActors(options =>
{
options.UseJsonSerialization = JsonSerializationEnabled;
options.Actors.RegisterActor();
options.Actors.RegisterActor();
options.Actors.RegisterActor();
options.Actors.RegisterActor();
options.Actors.RegisterActor();
options.Actors.RegisterActor();
options.Actors.RegisterActor();
});
}
///
/// Configures Application Builder and WebHost environment.
///
/// Application builder.
/// Webhost environment.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
var logger = new LoggerConfiguration().WriteTo.File("log.txt").CreateLogger();
LoggerFactory.Create(builder =>
{
builder.AddSerilog(logger);
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSerilogRequestLogging();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseCloudEvents();
app.UseEndpoints(endpoints =>
{
endpoints.MapSubscribeHandler();
endpoints.MapActorsHandlers();
endpoints.MapControllers();
});
}
}
}