DotNet 学习笔记 应用状态管理

Application State Options
---------------------------------------------------------------------
*HttpContext.Items
app.Use(async (context, next) =>
{
    // perform some verification
    context.Items["isVerified"] = true;
    await next.Invoke();
});

app.Run(async (context) =>
{
    await context.Response.WriteAsync("Verified request? " + context.Items["isVerified"]);
});
----------------------------------------------------------------------
*QueryString and Post
----------------------------------------------------------------------
*Cookies
----------------------------------------------------------------------
*Session
Microsoft.AspNetCore.Session 
Microsoft.Extensions.Caching.Memory 

services.AddDistributedMemoryCache();
services.AddSession();


app.UseSession();

services.AddSession(options =>
{
  options.CookieName = ".AdventureWorks.Session";
  options.IdleTimeout = TimeSpan.FromSeconds(10);
});

{
    services.AddDistributedMemoryCache();

    services.AddSession(options =>
    {
        options.IdleTimeout = TimeSpan.FromSeconds(10);
    });
}


// main catchall middleware
app.Run(async context =>
{
    RequestEntryCollection collection = GetOrCreateEntries(context);

    if (collection.TotalCount() == 0)
    {
        await context.Response.WriteAsync("<html><body>");
        await context.Response.WriteAsync("Your session has not been established.<br>");
        await context.Response.WriteAsync(DateTime.Now.ToString() + "<br>");
        await context.Response.WriteAsync("<a href="/session">Establish session</a>.<br>");
    }
    else
    {
        collection.RecordRequest(context.Request.PathBase + context.Request.Path);
        SaveEntries(context, collection);

        // Note: it's best to consistently perform all session access before writing anything to response
        await context.Response.WriteAsync("<html><body>");
        await context.Response.WriteAsync("Session Established At: " + context.Session.GetString("StartTime") + "<br>");
        foreach (var entry in collection.Entries)
        {
            await context.Response.WriteAsync("Request: " + entry.Path + " was requested " + entry.Count + " times.<br />");
        }

        await context.Response.WriteAsync("Your session was located, you've visited the site this many times: " + collection.TotalCount() + "<br />");
    }
    await context.Response.WriteAsync("<a href="/untracked">Visit untracked part of application</a>.<br>");
    await context.Response.WriteAsync("</body></html>");
});

public class RequestEntry
{
    public string Path { get; set; }
    public int Count { get; set; }
}
public class RequestEntryCollection
{
    public List<RequestEntry> Entries { get; set; } = new List<RequestEntry>();

    public void RecordRequest(string requestPath)
    {
        var existingEntry = Entries.FirstOrDefault(e => e.Path == requestPath);
        if (existingEntry != null) { existingEntry.Count++; return; }

        var newEntry = new RequestEntry()
        {
            Path = requestPath,
            Count = 1
        };
        Entries.Add(newEntry);
    }

    public int TotalCount()
    {
        return Entries.Sum(e => e.Count);
    }
}

private RequestEntryCollection GetOrCreateEntries(HttpContext context)
{
    RequestEntryCollection collection = null;
    byte[] requestEntriesBytes = context.Session.Get("RequestEntries");

    if (requestEntriesBytes != null && requestEntriesBytes.Length > 0)
    {
        string json = System.Text.Encoding.UTF8.GetString(requestEntriesBytes);
        return JsonConvert.DeserializeObject<RequestEntryCollection>(json);
    }
    if (collection == null)
    {
        collection = new RequestEntryCollection();
    }
    return collection;
}

// establish session
app.Map("/session", subApp =>
{
    subApp.Run(async context =>
    {
        // uncomment the following line and delete session coookie to generate an error due to session access after response has begun
        // await context.Response.WriteAsync("some content");
        RequestEntryCollection collection = GetOrCreateEntries(context);
        collection.RecordRequest(context.Request.PathBase + context.Request.Path);
        SaveEntries(context, collection);
        if (context.Session.GetString("StartTime") == null)
        {
            context.Session.SetString("StartTime", DateTime.Now.ToString());
        }

        await context.Response.WriteAsync("<html><body>");
        await context.Response.WriteAsync($"Counting: You have made {collection.TotalCount()} requests to this application.<br><a href="/">Return</a>");
        await context.Response.WriteAsync("</body></html>");
    });
});


private void SaveEntries(HttpContext context, RequestEntryCollection collection)
{
    string json = JsonConvert.SerializeObject(collection);
    byte[] serializedResult = System.Text.Encoding.UTF8.GetBytes(json);

    context.Session.Set("RequestEntries", serializedResult);
}


// example middleware that does not reference session at all and is configured before app.UseSession()
app.Map("/untracked", subApp =>
{
    subApp.Run(async context =>
    {
        await context.Response.WriteAsync("<html><body>");
        await context.Response.WriteAsync("Requested at: " + DateTime.Now.ToString("hh:mm:ss.ffff") + "<br>");
        await context.Response.WriteAsync("This part of the application isn't referencing Session...<br><a href="/">Return</a>");
        await context.Response.WriteAsync("</body></html>");
    });
});

app.UseSession();
----------------------------------------------------------------------
*Cache
-------------------------------------------------------------------
*Configuration
原文地址:https://www.cnblogs.com/ziranquliu/p/5884213.html