-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathStartup.cs
More file actions
146 lines (134 loc) · 4.64 KB
/
Startup.cs
File metadata and controls
146 lines (134 loc) · 4.64 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using EdjCase.JsonRpc.Router.Defaults;
using Microsoft.AspNetCore;
using System.Reflection;
using System.Text.Json;
using EdjCase.JsonRpc.Router.Swagger.Extensions;
using Microsoft.Extensions.Configuration;
using System.Text.Json.Serialization;
using EdjCase.JsonRpc.Router.Sample.Controllers;
namespace EdjCase.JsonRpc.Router.Sample
{
public class Startup
{
private readonly IConfiguration configuration;
public Startup(IConfiguration configuration)
{
this.configuration = configuration;
}
// This method gets called by a runtime.
// Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
var globalJsonSerializerOptions = new JsonSerializerOptions
{
//Example json config
DefaultIgnoreCondition = JsonIgnoreCondition.Never,
WriteIndented = true
};
void ConfigureRpc(RpcServerConfiguration config)
{
//(Optional) Hard cap on batch size, will block requests will larger sizes, defaults to no limit
config.BatchRequestLimit = 5;
//(Optional) If true returns full error messages in response, defaults to false
config.ShowServerExceptions = false;
//(Optional) Configure how the router serializes requests
config.JsonSerializerSettings = globalJsonSerializerOptions;
//(Optional) Configure custom exception handling for exceptions during invocation of the method
config.OnInvokeException = (context) =>
{
if (context.Exception is InvalidOperationException)
{
//Handle a certain type of exception and return a custom response instead
//of an internal server error
int customErrorCode = 1;
var customData = new
{
Field = "Value"
};
var response = new RpcMethodErrorResult(customErrorCode, "Custom message", customData);
return OnExceptionResult.UseObjectResponse(response);
}
//Continue to throw the exception
return OnExceptionResult.DontHandle();
};
}
services
.AddControllers()
.Services
.AddJsonRpcWithSwagger(ConfigureRpc, globalJsonSerializerOptions)
.Configure<RequestCacheOptions>(options =>
{
options.SizeLimit = 10;
options.SlidingExpiration = TimeSpan.FromMinutes(5);
options.AbsoluteExpiration = TimeSpan.FromMinutes(90);
});
}
// Configure is called after ConfigureServices is called.
public void Configure(IApplicationBuilder app)
{
app
.Map("/Mix", b =>
{
b
.UseRouting()
.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
})
.UseJsonRpc(b =>
{
b.AddController<NonApiController>();
});
})
.Map("/BaseController", b =>
{
//Will make all controllers that derived from `ControllerBase` available
//Each dervied controller will use their name as the route, unless overridden by RpcRouteAttribute
b.UseJsonRpcWithBaseController<ControllerBase>();
})
.Map("/Controllers", b =>
{
b.UseJsonRpc(options =>
{
options
//Will make controller methods available for path '/First', unless overridden by RpcRouteAttribute
//Not that any class will work here, not just a class derived from `RpcController`
.AddController<NonRpcController>()
//Will make `CustomController` methods available for custom path '/CustomPath'
.AddControllerWithCustomPath<CustomController>("CustomPath");
});
})
.Map("/Methods", b =>
{
b.UseJsonRpc(options =>
{
MethodInfo customControllerMethod1 = typeof(CustomController).GetMethod("Method1");
MethodInfo otherControllerMethod1 = typeof(OtherController).GetMethod("Method1");
options
//Will make the `Method1` method in `CustomController` available with route '/'
//Note that since that method has `RpcRouteAttribute("Method")`, that will change the method name
//from `Method1` to `Method` in the router
.AddMethod(customControllerMethod1)
//Will make the `Method1` method in `OtherController` available with route '/CustomMethods'
.AddMethod(otherControllerMethod1, "CustomMethods");
});
})
//Will make all public classes deriving from `RpcController` available to the rpc router
.UseJsonRpcWithSwaggerUI();
}
}
public class Program
{
public static void Main(string[] args)
{
var host = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}