Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 需要在Http服务器启动时传递ds(数据结构),并使该ds在控制器之间成为全局的_Asp.net Mvc_Asp.net Web Api_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

Asp.net mvc 需要在Http服务器启动时传递ds(数据结构),并使该ds在控制器之间成为全局的

Asp.net mvc 需要在Http服务器启动时传递ds(数据结构),并使该ds在控制器之间成为全局的,asp.net-mvc,asp.net-web-api,asp.net-core,asp.net-core-mvc,Asp.net Mvc,Asp.net Web Api,Asp.net Core,Asp.net Core Mvc,所以 下面是代码设置 有一个驱动程序应用程序,它启动HTTP服务器(ASP.NET核心Web API项目) 驱动程序应用程序为启动HTTP服务器而调用的方法是 这: 公共类Http\u服务器 { 公共静态ConcurrentQueue cq=新ConcurrentQueue(); 公共静态void InitHttpServer(ConcurrentQueue队列) { cq=队列; var host=new WebHostBuilder() .UseKestrel() .UseContentR

所以

下面是代码设置

有一个驱动程序应用程序,它启动HTTP服务器(ASP.NET核心Web API项目)

驱动程序应用程序为启动HTTP服务器而调用的方法是 这:

公共类Http\u服务器
{
公共静态ConcurrentQueue cq=新ConcurrentQueue();
公共静态void InitHttpServer(ConcurrentQueue队列)
{ 
cq=队列;
var host=new WebHostBuilder()
.UseKestrel()
.UseContentRoot(目录.GetCurrentDirectory())
.Useii整合()
.UseStartup()
.UseApplicationInsights()
.Build();
host.Run();
}
}
控制器操作任务:

[HttpPost]
[路线(“XYZ”)]
公共虚拟IActionResult AddXYZ([FromBody]列表资源成员)
{
//在ds中添加一些内容
Http_Server.cq.Enqueue(新对象());
//回击
返回新的ObjectResult(示例);
}
传递的数据结构(并发队列)将在控制器级别可见(就像可在所有控制器上访问的全局变量)

将ds设为静态变量并跨控制器访问是否合适


或者有没有办法将这些ds传递到不同的层

这是我在寻找更好的解决方案。你想做的似乎不是最好的方法

首先,您希望通过调用应用程序
StartUp.ConfigureServices
方法中的
AddMemoryCache
来启用应用程序中的缓存

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMemoryCache();
    ...
}
然后,您要使用缓存。像这样的事情会让你走上正确的方向

public class XYZController : Controller {
    private IMemoryCache _memoryCache;
    private const string xyzCacheKey = "XYZ";

    public XYZController(IMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }

    [HttpPost("XYZ")]
    public IActionResult AddXYZ([FromBody]ResourceMember[] resourceMembers)
    {
        try
        {
            if (!_memoryCache.TryGetValue(xyzCacheKey, out ConcurrentQueue<Object> xyz))
            {
                xyz = new ConcurrentQueue<Object>();
                _memoryCache.Set(xyzCacheKey, xyz, new MemoryCacheEntryOptions()
                {
                    SlidingExpiration = new TimeSpan(24, 0, 0)
                });
            }
            xyz.Enqueue(resourceMembers);

            return Ok();
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
    }
}

public class ResourceMember { }
公共类XYZController:控制器{
私有IMemoryCache\u memoryCache;
私有常量字符串xyzCacheKey=“XYZ”;
公共XYZ控制器(IMemoryCache memoryCache)
{
_memoryCache=memoryCache;
}
[HttpPost(“XYZ”)]
public IActionResult AddXYZ([FromBody]ResourceMember[]resourceMembers)
{
尝试
{
if(!\u memoryCache.TryGetValue(xyzCacheKey,out ConcurrentQueue xyz))
{
xyz=新的ConcurrentQueue();
_Set(xyzCacheKey,xyz,新的MemoryCacheEntryOptions()
{
SlidingExpiration=新的时间跨度(24,0,0)
});
}
xyz.排队(资源成员);
返回Ok();
}
捕获(例外情况除外)
{
返回请求(ex);
}
}
}
公共类资源成员{}
这样做的目的是允许您使用a来保存对象,以及您在
ConcurrentQueue
对象中的
Enqueue
对象,如果您将其作为缓存中的主要对象。现在,您可以在
MemoryCache
中缓存任何对象类型,并根据添加到缓存时提供的键在需要时提取该值。在上面的例子中,我创建了一个名为
xyzCacheKey
的常量,字符串值为XYZ,用作键

你正在尝试的静态全局变量不是。。。好

如果这没有帮助,请在评论中告诉我,我将删除答案


祝你好运

这是我在寻找更好的解决方案。你想做的似乎不是最好的方法

首先,您希望通过调用应用程序
StartUp.ConfigureServices
方法中的
AddMemoryCache
来启用应用程序中的缓存

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMemoryCache();
    ...
}
然后,您要使用缓存。像这样的事情会让你走上正确的方向

public class XYZController : Controller {
    private IMemoryCache _memoryCache;
    private const string xyzCacheKey = "XYZ";

    public XYZController(IMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }

    [HttpPost("XYZ")]
    public IActionResult AddXYZ([FromBody]ResourceMember[] resourceMembers)
    {
        try
        {
            if (!_memoryCache.TryGetValue(xyzCacheKey, out ConcurrentQueue<Object> xyz))
            {
                xyz = new ConcurrentQueue<Object>();
                _memoryCache.Set(xyzCacheKey, xyz, new MemoryCacheEntryOptions()
                {
                    SlidingExpiration = new TimeSpan(24, 0, 0)
                });
            }
            xyz.Enqueue(resourceMembers);

            return Ok();
        }
        catch (Exception ex)
        {
            return BadRequest(ex);
        }
    }
}

public class ResourceMember { }
公共类XYZController:控制器{
私有IMemoryCache\u memoryCache;
私有常量字符串xyzCacheKey=“XYZ”;
公共XYZ控制器(IMemoryCache memoryCache)
{
_memoryCache=memoryCache;
}
[HttpPost(“XYZ”)]
public IActionResult AddXYZ([FromBody]ResourceMember[]resourceMembers)
{
尝试
{
if(!\u memoryCache.TryGetValue(xyzCacheKey,out ConcurrentQueue xyz))
{
xyz=新的ConcurrentQueue();
_Set(xyzCacheKey,xyz,新的MemoryCacheEntryOptions()
{
SlidingExpiration=新的时间跨度(24,0,0)
});
}
xyz.排队(资源成员);
返回Ok();
}
捕获(例外情况除外)
{
返回请求(ex);
}
}
}
公共类资源成员{}
这样做的目的是允许您使用a来保存对象,以及您在
ConcurrentQueue
对象中的
Enqueue
对象,如果您将其作为缓存中的主要对象。现在,您可以在
MemoryCache
中缓存任何对象类型,并根据添加到缓存时提供的键在需要时提取该值。在上面的例子中,我创建了一个名为
xyzCacheKey
的常量,字符串值为XYZ,用作键

你正在尝试的静态全局变量不是。。。好

如果这没有帮助,请在评论中告诉我,我将删除答案


祝你好运

我对并发队列不是很熟悉,但是,为什么不把这个DS作为一个服务,并在ASP.NET内核中使用DI使它对控制器可用呢。我想你需要把这一切变成一个单身服务。谢谢!这样看起来更好。我不太熟悉并发队列,但是,为什么不将此DS作为一个服务,并在ASP.NET核心中使用DI使其可供控制器使用呢。我想你需要把这一切变成一个单身服务。谢谢!这样看起来更好。