C# 我可以在请求之间保留一个不可序列化的对象吗?

C# 我可以在请求之间保留一个不可序列化的对象吗?,c#,asp.net-core,asp.net-core-webapi,C#,Asp.net Core,Asp.net Core Webapi,我已经创建了一个.NET核心API项目。它可以工作,但会为每个get请求创建一个新对象。在哪里可以保存对象以便重用?我搜索过谷歌,找到了Session,但它似乎只存储字节或序列化字节。我想我无法序列化对象 如果DoSomeInitialization需要1秒,则在arg1相同的情况下,在每个get请求中创建一个新的SomeLibrary似乎效率不高,并且SomeLibrary不可序列化,因此无法将其存储到会话中 请求 httpx://..../dog/1 httpx://..../dog/2 h

我已经创建了一个.NET核心API项目。它可以工作,但会为每个get请求创建一个新对象。在哪里可以保存对象以便重用?我搜索过谷歌,找到了Session,但它似乎只存储字节或序列化字节。我想我无法序列化对象

如果DoSomeInitialization需要1秒,则在arg1相同的情况下,在每个get请求中创建一个新的SomeLibrary似乎效率不高,并且SomeLibrary不可序列化,因此无法将其存储到会话中

请求

httpx://..../dog/1 httpx://..../dog/2 httpx://..../dog/3 httpx://..../cat/1 httpx://..../cat/2
我可以在前三个请求中重复使用相同的SomeLibrary对象。

您可以尝试使用singleton模式,但您应该考虑它是否适合您的需求,因为singleton模式不是所有类的好方法

 public class Singleton
    {
        private static string _locker = new object();
        private static Singleton _instance = null;
        public string SerializedString="";

        ///Constructore is called just one time !
        public Singleton(arg1){
         DoSerialization(arg1);
         }
        public static Singleton Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_locker)
                    {
                        if (_instance == null)
                        {
                            _instance = new Singleton();
                        }
                    }
                }
                return _instance;
            }
        }
    private void DoSerialization(arg1){
      this.SerializedString=JsonConvert.SerializeObject(arg1)
     }
    }
像这样使用它

var SameInstance=new Singleton(arg1).Instance;
var serialized=SameInstance.SerializedString;

我想您在项目中使用DI。首先阅读本文。然后您将认识到,您可以使用以下代码仅注册每个应用程序的一个实例:

//Startup.cs
services.AddSingleton<ISomeLibrary>(new SomeLibrary());

....

//controller
public SomeController : Controller
{
    private readonly ISomeLibrary _sl;

    public SomeController(ISomeLibrary sl)
    {
        _sl = sl;
    }

    ...
}
更新:

根据您最后的评论,您可以这样实现它:

public interface ISmthResolver
{
    ISomeLibrary Get(string arg);
}

...

public class SmthResolver : ISmthResolver
{
    private readonly ConcurrentDictionary<string, Lazy<ISomeLibrary>> _instances = new ConcurrentDictionary<string, Lazy<ISomeLibrary>>();

    public ISomeLibrary Get(string arg) => _instances.GetOrAdd(arg, key => new Lazy<ISomeLibrary>(() => new SomeLibrary())).Value;
}

...

services.AddSingleton<SmthResolver, ISmthResolver>();

...

public class Some
{
    public Some(ISmthResolver sr)
    {
        var sl = sr.Get("arg");
    }
}

也请阅读此文。

使用控制器的静态成员来保存对象如何?我考虑过,但这是一种推荐的方法吗?考虑过在SomeLibrary对象上使用单例规范进行依赖项注入吗?它会起作用,但在考虑推荐的方法时,因为这是Dandre建议的asp核心,您可以使用Singleton服务并将其注入控制器。问题是,我想缓存SomeLibrary的多个实例。如我的OP中所示,我想缓存一个用于dog,一个用于cat,因此在以后的请求中,当arg为dog或cat时,我不会创建新的SomeLibrary。如果我使用这个DI patthern,我是否应该定义/创建一个可以保存SomeLibrary的多个实例的对象,例如,一个具有Dictionary字段的类,并使用它来添加singleton?
public interface ISmthResolver
{
    ISomeLibrary Get(string arg);
}

...

public class SmthResolver : ISmthResolver
{
    private readonly ConcurrentDictionary<string, Lazy<ISomeLibrary>> _instances = new ConcurrentDictionary<string, Lazy<ISomeLibrary>>();

    public ISomeLibrary Get(string arg) => _instances.GetOrAdd(arg, key => new Lazy<ISomeLibrary>(() => new SomeLibrary())).Value;
}

...

services.AddSingleton<SmthResolver, ISmthResolver>();

...

public class Some
{
    public Some(ISmthResolver sr)
    {
        var sl = sr.Get("arg");
    }
}