果园核心问题以编程方式添加模块

时间:2021-05-28 10:55:55

标签: c# asp.net-core asp.net-core-mvc modularity orchardcore

在我的项目中,我必须为我找到的项目构建一个核心和模块 OrchardCore 但问题是: 我想构建一个上传模块(带有 .nupkg 类型的包)并安装到我在服务器上发布的项目中的平台 现在我有一个简单的项目: enter image description here => 我的解决方案资源管理器 我的核心startup.cs:

    using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading.Tasks;

namespace modularv001
{
    public class Startup
    {
        private IList<AssemblyHanlder> components = new List<AssemblyHanlder>();
        private void GetallModules()
        {
            var rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string InstallPath = Path.Combine(rootPath, "InstalledModules");
            rootPath = Path.Combine(rootPath, "container");
            foreach (string file in Directory.EnumerateFiles(rootPath, "*.nupkg", SearchOption.AllDirectories))
            {
                using (ZipArchive nuget = ZipFile.Open(file, ZipArchiveMode.Read))
                {
                    foreach (ZipArchiveEntry entry in nuget.Entries)
                    {
                        if (entry.Name.Contains(".dll"))
                        {
                            string createPathSource = InstallPath + "\\" + entry.Name;

                            using (BinaryReader reader = new BinaryReader(entry.Open()))
                            {
                                // Step 1
                                using (FileStream fsNew = new FileStream(createPathSource, FileMode.Create, FileAccess.Write))
                                {
                                    fsNew.Write(reader.ReadBytes((int)entry.Length), 0, (int)entry.Length);
                                }

                                // Step 2
                                using (FileStream readStream = File.Open(createPathSource, FileMode.Open))
                                {
                                    var assempbly = AssemblyLoadContext.Default.LoadFromStream(readStream);
                                    if (entry.Name.Contains(".Views.dll"))
                                    {
                                        components.Add(new AssemblyHanlder()
                                        {
                                            components = assempbly,
                                            isView = true
                                        });
                                    }
                                    else
                                    {
                                        components.Add(new AssemblyHanlder()
                                        {
                                            components = assempbly,
                                            isView = false
                                        });
                                    }

                                }
                            }
                        }





                    }
                }
            }
        }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOrchardCore().AddMvc();
            GetallModules();
            /*=======================================================
             
             
             load module in  private IList<AssemblyHanlder> components = new List<AssemblyHanlder>();
                 internal class AssemblyHanlder
                {
                    public Assembly components { get; set; }
                    public bool isView { get; set; }
                 }
             
             ==========================================================*/

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();
            app.UseOrchardCore();
        }
    }
}

我的核心 Program.cs :

  using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace modularv001
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

 

我的模块 program.cs :

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Module1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

我的模块startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Module1
{
    public class Startup
    {
        public void Configure(IEndpointRouteBuilder endpoints)
        {

        }
    }
}

现在我很困惑,当核心在服务器上并且所有应用程序都在线时,如何将我的模块从 .nupkg 文件安装到项目中

0 个答案:

没有答案