Skip to content

Instantly share code, notes, and snippets.

View NDiiong's full-sized avatar
😀
Hi there!

duong.nb NDiiong

😀
Hi there!
View GitHub Profile
@NDiiong
NDiiong / ResponseCompression.cs
Created July 16, 2020 02:57
Response Compression .Net Core
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<BrotliCompressionProvider>();
});
services.Configure<BrotliCompressionProviderOptions>(options => options.Level = CompressionLevel.Optimal);
}
@NDiiong
NDiiong / ServiceCollectionExtensions-Register.cs
Last active September 11, 2020 04:55
Remove IServiceCollection
public static partial class ServiceCollectionExtensions
{
//Ref: https://stackoverflow.com/questions/49087739/net-core-register-raw-generic-with-different-number-of-parameters
public static IServiceCollection Scan(this IServiceCollection serviceCollection, IEnumerable<Assembly> assemblies, Type interfaceType, ServiceLifetime lifetime)
{
foreach (var type in assemblies.SelectMany(x => x.GetTypes().Where(t => t.IsClass && !t.IsAbstract)))
{
foreach (var i in type.GetInterfaces())
{
// Check for generic
public static class WatchHelper
{
public static void Track(Action action, ILogger logger, string message, string logFile)
{
message = message.PadRight(100, '.');
var sw = Stopwatch.StartNew();
try
{
action?.Invoke();
}
@NDiiong
NDiiong / ValidateXmlWithXsd.cs
Created July 29, 2020 12:01 — forked from automatonic/ValidateXmlWithXsd.cs
Programmatically validate an XML document with an XSD (XML Schema)
//License: MIT
//using System.Xml;
//using System.Xml.Schema;
public static List<ValidationEventArgs> ValidateXmlWithXsd(string xmlPath, string xsdPath)
{
XmlSchemaSet s = new XmlSchemaSet();
var args = new List<ValidationEventArgs>();
using (XmlReader reader = XmlReader.Create(xsdPath))
{
//These validations will be from reading the xsd itself
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
namespace XSD_fun
{
class Program
{
@NDiiong
NDiiong / HttpClient Sample2.cs
Created July 30, 2020 05:52 — forked from tohnaman/HttpClient Sample2.cs
HttpClient Sample2
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var param = new Dictionary<string, string>
{
{
"title", title
}
};
var query = await new FormUrlEncodedContent(param).ReadAsStringAsync();
public class JsonClient : IRestClient
{
private IJsonSerializer serializer;
private HttpClient httpClient;
protected virtual HttpClient HttpClient
{
get
{
return this.httpClient ?? (this.httpClient = new HttpClient());
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading.Tasks;
namespace Infrastructure.Services
{
public class HttpClientService : IHttpClientService
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Converters;
namespace ArchDev
{
public static class HttpClientExtensions
@NDiiong
NDiiong / HttpClientSample.cs
Created July 30, 2020 06:05 — forked from tohnaman/HttpClientSample.cs
HttpClient Sample
private static HttpClient client = new HttpClient();
// GET
var response = await client.GetAsync(@"http://localhost/admin");
var cookies = response.Headers.FirstOrDefault(pair => String.Compare(pair.Key, @"Set-Cookie", StringComparison.OrdinalIgnoreCase) == 0).Value;
// get token from html
var html = await response.Content.ReadAsStringAsync();
var regex = new Regex("(name=\\\"_token\\\" value=)\\\"(.*)\\\"");
var token = regex.Match(html).Groups[2].Value;