将.NETCore 3.1依赖dll放入additionalProbingPaths

修改 .NET Core 3.1 项目 依赖dll加载路径

1. 修改runtimeconfig.json

添加runtimeconfig.template.json
依赖dll放入的文件夹名称

{
  "additionalProbingPaths": [
    "lib"
  ]
}

2.根据deps.json移动dll的位置

使用python脚本

moveDeps.py

点击查看代码
import json
import os

target_dir = ""
target_dlls = []


def loadRuntimeConfigJson():
    with open("runtimeconfig.template.json") as f:
        temp = json.load(f)
        global target_dir
    if type(temp["additionalProbingPaths"]) == list:
        target_dir = temp["additionalProbingPaths"][0]
    if type(temp["additionalProbingPaths"]) == str:
        target_dir = temp["additionalProbingPaths"]


def loadDepJson():
    dep_name = None
    for file in os.listdir():
        if file.endswith(".deps.json"):
            dep_name = file
    dep={}
    if dep_name is not None:
        with open(dep_name) as f:
            dep = json.load(f)

    targets = dict(dict(dep["targets"]).popitem()[1])
    libraries = dict(dict(dep["libraries"]))
    global target_dlls
    for k, v in targets.items():
        if not v.get("compileOnly") and libraries[k].get("type") == "package":
            try:
                t = targets[k]["runtime"].popitem()
                target_path = os.path.join(
                    os.path.join(target_dir, libraries[k]["path"]),
                    t[0])
                target_dlls.append((os.path.split(t[0])[-1], target_path))
            except:
                pass

def MoveDlls():
    global target_dlls
    for i, j in target_dlls:
        dir = os.path.dirname(j)
        if not os.path.exists(dir):
            os.makedirs(dir)
        os.rename(i, j)


if __name__ == '__main__':
    loadRuntimeConfigJson()
    loadDepJson()
    MoveDlls()


发布完成后将py文件放入发布后的文件夹使用python3 moveDeps.py就会根据dep.json文件将配置移动到lib文件中


相关资料
https://stackoverflow.com/questions/48650348/additionalprobingpaths-not-respected-after-dotnet-publish

posted @ 2021-12-24 22:43  Ymangz  阅读(800)  评论(0编辑  收藏  举报