Skip to content

Commit

Permalink
[r] latest C#
Browse files Browse the repository at this point in the history
  • Loading branch information
i4004 committed May 18, 2022
1 parent fdb1c18 commit f705ac7
Show file tree
Hide file tree
Showing 12 changed files with 125 additions and 133 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"cref",
"registrator"
]
}
10 changes: 10 additions & 0 deletions src/Simplify.Web.Multipart/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [1.4.5] - 2022-05-18

### Added

- Explicit .NET 6 targeting

### Dependencies

- Simplify.Web bump to 4.6 (PR#36)

## [1.4.4] - 2022-03-13

### Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,37 @@
using HttpMultipartParser;
using Simplify.Web.Model.Binding;

namespace Simplify.Web.Multipart.Model.Binding
namespace Simplify.Web.Multipart.Model.Binding;

/// <summary>
/// Provides form multipart data to object binding
/// </summary>
/// <seealso cref="IModelBinder" />
public class HttpMultipartFormModelBinder : IModelBinder
{
/// <summary>
/// Provides form multipart data to object binding
/// Binds the model.
/// </summary>
/// <seealso cref="IModelBinder" />
public class HttpMultipartFormModelBinder : IModelBinder
/// <typeparam name="T">Model type</typeparam>
/// <param name="args">The <see cref="ModelBinderEventArgs{T}" /> instance containing the event data.</param>
public async Task BindAsync<T>(ModelBinderEventArgs<T> args)
{
/// <summary>
/// Binds the model.
/// </summary>
/// <typeparam name="T">Model type</typeparam>
/// <param name="args">The <see cref="ModelBinderEventArgs{T}" /> instance containing the event data.</param>
public async Task BindAsync<T>(ModelBinderEventArgs<T> args)
{
if (!args.Context.Request.ContentType.Contains("multipart/form-data"))
return;
if (!args.Context.Request.ContentType.Contains("multipart/form-data"))
return;

var multipartModelType = typeof(MultipartViewModel);
var multipartModelType = typeof(MultipartViewModel);

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

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

var model = (MultipartViewModel)(object)obj;
var model = (MultipartViewModel)(object)obj;

model.Files = parser.Files;
model.Parameters = parser.Parameters;
model.Files = parser.Files;
model.Parameters = parser.Parameters;

args.SetModel(obj);
}
args.SetModel(obj);
}
}
37 changes: 18 additions & 19 deletions src/Simplify.Web.Multipart/Model/MultipartViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
using System.Collections.Generic;
using HttpMultipartParser;

namespace Simplify.Web.Multipart.Model
namespace Simplify.Web.Multipart.Model;

/// <summary>
/// HTTP multipart form data model
/// </summary>
public class MultipartViewModel
{
/// <summary>
/// HTTP multipart form data model
/// HTTP multipart form data files
/// </summary>
public class MultipartViewModel
{
/// <summary>
/// HTTP multipart form data files
/// </summary>
/// <value>
/// The files.
/// </value>
public IReadOnlyList<FilePart> Files { get; set; }
/// <value>
/// The files.
/// </value>
public IReadOnlyList<FilePart> Files { get; set; }

/// <summary>
/// HTTP multipart form data parameters
/// </summary>
/// <value>
/// The parameters.
/// </value>
public IReadOnlyList<ParameterPart> Parameters { get; set; }
}
/// <summary>
/// HTTP multipart form data parameters
/// </summary>
/// <value>
/// The parameters.
/// </value>
public IReadOnlyList<ParameterPart> Parameters { get; set; }
}
7 changes: 3 additions & 4 deletions src/Simplify.Web.Multipart/Simplify.Web.Multipart.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
<LangVersion>9.0</LangVersion>
<OutputPath>bin\Any CPU\$(Configuration)\</OutputPath>
<TargetFrameworks>net6.0;net5.0;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<Version>1.4.4</Version>
<Version>1.4.5</Version>

<Authors>Alexander Krylkov</Authors>
<Product>Simplify</Product>
Expand Down
21 changes: 10 additions & 11 deletions src/Simplify.Web.Multipart/SimplifyDIRegistratorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using Simplify.DI;
using Simplify.Web.Multipart.Model.Binding;

namespace Simplify.Web.Multipart
namespace Simplify.Web.Multipart;

/// <summary>
/// Provides Simplify.Web.Json default registration
/// </summary>
public static class SimplifyDIRegistratorExtensions
{
/// <summary>
/// Provides Simplify.Web.Json default registration
/// Registers Simplify.Web.Json JsonModelBinder.
/// </summary>
public static class SimplifyDIRegistratorExtensions
{
/// <summary>
/// Registers Simplify.Web.Json JsonModelBinder.
/// </summary>
/// <param name="registrator">The registrator.</param>
public static IDIRegistrator RegisterHttpMultipartFormModelBinder(this IDIRegistrator registrator) =>
registrator.Register<HttpMultipartFormModelBinder>(LifetimeType.Singleton);
}
/// <param name="registrator">The registrator.</param>
public static IDIRegistrator RegisterHttpMultipartFormModelBinder(this IDIRegistrator registrator) =>
registrator.Register<HttpMultipartFormModelBinder>(LifetimeType.Singleton);
}
31 changes: 11 additions & 20 deletions src/TestClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,19 @@
using System.Text;
using RestSharp;

namespace TestClient
{
internal class Program
{
private static void Main()
{
var client = new RestClient("http://localhost:5000/");
var client = new RestClient("http://localhost:5000/");

var request = new RestRequest("api/v1/testIn", Method.Post)
{
AlwaysMultipartFormData = true
};
var request = new RestRequest("api/v1/testIn", Method.Post)
{
AlwaysMultipartFormData = true
};

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

var result = client.ExecuteAsync(request).Result;
var result = client.ExecuteAsync(request).Result;

if (result.IsSuccessful != true)
throw new InvalidOperationException("Error sending file: " + result.Content);
if (result.IsSuccessful != true)
throw new InvalidOperationException("Error sending file: " + result.Content);

Console.WriteLine("HTTP status: " + result.StatusCode);
Console.ReadLine();
}
}
}
Console.WriteLine("HTTP status: " + result.StatusCode);
Console.ReadLine();
39 changes: 19 additions & 20 deletions src/TestServer/Controllers/Api/v1/TestInController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@
using Simplify.Web.Attributes;
using Simplify.Web.Multipart.Model;

namespace TestServer.Controllers.Api.v1
namespace TestServer.Controllers.Api.v1;

[Post("/api/v1/testIn")]
public class TestInController : AsyncController<MultipartViewModel>
{
[Post("/api/v1/testIn")]
public class TestInController : AsyncController<MultipartViewModel>
public override async Task<ControllerResponse> Invoke()
{
public override async Task<ControllerResponse> Invoke()
{
var file = Model.Files.FirstOrDefault() ?? throw new ArgumentException("No files in model");
using var stream = new StreamReader(file.Data);
var fileData = await stream.ReadToEndAsync();
var file = Model.Files.FirstOrDefault() ?? throw new ArgumentException("No files in model");
using var stream = new StreamReader(file.Data);
var fileData = await stream.ReadToEndAsync();

Trace.WriteLine($"Files count: '{Model.Files.Count}'");
Trace.WriteLine($"File name: '{file.FileName}'");
Trace.WriteLine($"File content: '{fileData}'");
Trace.WriteLine($"Files count: '{Model.Files.Count}'");
Trace.WriteLine($"File name: '{file.FileName}'");
Trace.WriteLine($"File content: '{fileData}'");

// Assert
// Assert

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

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

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

return NoContent();
}
return NoContent();
}
}
14 changes: 5 additions & 9 deletions src/TestServer/Controllers/StatusController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using Simplify.Web;
using Simplify.Web.Attributes;

namespace TestServer.Controllers
namespace TestServer.Controllers;

[Get("status")]
public class StatusController : Controller
{
[Get("status")]
public class StatusController : Controller
{
public override ControllerResponse Invoke()
{
return Content("Service is running!");
}
}
public override ControllerResponse Invoke() => Content("Service is running!");
}
15 changes: 7 additions & 8 deletions src/TestServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace TestServer
namespace TestServer;

public class Program
{
public class Program
{
public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();
public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
12 changes: 4 additions & 8 deletions src/TestServer/Setup/IocRegistrations.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using Simplify.DI;
using Simplify.Web.Multipart;

namespace TestServer.Setup
namespace TestServer.Setup;

public static class IocRegistrations
{
public static class IocRegistrations
{
public static void Register()
{
DIContainer.Current.RegisterHttpMultipartFormModelBinder();
}
}
public static void Register() => DIContainer.Current.RegisterHttpMultipartFormModelBinder();
}
21 changes: 10 additions & 11 deletions src/TestServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@
using Simplify.Web.Multipart.Model.Binding;
using TestServer.Setup;

namespace TestServer
namespace TestServer;

public class Startup
{
public class Startup
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
IocRegistrations.Register();
IocRegistrations.Register();

if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();

HttpModelHandler.RegisterModelBinder<HttpMultipartFormModelBinder>();
HttpModelHandler.RegisterModelBinder<HttpMultipartFormModelBinder>();

app.UseSimplifyWeb();
app.UseSimplifyWeb();

DIContainer.Current.Verify();
}
DIContainer.Current.Verify();
}
}

0 comments on commit f705ac7

Please sign in to comment.