Skip to content

Instantly share code, notes, and snippets.

var orders = await context.Orders
.AsNoTracking()
.Include(o => o.Customer)
.Where(o => o.OrderDate > DateTime.Now.AddDays(-30))
.ToListAsync();
// Without tracking - better performance for read-only scenarios
var customers = await context.Customers
.AsNoTracking()
.ToListAsync();
[Test]
public async Task ValidateAsync_WithEICARTestFile_ReturnsFail()
{
// EICAR test string - universally detected as malware
const string eicarString = @"X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";
var eicarBytes = Encoding.UTF8.GetBytes(eicarString);
var mockFile = new Mock<IFormFile>();
mockFile.Setup(f => f.FileName).Returns("eicar.txt");
using System.Runtime.InteropServices;
public sealed class AmsiContext : IDisposable
{
private IntPtr _handle;
private AmsiContext(IntPtr handle)
{
_handle = handle;
}
public sealed class MalwareScanValidationStep : IValidationStep
{
private readonly ILogger _logger;
public MalwareScanValidationStep(ILogger logger)
{
_logger = logger;
}
public async Task<ValidationResult> ValidateAsync(IFormFile file, CancellationToken token)
public static ValidationPipeline CreateDefaultPipeline(ILogger logger)
{
const long maxFileSize = 128L * 1024 * 1024; // 128 MB
var pipeline = new ValidationPipeline(
[
new FileSizeValidationStep(maxFileSize, logger), // 1. Check size
new ContentTypeValidationStep(SupportedTypes, logger), // 2. Check claimed type
new FileSignatureValidationStep(SupportedTypes, logger), // 3. Verify actual type
new MalwareScanValidationStep(logger) // 4. Scan for malware
]);
fileStream.Position = 0; // Reset for subsequent operations
public static class FileSignatureValidator
{
public static bool IsValid(
IFileFormatInspector inspector,
Stream fileStream,
string fileName,
SupportedContentTypes supportedContentTypes,
ILogger logger)
{
try
var allFormats = FileFormatLocator.GetFormats(this.GetType().Assembly, true);
public sealed class FileSignatureValidationStep : IValidationStep
{
private readonly SupportedContentTypes _supportedContentTypes;
private readonly IFileFormatInspector _fileFormatInspector;
private readonly ILogger _logger;
public FileSignatureValidationStep(SupportedContentTypes supportedContentTypes, ILogger logger)
{
_supportedContentTypes = supportedContentTypes;