Skip to content

Instantly share code, notes, and snippets.

View smartliubo's full-sized avatar
🎯
Focusing

smart smartliubo

🎯
Focusing
View GitHub Profile
@smartliubo
smartliubo / MaxHeap
Created September 30, 2021 08:13
Use Array Implements the MaxHeap
public class MaxHeap
{
private int[] heap = new int[1000]; // use 1 as the start index to record the heap elements
private int heapSize = 0;
public MaxHeap() { }
public void Push(int x)
{
heap[++heapSize] = x;
@smartliubo
smartliubo / AsyncFlush、AsyncDispose
Created June 22, 2021 07:05
异步释放资源、异步处理缓冲区
app.Run(async context =>
{
// 显示使用异步AsyncDispose来调用异步FlushAsync()
await using (var streamWriter = new StreamWriter(context.Response.Body))
{
await streamWriter.WriteAsync("Hello World");
}
});
app.Run(async context =>
@smartliubo
smartliubo / Response Header
Created June 22, 2021 02:49
Http响应头
# method1
if(!context.Response.HasStarted)
{
context.Response.Headers["test"] = "value";
}
# method2
context.Response.OnStarting(()=>
{
context.Response.Headers["someheader"] = "somevalue";
@smartliubo
smartliubo / double buffering
Last active June 21, 2021 10:48
双重缓冲
// 使用HttpCompletionOption.ResponseHeadersRead避免缓冲整个响应体至内存中
using (var response = await _client.GetAsync(_url, HttpCompletionOption.ResponseHeadersRead))
{
// 获取响应流
var responseStream = await response.Content.ReadAsStreamAsync();
// 创建StreamReader、JsonTextReader
// 做了一些缓冲,不是双缓冲
var textReader = new StreamReader(responseStream);
var reader = new JsonTextReader(textReader);
@smartliubo
smartliubo / RoleManager
Created June 19, 2021 06:02
角色管理
public class RoleManager<TRole> :IDisposable where TRole : class
{
private bool _disposed;
public virtual CancellationToken CancellationToken => CancellationToken.None;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
public class Result
{
private static readonly Result _success = new Result { Succeeded = true };
private List<Error> _errors = new List<Error>();
public bool Succeeded { get;protected set;}
public IEnumerable<Error> Errors => _errors;
public static Result Success => _success;
# internal 仅在程序集内部使用
internal static class LoggingExtensions
{
private static readonly Action<ILogger,Exception> _invalidException;
static LoggingExtensions()
{
_invalidException = LoggerMessage.Define(eventId:new EventId(0,"InvalidExceptionOccur"),
logLevel:LogLevel.Debug,
formatString:"ValidateAsync Failed: did not find expect '{Actual}' purpose");
}
@smartliubo
smartliubo / Ioc、DI、
Created June 16, 2021 02:41
使用Ioc注入的服务存在依赖项时的解决办法
# good example
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
// 通过配置选项来解决强依赖关系
services.AddOptions<CookieAuthenticationOptions>(
CookieAuthenticationDefaults.AuthenticationScheme)
.Configure<IMyService>((options, myService) =>
@smartliubo
smartliubo / Task
Created May 28, 2021 01:50
任务扩展
public static class TaskExtensions
{
public static async Task RunAndForget(
this Task task,Action<Exception> onException =null)
{
try
{
await task;
}
catch(Exception ex)
@smartliubo
smartliubo / PInvoke、Network
Created May 27, 2021 10:33
Access Network share file using PInvoke
public class NetworkConnection : IDisposable
{
string _networkName;
public NetworkConnection(string networkName,NetworkCredential credentials)
{
_networkName = networkName;
var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,