Search code examples
c#dynamic-programmingsystem.reflectionsignalrcore

Can load file or assembly while importing dll dynamically


I'm trying to write a dotnet core console program, that dynamically loads specified dll file to get type appeared in dll.

My dll project looks like this:

SignlaR Hub class:

namespace GameServer
{
    public class MyHub : Hub
    {
    }
}

StartUp class:

namespace GameServer
{
    public class StartUp
    {
        public IConfiguration Configuration { get; private set; }

        public StartUp(IConfiguration config)
        {
            Configuration = config;
        }

        public void ConfigureServices(IServiceCollection services)
        {
         
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();

            app.UseAuthentication();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<MyHub>("/hub");
            });
        }
    }
}

And Program Class:

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

        public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://localhost:5005")
                .UseStartup<StartUp>();
        }
    }
}

All of this are in same project and I build it to generate .dll file. After that I wrote a dotnet core console program to dynamically include that dll and get type of classes I've got. This is how it looks like:

public class Program
    {
        public static void Main(string[] args)
        {
            var dll = Assembly.LoadFile(@"C:\Users\Dreamer\Desktop\git Repos\GameServer\bin\Debug\net5.0\GameServer.dll");
            foreach(Type type in dll.GetExportedTypes())
            {
                Console.WriteLine(type);
            }

        }
    }

But when I run this code it throws exception. exception image.

It's cause of creating signal hub class, can't get it's type. When I delete that class it works completely fine. I tried to add that "missing package" from nuget in dll program, but it still doesn't work. Moreover, package-Microsoft.AspNetCore.SignalR.Core version 5.0 (as exception says) doesn't exist, in nuget packages this package's version is 1.0.

I couldn't find any work around so if anyone has clue please let me know.

Thanks in advance.


Solution

  • Solved: It's .net core console program so it needs some includes in .csproj file. Original .dll file was using .net core built-in signalr core namespace, which is not installed from nuget so it was using locally downloaded microsoft .dlls.

    In the program's .csproj file I had to include following code:

    <ItemGroup>
            <FrameworkReference Include="Microsoft.AspNetCore.App" />
    </ItemGroup>
    

    So that both programs could read .dll from same place, which was not included in original .dll.