从ASP.NET Core View引用NET461 DLL模型类型

时间:2016-05-30 14:10:27

标签: asp.net-core .net-core-rc2

我有一个新的ASP.NET Core RC2,如下所示:

  • 存储在.NET Framework 461的DLL库中的模型
  • Web是MVC Core 1.0 Full Framework - 不基于核心框架

当引用DLL库中的模型类型并运行项目时,我收到以下错误:

  

编译所需资源时发生错误   处理此请求。请查看以下特定错误   详细说明并适当修改源代码。生成的代码   类型或命名空间名称'数据'在命名空间中不存在   ' ADMA.EWRS' (你错过了一个程序集引用吗?)   23. public class _Views_Murad_Index_cshtml:Microsoft.AspNetCore.Mvc.Razor.RazorPage>   类型或命名空间名称'数据'在命名空间中不存在   ' ADMA.EWRS' (你错过了一个程序集引用吗?)   39. public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper>   Html {get;私人集;编译期间发生错误   处理此请求所需的资源。请查看   遵循特定的错误详细信息并修改您的源代码   适当。生成的代码类型或命名空间名称'数据'不   命名空间中不存在' ADMA.EWRS' (你错过了一个集会吗?   参考?)   23. public class _Views_Murad_Index_cshtml:Microsoft.AspNetCore.Mvc.Razor.RazorPage>   类型或命名空间名称'数据'在命名空间中不存在   ' ADMA.EWRS' (你错过了一个程序集引用吗?)   39. public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper>   Html {get;私人集; }

将Project.json配置如下:

{
  "dependencies": {
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
    "Microsoft.AspNetCore.Authorization": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Session": "1.0.0-rc2-final",
    "ADMA.EWRS.Web.Security": "1.0.0-*"
  },

  "tools": {
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },

  "frameworks": {
    "net461": {
      "dependencies": {
        "ADMA.EWRS.BizDomain": {
          "target": "project"
        },
        "ADMA.EWRS.Data.Models": {
          "target": "project"
        }
      }
    }
  },


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

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Solution Explorer

X Project

1 个答案:

答案 0 :(得分:1)

添加Razor选项后解决,如下面的代码:

 services.AddMvc().// Murad Add this for RC2, remove it if release 1.0 after June 
                AddRazorOptions(options =>
            {
                var previous = options.CompilationCallback;
                options.CompilationCallback = context =>
                {
                    previous?.Invoke(context);
                    context.Compilation = context.Compilation.AddReferences(Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ADMA.EWRS.Data.Models.Murad).Assembly.Location));
                };
            });

            //var myAssemblies = AppDomain.CurrentDomain.GetAssemblies().Select(x => Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(x.Location)).ToList();
            //services.Configure((Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions options) =>
            //{
            //    var previous = options.CompilationCallback;
            //    options.CompilationCallback = (context) =>
            //    {
            //        previous?.Invoke(context);

            //        context.Compilation = context.Compilation.AddReferences(myAssemblies);
            //    };
            //});

检查

https://github.com/aspnet/Mvc/issues/4686