-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathStopStrategyMiddleware.cs
51 lines (45 loc) · 2.04 KB
/
StopStrategyMiddleware.cs
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
using DevelopmentInProgress.TradeServer.StrategyExecution.WebHost.Cache.TradeStrategy;
using DevelopmentInProgress.TradeView.Core.TradeStrategy;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Net;
using System.Threading.Tasks;
namespace DevelopmentInProgress.TradeServer.StrategyExecution.WebHost.Web.Middleware
{
public class StopStrategyMiddleware
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA1801:Review unused parameters")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0060:Remove unused parameter")]
public StopStrategyMiddleware(RequestDelegate next)
{
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types")]
public async Task Invoke(HttpContext context, ITradeStrategyCacheManager tradeStrategyCacheManager)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (tradeStrategyCacheManager == null)
{
throw new ArgumentNullException(nameof(tradeStrategyCacheManager));
}
try
{
var json = context.Request.Form["strategyparameters"];
// check the json can convert to type StrategyParameters
var strategyParameters = JsonConvert.DeserializeObject<StrategyParameters>(json);
await tradeStrategyCacheManager.StopStrategy(strategyParameters.StrategyName, json).ConfigureAwait(false);
}
catch (Exception ex)
{
var response = context.Response;
response.ContentType = "application/json";
response.StatusCode = (int)HttpStatusCode.InternalServerError;
await response.WriteAsync(JsonConvert.SerializeObject(ex)).ConfigureAwait(false);
}
}
}
}