小编mat*_*lto的帖子

.Net Core MemoryCache PostEvictionCallback无法正常工作

我在Microsoft.Extensions.Caching.Memory.MemoryCache中设置了具有滑动过期的缓存项.我想在每次缓存项到期时触发回调,但在我查询缓存中过期的缓存项之前,不会触发回调.

这是代码:

using System;
using Microsoft.Extensions.Caching.Memory;

namespace Memcache
{
    public class Program
    {
        private static MemoryCache _cache;
        private static int _cacheExpSecs;

        public static void Main(string[] args)
        {
            _cache = new MemoryCache(new MemoryCacheOptions());
            _cacheExpSecs = 2;

            var cacheEntryOptions = new MemoryCacheEntryOptions()
            .SetSlidingExpiration(TimeSpan.FromSeconds(_cacheExpSecs))
            .RegisterPostEvictionCallback(callback: EvictionCallback);

            _cache.Set(1, "One", cacheEntryOptions);
            _cache.Set(2, "Two", cacheEntryOptions);

            var autoEvent = new System.Threading.AutoResetEvent(false);

            System.Threading.Timer timer = new System.Threading.Timer(checkCache, autoEvent, 1000, 6000);

            Console.Read();
        }

        private static void checkCache(Object o)
        {
            if(_cache.Get(1)!=null)
            {
                Console.WriteLine(string.Format(@"checkCache: Cache with key {0} will be removed …
Run Code Online (Sandbox Code Playgroud)

.net caching memorycache asp.net-core

7
推荐指数
2
解决办法
3897
查看次数

JsonIgnore 属性在 ASP.NET Core 3 中保持序列化属性

我最近将我的 API 项目更新为 ASP.NET Core 3。从那时起,[JsonIgnore]属性不起作用:

public class Diagnostico
{
    [JsonIgnore]
    public int TipoDiagnostico { get; set; }

    [JsonIgnore]
    public int Orden { get; set; }

    [JsonIgnore]
    public DateTime? FechaInicio { get; set; }

    public string TipoCodificacion { get; set; }

    public string Codigo { get; set; }

    public string Descripcion { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

类的所有属性都被序列化。API 端点在 .NET Core 3 中,但所有逻辑都在 .NET Standard 2.1 中。我意识到序列化程序已从 更改Newtonsoft.JsonSystem.Text.Json。此包在 .NET Standard 2.1 中不可用(它仅适用于 .NET Core),因此要[JsonIgnore] …

c# json json.net asp.net-core asp.net-core-3.0

7
推荐指数
1
解决办法
4262
查看次数

如何将一个Task存储在字典中以便以后在C#中执行

我正在尝试将一个Task存储在字典中以便以后执行,但没有成功:

Dictionary<string, Task<object>> router = new Dictionary<string, Task<object>();
router["xyz"] = functionToLaterExec

async Task<string> functionToLaterExec()
{
    return await Task.FromResult("Success!");
} 
Run Code Online (Sandbox Code Playgroud)

如果我将该函数声明为

string functionToExec()
Run Code Online (Sandbox Code Playgroud)

然后我可以这样做:

router["xyz"] = new Task<string>(functionToExec);
Run Code Online (Sandbox Code Playgroud)

但我需要该函数是异步的.并且不知道如何调用它.我试过了:

var t = router["xyz"];
var data = Encoding.UTF8.GetBytes(await t);
Run Code Online (Sandbox Code Playgroud)

没有成功.

谢谢.

c# dictionary dynamic

3
推荐指数
1
解决办法
962
查看次数