-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
109 lines (100 loc) · 4.46 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
109 lines (100 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using Asp.Versioning;
using Elsa;
using Elsa.Models;
using Elsa.Server.Api;
using Elsa.Server.Api.Extensions;
using Elsa.Server.Api.Extensions.SchemaFilters;
using Elsa.Server.Api.Mapping;
using Elsa.Server.Api.Services;
using Elsa.Server.Api.Swagger.Examples;
#if NET10_0_OR_GREATER
using Microsoft.OpenApi;
#else
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
#endif
using Swashbuckle.AspNetCore.Filters;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddElsaApiEndpoints(this IServiceCollection services, ElsaApiOptions apiOptions) =>
services.AddElsaApiEndpoints(options =>
{
options.SetupNewtonsoftJson = apiOptions.SetupNewtonsoftJson;
});
public static IServiceCollection AddElsaApiEndpoints(this IServiceCollection services, Action<ElsaApiOptions>? configureApiOptions = default)
{
var apiOptions = new ElsaApiOptions();
configureApiOptions?.Invoke(apiOptions);
var setupNewtonsoftJson = apiOptions.SetupNewtonsoftJson ?? (_ => { });
//Don't set Newtonsoft globally
services.AddControllers();//.AddNewtonsoftJson(setupNewtonsoftJson);
services.AddRouting(options => { options.LowercaseUrls = true; });
services.AddApiVersioning(
options =>
{
options.ReportApiVersions = true;
options.DefaultApiVersion = ApiVersion.Default;
options.AssumeDefaultVersionWhenUnspecified = true;
}).AddApiExplorer(o =>
{
o.GroupNameFormat = "'v'VVV";
o.SubstituteApiVersionInUrl = true;
});
services
.AddSingleton<ConnectionConverter>()
.AddSingleton<ActivityBlueprintConverter>()
.AddScoped<IWorkflowBlueprintMapper, WorkflowBlueprintMapper>()
.AddSingleton<IEndpointContentSerializerSettingsProvider, EndpointContentSerializerSettingsProvider>()
.AddAutoMapperProfile<AutoMapperProfile>()
.AddSignalR();
services.AddMvc(options =>
{
//Use this conventions to set ElsaNewtonsoftJsonConvention to all controllers in Elsa.Server.Api
options.Conventions.Add(new ElsaNewtonsoftJsonConvention());
});
return services;
}
public static IServiceCollection AddElsaSwagger(this IServiceCollection services, Action<SwaggerGenOptions>? configure = default) =>
services
.AddSwaggerExamplesFromAssemblyOf<WorkflowDefinitionExample>()
.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Elsa", Version = "v1" });
c.EnableAnnotations();
//c.ExampleFilters(); I don't know why, this line will make swagger error
c.MapType<VersionOptions?>(() => new OpenApiSchema
{
#if NET10_0_OR_GREATER
Type = JsonSchemaType.String | JsonSchemaType.Null,
Example = "Latest",
Description = "Any of Latest, Published, Draft, LatestOrPublished or a specific version number.",
Default = "Latest"
#else
Type = PrimitiveType.String.ToString().ToLower(),
Example = new OpenApiString("Latest"),
Description = "Any of Latest, Published, Draft, LatestOrPublished or a specific version number.",
Nullable = true,
Default = new OpenApiString("Latest")
#endif
});
c.MapType<Type>(() => new OpenApiSchema
{
#if NET10_0_OR_GREATER
Type = JsonSchemaType.String,
Example = "System.String, mscorlib"
#else
Type = PrimitiveType.String.ToString().ToLower(),
Example = new OpenApiString("System.String, mscorlib")
#endif
});
//Allow enums to be displayed
c.SchemaFilter<XEnumNamesSchemaFilter>();
configure?.Invoke(c);
});
}
}