Azure / azure-sdk-for-net

This repository is for active development of the Azure SDK for .NET. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/dotnet/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-net.
MIT License
5.36k stars 4.79k forks source link

issu in Running Native DLL #6664

Closed aghilimc closed 5 years ago

aghilimc commented 5 years ago

i have some big pdf's and i want to compress these files,

i'm using this api and it works fine in console app : https://www.pdf-tools.com/pdf20/en/products/pdf-optimization/free-trial-software/download/list/pdf-optimizer/

but i have problem in run this in azure function.and it can not load dll .

my project config are:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <PlatformTarget>AnyCPU</PlatformTarget>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
    <PackageReference Include="SSH.NET" Version="2016.1.0" />
  </ItemGroup>

i set manually to call the dll by :

         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern IntPtr LoadLibrary(string libname);

    public static void PdfOptimizeAssemblyLoadEventHandler(object sender, AssemblyLoadEventArgs args)
            {
                if (args.LoadedAssembly.FullName.StartsWith("PdfOptimizeNET"))
                {
                    IntPtr Handle = IntPtr.Zero;
                    if (IntPtr.Size == 4)
                    {
                        // 32-bit
                        Handle = LoadLibrary(@"x86\PdfOptimizeAPI.dll");
                    }
                    else if (IntPtr.Size == 8)
                    {
                        // 64-bit
                        Handle = LoadLibrary(@"x64\PdfOptimizeAPI.dll");
                    }
                    if (Handle == IntPtr.Zero)
                    {
                        int errorCode = Marshal.GetLastWin32Error();
                        // log some error
                    }
                }
            }

and in side function :

   [FunctionName("Function1")]
        public static void Run([TimerTrigger("59 * * * * * ")]TimerInfo myTimer, ILogger log)
        { 
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.AssemblyLoad += new AssemblyLoadEventHandler(PdfOptimizeAssemblyLoadEventHandler);

            try
            {
                pdfOptimization.Optimization("", "", log);
            }
            catch (Exception ex)
            { 
                throw;
            }
        }
    }

with AnyCPU i get this exception: The type initializer for 'Pdftools.PdfOptimize.Optimizer' threw an exception.

if i set debug to x64, the application doesn't run.

praries880 commented 5 years ago

Thank you for opening this issue! We are routing it to the appropriate team for follow up. cc: @shahabhijeet

erich-wang commented 5 years ago

@aghilimc , Azure function doesn't support native dll currently, please refer to https://github.com/Azure/Azure-Functions/issues/622. Close the issue since https://github.com/Azure/Azure-Functions is better place to track the issue.