Skip to content

Commit f705ac7

Browse files
committed
[r] latest C#
1 parent fdb1c18 commit f705ac7

12 files changed

+125
-133
lines changed

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cSpell.words": [
3+
"cref",
4+
"registrator"
5+
]
6+
}

src/Simplify.Web.Multipart/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## [1.4.5] - 2022-05-18
4+
5+
### Added
6+
7+
- Explicit .NET 6 targeting
8+
9+
### Dependencies
10+
11+
- Simplify.Web bump to 4.6 (PR#36)
12+
313
## [1.4.4] - 2022-03-13
414

515
### Dependencies

src/Simplify.Web.Multipart/Model/Binding/HttpMultipartFormModelBinder.cs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,37 @@
33
using HttpMultipartParser;
44
using Simplify.Web.Model.Binding;
55

6-
namespace Simplify.Web.Multipart.Model.Binding
6+
namespace Simplify.Web.Multipart.Model.Binding;
7+
8+
/// <summary>
9+
/// Provides form multipart data to object binding
10+
/// </summary>
11+
/// <seealso cref="IModelBinder" />
12+
public class HttpMultipartFormModelBinder : IModelBinder
713
{
814
/// <summary>
9-
/// Provides form multipart data to object binding
15+
/// Binds the model.
1016
/// </summary>
11-
/// <seealso cref="IModelBinder" />
12-
public class HttpMultipartFormModelBinder : IModelBinder
17+
/// <typeparam name="T">Model type</typeparam>
18+
/// <param name="args">The <see cref="ModelBinderEventArgs{T}" /> instance containing the event data.</param>
19+
public async Task BindAsync<T>(ModelBinderEventArgs<T> args)
1320
{
14-
/// <summary>
15-
/// Binds the model.
16-
/// </summary>
17-
/// <typeparam name="T">Model type</typeparam>
18-
/// <param name="args">The <see cref="ModelBinderEventArgs{T}" /> instance containing the event data.</param>
19-
public async Task BindAsync<T>(ModelBinderEventArgs<T> args)
20-
{
21-
if (!args.Context.Request.ContentType.Contains("multipart/form-data"))
22-
return;
21+
if (!args.Context.Request.ContentType.Contains("multipart/form-data"))
22+
return;
2323

24-
var multipartModelType = typeof(MultipartViewModel);
24+
var multipartModelType = typeof(MultipartViewModel);
2525

26-
if (typeof(T) != multipartModelType)
27-
throw new ModelBindingException("For HTTP multipart form data model type should be: " + multipartModelType.Name);
26+
if (typeof(T) != multipartModelType)
27+
throw new ModelBindingException("For HTTP multipart form data model type should be: " + multipartModelType.Name);
2828

29-
var parser = await MultipartFormDataParser.ParseAsync(args.Context.Request.Body);
30-
var obj = Activator.CreateInstance<T>();
29+
var parser = await MultipartFormDataParser.ParseAsync(args.Context.Request.Body);
30+
var obj = Activator.CreateInstance<T>();
3131

32-
var model = (MultipartViewModel)(object)obj;
32+
var model = (MultipartViewModel)(object)obj;
3333

34-
model.Files = parser.Files;
35-
model.Parameters = parser.Parameters;
34+
model.Files = parser.Files;
35+
model.Parameters = parser.Parameters;
3636

37-
args.SetModel(obj);
38-
}
37+
args.SetModel(obj);
3938
}
4039
}
Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
using System.Collections.Generic;
22
using HttpMultipartParser;
33

4-
namespace Simplify.Web.Multipart.Model
4+
namespace Simplify.Web.Multipart.Model;
5+
6+
/// <summary>
7+
/// HTTP multipart form data model
8+
/// </summary>
9+
public class MultipartViewModel
510
{
611
/// <summary>
7-
/// HTTP multipart form data model
12+
/// HTTP multipart form data files
813
/// </summary>
9-
public class MultipartViewModel
10-
{
11-
/// <summary>
12-
/// HTTP multipart form data files
13-
/// </summary>
14-
/// <value>
15-
/// The files.
16-
/// </value>
17-
public IReadOnlyList<FilePart> Files { get; set; }
14+
/// <value>
15+
/// The files.
16+
/// </value>
17+
public IReadOnlyList<FilePart> Files { get; set; }
1818

19-
/// <summary>
20-
/// HTTP multipart form data parameters
21-
/// </summary>
22-
/// <value>
23-
/// The parameters.
24-
/// </value>
25-
public IReadOnlyList<ParameterPart> Parameters { get; set; }
26-
}
19+
/// <summary>
20+
/// HTTP multipart form data parameters
21+
/// </summary>
22+
/// <value>
23+
/// The parameters.
24+
/// </value>
25+
public IReadOnlyList<ParameterPart> Parameters { get; set; }
2726
}

src/Simplify.Web.Multipart/Simplify.Web.Multipart.csproj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
4-
<LangVersion>9.0</LangVersion>
5-
<OutputPath>bin\Any CPU\$(Configuration)\</OutputPath>
3+
<TargetFrameworks>net6.0;net5.0;netstandard2.0</TargetFrameworks>
4+
<LangVersion>latest</LangVersion>
65
<EmbedUntrackedSources>true</EmbedUntrackedSources>
76
<IncludeSymbols>true</IncludeSymbols>
87
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
98
<GenerateDocumentationFile>true</GenerateDocumentationFile>
109

11-
<Version>1.4.4</Version>
10+
<Version>1.4.5</Version>
1211

1312
<Authors>Alexander Krylkov</Authors>
1413
<Product>Simplify</Product>
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
using Simplify.DI;
22
using Simplify.Web.Multipart.Model.Binding;
33

4-
namespace Simplify.Web.Multipart
4+
namespace Simplify.Web.Multipart;
5+
6+
/// <summary>
7+
/// Provides Simplify.Web.Json default registration
8+
/// </summary>
9+
public static class SimplifyDIRegistratorExtensions
510
{
611
/// <summary>
7-
/// Provides Simplify.Web.Json default registration
12+
/// Registers Simplify.Web.Json JsonModelBinder.
813
/// </summary>
9-
public static class SimplifyDIRegistratorExtensions
10-
{
11-
/// <summary>
12-
/// Registers Simplify.Web.Json JsonModelBinder.
13-
/// </summary>
14-
/// <param name="registrator">The registrator.</param>
15-
public static IDIRegistrator RegisterHttpMultipartFormModelBinder(this IDIRegistrator registrator) =>
16-
registrator.Register<HttpMultipartFormModelBinder>(LifetimeType.Singleton);
17-
}
14+
/// <param name="registrator">The registrator.</param>
15+
public static IDIRegistrator RegisterHttpMultipartFormModelBinder(this IDIRegistrator registrator) =>
16+
registrator.Register<HttpMultipartFormModelBinder>(LifetimeType.Singleton);
1817
}

src/TestClient/Program.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,19 @@
22
using System.Text;
33
using RestSharp;
44

5-
namespace TestClient
6-
{
7-
internal class Program
8-
{
9-
private static void Main()
10-
{
11-
var client = new RestClient("http://localhost:5000/");
5+
var client = new RestClient("http://localhost:5000/");
126

13-
var request = new RestRequest("api/v1/testIn", Method.Post)
14-
{
15-
AlwaysMultipartFormData = true
16-
};
7+
var request = new RestRequest("api/v1/testIn", Method.Post)
8+
{
9+
AlwaysMultipartFormData = true
10+
};
1711

18-
request.AddFile("test file", Encoding.UTF8.GetBytes("Hello World!!!"), "MyFile.txt", "text/plain");
12+
request.AddFile("test file", Encoding.UTF8.GetBytes("Hello World!!!"), "MyFile.txt", "text/plain");
1913

20-
var result = client.ExecuteAsync(request).Result;
14+
var result = client.ExecuteAsync(request).Result;
2115

22-
if (result.IsSuccessful != true)
23-
throw new InvalidOperationException("Error sending file: " + result.Content);
16+
if (result.IsSuccessful != true)
17+
throw new InvalidOperationException("Error sending file: " + result.Content);
2418

25-
Console.WriteLine("HTTP status: " + result.StatusCode);
26-
Console.ReadLine();
27-
}
28-
}
29-
}
19+
Console.WriteLine("HTTP status: " + result.StatusCode);
20+
Console.ReadLine();

src/TestServer/Controllers/Api/v1/TestInController.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,32 @@
77
using Simplify.Web.Attributes;
88
using Simplify.Web.Multipart.Model;
99

10-
namespace TestServer.Controllers.Api.v1
10+
namespace TestServer.Controllers.Api.v1;
11+
12+
[Post("/api/v1/testIn")]
13+
public class TestInController : AsyncController<MultipartViewModel>
1114
{
12-
[Post("/api/v1/testIn")]
13-
public class TestInController : AsyncController<MultipartViewModel>
15+
public override async Task<ControllerResponse> Invoke()
1416
{
15-
public override async Task<ControllerResponse> Invoke()
16-
{
17-
var file = Model.Files.FirstOrDefault() ?? throw new ArgumentException("No files in model");
18-
using var stream = new StreamReader(file.Data);
19-
var fileData = await stream.ReadToEndAsync();
17+
var file = Model.Files.FirstOrDefault() ?? throw new ArgumentException("No files in model");
18+
using var stream = new StreamReader(file.Data);
19+
var fileData = await stream.ReadToEndAsync();
2020

21-
Trace.WriteLine($"Files count: '{Model.Files.Count}'");
22-
Trace.WriteLine($"File name: '{file.FileName}'");
23-
Trace.WriteLine($"File content: '{fileData}'");
21+
Trace.WriteLine($"Files count: '{Model.Files.Count}'");
22+
Trace.WriteLine($"File name: '{file.FileName}'");
23+
Trace.WriteLine($"File content: '{fileData}'");
2424

25-
// Assert
25+
// Assert
2626

27-
if (file.Name != "test file")
28-
return Content($"Wrong name, actual: '{file.Name}'", 500);
27+
if (file.Name != "test file")
28+
return Content($"Wrong name, actual: '{file.Name}'", 500);
2929

30-
if (file.FileName != "MyFile.txt")
31-
return Content($"Wrong file name, actual: '{file.FileName}'", 500);
30+
if (file.FileName != "MyFile.txt")
31+
return Content($"Wrong file name, actual: '{file.FileName}'", 500);
3232

33-
if (fileData != "Hello World!!!")
34-
return Content($"Wrong file data, actual: '{fileData}'", 500);
33+
if (fileData != "Hello World!!!")
34+
return Content($"Wrong file data, actual: '{fileData}'", 500);
3535

36-
return NoContent();
37-
}
36+
return NoContent();
3837
}
3938
}
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
using Simplify.Web;
22
using Simplify.Web.Attributes;
33

4-
namespace TestServer.Controllers
4+
namespace TestServer.Controllers;
5+
6+
[Get("status")]
7+
public class StatusController : Controller
58
{
6-
[Get("status")]
7-
public class StatusController : Controller
8-
{
9-
public override ControllerResponse Invoke()
10-
{
11-
return Content("Service is running!");
12-
}
13-
}
9+
public override ControllerResponse Invoke() => Content("Service is running!");
1410
}

src/TestServer/Program.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using Microsoft.AspNetCore;
22
using Microsoft.AspNetCore.Hosting;
33

4-
namespace TestServer
4+
namespace TestServer;
5+
6+
public class Program
57
{
6-
public class Program
7-
{
8-
public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();
8+
public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();
99

10-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
11-
WebHost.CreateDefaultBuilder(args)
12-
.UseStartup<Startup>();
13-
}
10+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
11+
WebHost.CreateDefaultBuilder(args)
12+
.UseStartup<Startup>();
1413
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
using Simplify.DI;
22
using Simplify.Web.Multipart;
33

4-
namespace TestServer.Setup
4+
namespace TestServer.Setup;
5+
6+
public static class IocRegistrations
57
{
6-
public static class IocRegistrations
7-
{
8-
public static void Register()
9-
{
10-
DIContainer.Current.RegisterHttpMultipartFormModelBinder();
11-
}
12-
}
8+
public static void Register() => DIContainer.Current.RegisterHttpMultipartFormModelBinder();
139
}

src/TestServer/Startup.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@
77
using Simplify.Web.Multipart.Model.Binding;
88
using TestServer.Setup;
99

10-
namespace TestServer
10+
namespace TestServer;
11+
12+
public class Startup
1113
{
12-
public class Startup
14+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
1315
{
14-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
15-
{
16-
IocRegistrations.Register();
16+
IocRegistrations.Register();
1717

18-
if (env.IsDevelopment())
19-
app.UseDeveloperExceptionPage();
18+
if (env.IsDevelopment())
19+
app.UseDeveloperExceptionPage();
2020

21-
HttpModelHandler.RegisterModelBinder<HttpMultipartFormModelBinder>();
21+
HttpModelHandler.RegisterModelBinder<HttpMultipartFormModelBinder>();
2222

23-
app.UseSimplifyWeb();
23+
app.UseSimplifyWeb();
2424

25-
DIContainer.Current.Verify();
26-
}
25+
DIContainer.Current.Verify();
2726
}
2827
}

0 commit comments

Comments
 (0)