SetObjectAsJson 和 GetObjectFromJson 未显示为 .NET Core 1.0 中 HttpContext.Session 的一部分

Jon*_*ner 0 asp.net-core-mvc visual-studio-2015 asp.net-core

我担心这是一个菜鸟错误,但我一直在寻找,但找不到与此相关的任何内容。

我有一个为 Intranet 应用程序构建的 .NET Core v1.0 Web 应用程序。它已经通过用户测试并部署到生产中。

今天有用户指出了一个小错误。自部署以来,我已经在 VS2015 中迁移了工作区,因此我下载了该项目。在初始构建时,我收到 CS1061 错误,指出这SetObjectAsJson不是ISession. 这是违规代码:

public async Task<IActionResult> Index(DateTime? beginDate, DateTime? endDate, string currentLocation)
    {       

        var userId = this.User.Identity.Name;
        var userEmail = userId.Remove(0, 11) + "@roystonllc.com";
        var querystring = this.HttpContext.Request.QueryString.Value;
        var ex = _context.Engineer.Where(x => x.sEmail == userEmail);
        var engineer = await ex.FirstOrDefaultAsync();
        HttpContext.Session.SetObjectAsJson("Engineer", engineer);
Run Code Online (Sandbox Code Playgroud)

SetObjectAsJson 和 GetObjectFromJson 的所有引用都显示无效。以下是在 Startup 中添加的服务

public void ConfigureServices(IServiceCollection services)
    {

        var connection = @"Server=THESERVER;Database=THEDATABASE;Trusted_Connection=True;Integrated Security=false;User ID=THEUSER;Password=THEPASSWORD";
        services.AddDbContext<SCHEDULEContext>(options => options.UseSqlServer(connection));
        // Add framework services.
        services.AddMvc();
        services.AddMvcGrid();
        services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache
        services.AddSession();


    }
Run Code Online (Sandbox Code Playgroud)

这是我的 project.json:

    {
    "dependencies": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.0",
    "Microsoft.AspNetCore.Razor.Tools": {
    "version": "1.1.0-preview4-final",
   "type": "build"
},
"Microsoft.AspNetCore.Routing": "1.1.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
"Microsoft.Extensions.Configuration.Json": "1.1.0",
"Microsoft.Extensions.Logging.Console": "1.1.0",
"Microsoft.Extensions.Logging.Debug": "1.1.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
"Microsoft.NETCore.App": {
  "version": "1.1.0",
  "type": "platform"
},
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0",
"Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
//"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.1.0-preview4-final",
//"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.1.0-preview4-final"
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
  "version": "1.0.0-preview2-final",
  "type": "build"
},
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.0.0-preview2-final",
"Microsoft.DotNet.InternalAbstractions": "1.0.0",
"NonFactors.Grid.Mvc6": "1.3.0",
"NonFactors.Grid.Core.Mvc6": "1.3.0",
"Microsoft.AspNetCore.Session": "1.1.0",
"Microsoft.AspNet.Http.Abstractions": "1.0.0-rc1-final"
},

    "tools": {
    "BundlerMinifier.Core": "2.2.306",
"Microsoft.AspNetCore.Razor.Tools": "1.1.0-preview4-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.1.0-preview4-final",
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
  "version": "1.0.0-preview2-final",
  "imports": [
    "portable-net45+win8"
  ]
}
//"Microsoft.VisualStudio.Web.CodeGeneration.Tools": "1.1.0-preview4-final"
},

"frameworks": {
  "netcoreapp1.0": {
    "imports": [
      "dotnet5.6",
      "portable-net45+win8"
    ]
  }
},

"buildOptions": {
  "emitEntryPoint": true,
  "preserveCompilationContext": true
},

"runtimeOptions": {
  "configProperties": {
    "System.GC.Server": true
  }
},

"publishOptions": {
  "include": [
    "wwwroot",
    "**/*.cshtml",
    "appsettings.json",
    "web.config"
  ]
},

"scripts": {
  "prepublish": [ "bower install", "dotnet bundle" ],
  "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}
Run Code Online (Sandbox Code Playgroud)

roc*_*cky 7

SetObjectAsJson并且GetObjectFromJson不是 ASP.NET Core 的一部分。它们是扩展方法,从起源大概这个文章。尝试通过您的解决方案进行文本搜索,也许他们所在的库没有正确引用。

否则,将以下类添加到您的项目中:

public static class SessionExtensions
{
    public static void SetObjectAsJson(this ISession session, string key, object value)
    {
        session.SetString(key, JsonConvert.SerializeObject(value));
    }

    public static T GetObjectFromJson<T>(this ISession session, string key)
    {
        var value = session.GetString(key);

        return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,代码 ( JsonConvert) 取决于 Newtonsoft.Json NuGet 包,因此您可能还必须添加该引用。