小编swa*_*eph的帖子

在 azure 中作为应用服务发布后,在使用 DinkToPDF 进行 PDF 转换时无法加载外部库“libwkhtmltox.dll”

我正在使用 DinkToPDF 库将 html 字符串转换为等效的 PDF。要使用这个库,我们必须导入提供的本地库 libwkhtmltox.dll。当我在本地运行我的 .net 核心项目时,这工作正常,但是当我尝试在 Azure 中将我的 Web 项目发布为应用服务时,我收到以下错误,

未处理的异常:System.DllNotFoundException:无法加载共享库“/home/site/wwwroot/libwkhtmltox.dll”或其依赖项之一。为了帮助诊断加载问题,请考虑设置 LD_DEBUG 环境变量: /home/site/wwwroot/libwkhtmltox.dll: cannot open shared object file: No such file or directory

我在 startup.cs 文件中提到了库的用法,如下所示。

    internal class CustomAssemblyLoadContext : AssemblyLoadContext
    {
        public IntPtr LoadUnmanagedLibrary(string absolutePath)
        {
            return LoadUnmanagedDll(absolutePath);
        }
        protected override IntPtr LoadUnmanagedDll(String unmanagedDllName)
        {
            return LoadUnmanagedDllFromPath(unmanagedDllName);
        }

        protected override Assembly Load(AssemblyName assemblyName)
        {
            throw new NotImplementedException();
        }
    }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
            var context = new CustomAssemblyLoadContext();
            context.LoadUnmanagedLibrary(Path.Combine(Directory.GetCurrentDirectory(), "libwkhtmltox.dll")); …
Run Code Online (Sandbox Code Playgroud)

azure .net-core dinktopdf

6
推荐指数
1
解决办法
2500
查看次数

如何注册具有多个接口的服务并将该服务用作ConfigureServices .Net Core 3.1中的单个实例

我有实现多个接口的服务;例如

ServiceClass : IService1, IService2
{
// implementation
}
Run Code Online (Sandbox Code Playgroud)

现在我需要在 ConfigureServices() 中注册此服务,这样我就有一个 SerivceClass 实例,并且即使通过构造函数注入 IService1 或 IService2 也可以使用该实例。对于前,

public class SomeClass1: ISomeClass1
    {
      SomeClass1(IService1 service)
          {
           ...
          }
    }

public class SomeClass2 : SomeClass2
    {
      SomeClass2(IService2 service)
          {
           ...
          }
    }
Run Code Online (Sandbox Code Playgroud)

我尝试使用它的两个接口在ConfigureServices()中注册ServiceClass,但它会创建多个实例。

ConfigureServices(){

...
services.AddSingleton<IService1, ServiceClass>();
services.AddSingleton<IService2, ServiceClass>();
services.AddSingleton<ISomeClass1>(
                x => new SomeClass1(
                    x.GetRequiredService<IService1>(),
                    ));

services.AddSingleton<ISomeClass2>(
                x => new SomeClass2(
                    x.GetRequiredService<IService2>(),
                    )); 
....
}
Run Code Online (Sandbox Code Playgroud)

我需要找到一个解决方案,即使注入的接口不同,我也可以将相同的实例注入 SomeClass1 和 SomeClass2 。

c# asp.net-core

4
推荐指数
1
解决办法
2247
查看次数

标签 统计

.net-core ×1

asp.net-core ×1

azure ×1

c# ×1

dinktopdf ×1