Skip to content

Instantly share code, notes, and snippets.

@fabiomaulo
fabiomaulo / ThreexPlusOne.cs
Created November 5, 2022 23:04
The famous 3X + 1
internal class Program
{
private const ulong limit = 1;
private static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("No start number specified");
return;
}
@fabiomaulo
fabiomaulo / HtmlFileResult.cs
Created May 29, 2022 18:00
Return a view as download html file (netcore 3.1)
public class HtmlFileResult : ActionResult
{
private readonly string fileDownloadName;
private readonly ViewResult viewToRender;
public HtmlFileResult(ViewResult viewToRender, string fileDownloadName)
{
if (string.IsNullOrWhiteSpace(fileDownloadName))
{
throw new ArgumentException($"'{nameof(fileDownloadName)}' cannot be null or whitespace.", nameof(fileDownloadName));
// Little extension to do what I need for OpenApi
/// <summary>
/// Specifies the type of the value, depending on status code and MIME-Types, returned by the action
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = false)]
public class ProducesResponseAttribute : Attribute
{
public ProducesResponseAttribute(Type type, int statusCode = 200)
{
Type = type;
@fabiomaulo
fabiomaulo / HtmlFileResult.cs
Created April 3, 2020 03:47
Return a view as download html file
public class HtmlFileResult : FileResult
{
private readonly ControllerContext controllerContext;
private readonly string viewToRender;
private readonly object model;
public HtmlFileResult(ControllerContext controllerContext, string viewToRender, object model, string fileDownloadName) : base("text/html")
{
if (string.IsNullOrWhiteSpace(viewToRender))
{
@fabiomaulo
fabiomaulo / IViewModelValidator.cs
Created November 22, 2019 20:17
Data Annotations Validator
public interface IViewModelValidator
{
bool IsValid(object model);
}
public class DataAnnotationsValidator : IViewModelValidator
{
private class ReferenceEqualityComparer : IEqualityComparer<object>
{
public new bool Equals(object x, object y)
@fabiomaulo
fabiomaulo / JsonpResult.cs
Created October 13, 2018 19:32
Jsonp Action Result
public class JsonpResult : JsonResult
{
private const string NullCallbackExceptionMessage = "Callback cannot be null.";
private const string NullContextExceptionMessage = "Context cannot be null.";
private const string JsonpCallbackFormat = "{0}({1});";
private const string JsonpContentType = "application/javascript";
private const string InvalidOperationExceptionMessage =
"This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.";
@fabiomaulo
fabiomaulo / Mediator.cs
Last active October 2, 2018 11:18
The Meditor framework
/// <summary>
/// Define an object that encapsulates how a set of objects interact.
/// Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
/// </summary>
public interface IMediator
{
/// <summary>
/// Perform a query to the "model".
/// </summary>
/// <typeparam name="TRequest">The type of the request.</typeparam>
@fabiomaulo
fabiomaulo / MonitorController.cs
Created October 10, 2017 18:08
Monitor enabled routes ASPNET core
[Route("monitor")]
public class MonitorController : Controller
{
private readonly IActionDescriptorCollectionProvider _provider;
public MonitorController(IActionDescriptorCollectionProvider provider)
{
_provider = provider;
}
@fabiomaulo
fabiomaulo / ActiveDirectoryAuthHandler.cs
Created October 8, 2017 19:52
HttpClient handler with Auth using AAD
/// <summary>
/// AuthHandler for AAD
/// </summary>
public class ActiveDirectoryAuthHandler : DelegatingHandler
{
private const int maxAuthRetry = 3;
private readonly AuthenticationContext authContext;
private readonly ClientCredential clientCredential;
private readonly string appIdUri;
public class ContainerBasedJobActivator : IJobActivator
{
private readonly IDepencencyInjectionContainer container;
public ContainerBasedJobActivator(IDepencencyInjectionContainer container)
{
if (container == null) throw new ArgumentNullException(nameof(container));
this.container = container;
}
public T CreateInstance<T>()