.net6.0引用的dll放置单独的文件夹

.net6.0 采用原有的设置方法不起作用

复制代码
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
	<runtime>
		<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
			<!--<publisherPolicy apply="yes" />这句不要也是可以的-->
			<probing privatePath="dll;dll/2" />
		</assemblyBinding>
	</runtime>
</configuration>

http://www.dedeyun.com/it/csharp/104076.html

需要在App.xaml.cs文件中加入以下代码
https://9to5answer.com/using-an-absolute-path-in-probing-privatepath

复制代码
public App()
        {
            AssemblyResolver.Hook("DLL");
        }

public static class AssemblyResolver
{
    internal static void Hook(params string[] folders)
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        {
            // Check if the requested assembly is part of the loaded assemblies
            var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName == args.Name);
            if (loadedAssembly != null)
                return loadedAssembly;

            // This resolver is called when a loaded control tries to load a generated XmlSerializer - We need to discard it.
            // http://connect.microsoft.com/VisualStudio/feedback/details/88566/bindingfailure-an-assembly-failed-to-load-while-using-xmlserialization

            var n = new AssemblyName(args.Name);

            if (n.Name.EndsWith(".xmlserializers", StringComparison.OrdinalIgnoreCase))
                return null;

            // http://stackoverflow.com/questions/4368201/appdomain-currentdomain-assemblyresolve-asking-for-a-appname-resources-assembl

            if (n.Name.EndsWith(".resources", StringComparison.OrdinalIgnoreCase))
                return null;

            string assy = null;
            // Get execution folder to use as base folder
            var rootFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)??"";

            // Find the corresponding assembly file
            foreach (var dir in folders)
            {
                assy = new[] { "*.dll", "*.exe" }.SelectMany(g => Directory.EnumerateFiles(Path.Combine(rootFolder,dir), g)).FirstOrDefault(f =>
                {
                    try
                    {
                        return n.Name.Equals(AssemblyName.GetAssemblyName(f).Name,
                            StringComparison.OrdinalIgnoreCase);
                    }
                    catch (BadImageFormatException)
                    {
                        return false; /* Bypass assembly is not a .net exe */
                    }
                    catch (Exception ex)
                    {
                        // Logging etc here
                        throw;
                    }
                });

                if (assy != null)
                    return Assembly.LoadFrom(assy);
            }

            // More logging for failure here
            return null;
        };
    }
}
相关推荐
SEO-狼术2 小时前
Infragistics now supports .NET 10
pdf·.net
喵叔哟3 小时前
7.【.NET10 实战--孢子记账--产品智能化】--API 文档迁移 — Swashbuckle → OpenAPI + Scalar
.net
无风听海4 小时前
.NET10之 HttpClient 使用指南
.net
leonkay5 小时前
关于.NET中的队列理解
数据库·性能优化·.net·个人开发·设计规范·队列
CSharp精选营5 小时前
C# 如何减少代码运行时间:7 个实战技巧
性能优化·c#·.net·技术干货·实战技巧
~plus~21 小时前
.NET 8 C# 委托与事件实战教程
网络·c#·.net·.net 8·委托与事件·c#进阶
rockey6271 天前
AScript动态脚本多语言环境支持
sql·c#·.net·script·eval·function·动态脚本
dotNET实验室1 天前
ASP.NET Core 内存缓存实战:一篇搞懂该怎么配、怎么避坑
.net
龙侠九重天1 天前
ML.NET 实战:快速构建分类模型
分类·数据挖掘·c#·.net
无风听海1 天前
.NET10之内置日志配置与使用指南
asp.net·.net