首页 > 解决方案 > 如何在 .NET Http Trigger Azure 函数中包含 MCR 和引用 MATLAB 程序集?

问题描述

我目前正在使用 Azure Functions、.NET 和 Docker 创建一个使用以下教程中创建的 MagicSquareComp.dll 程序集的 HttpTrigger Azure 函数的概念验证项目:

https://www.mathworks.com/help/compiler_sdk/gs/create-a-dotnet-application-with-matlab-code.html

我能够按照下面的教程创建一个通用的 .NET Azure 函数:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image?tabs=nodejs

我尝试结合这两个教程中使用的技术来创建一个可以打印出幻方的 Azure 函数。在安装 MATLAB 运行时并正确配置之后,我能够让 HttpTrigger Azure 函数在我的本地 Windows 环境和 Ubuntu 18.04 VM 中工作。但是,当我尝试使用 docker 文件并使用 docker build 和 docker run 命令从 docker 容器运行 Azure 函数时,遇到以下错误:

Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function:
MyHttpTrigger ---> System.TypeInitializationException: The type initializer for 'MagicSquareComp.Class1' 
threw an exception. ---> System.TypeInitializationException: The type initializer for
'MathWorks.MATLAB.NET.Utility.MWMCR' threw an exception. ---> System.TypeInitializationException: The 
type initializer for 'MathWorks.MATLAB.NET.Arrays.MWArray' threw an exception. ---> 
System.DllNotFoundException: Unable to load shared library 'mclmcrrt9_7.dll' or one of its dependencies. 
In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: 
libmclmcrrt9_7.dll: cannot open shared object file: No such file or directory 

我正在使用的 docker 容器是一个 Linux 容器,我已经在其上安装了 MATLAB 运行时,与我在 Ubuntu 18.04 VM 上安装它的方式完全相同,并以完全相同的方式设置环境变量。我确保 MWArray.dll 和 MagicSquareComp.dll 在 /home/site/wwwroot/bin/ 目录中。我尝试将 MATLAB 运行时文件夹复制到 /home/site/wwwroot/bin/runtimes/。这些似乎都没有任何影响。当我尝试从 Docker 文件运行时,我仍然收到相同的错误。

下面是我在 docker 文件中使用的代码:

# FROM microsoft/dotnet:2.2-sdk AS installer-env 



# COPY . /src/dotnet-function-app 

# RUN cd /src/dotnet-function-app && \ 

#     mkdir -p /home/site/wwwroot && \ 

#     dotnet publish *.csproj --output /home/site/wwwroot 





FROM ubuntu:18.04 AS installer-env 

RUN apt-get -qq update && \ 

    apt-get -qq install -y nodejs && \ 

    apt-get -qq install -y npm && \ 

    npm install -g azure-functions-core-tools && \ 

    apt-get -qq install -y curl && \ 

    apt-get -qq install -y wget && \ 

    apt-get -qq install -y unzip && \ 

    curl -sL https://aka.ms/InstallAzureCLIDeb | bash && \ 

    wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb && \ 

    dpkg -i packages-microsoft-prod.deb && \ 

    apt-get -qq update && \ 

    apt-get -qq install -y apt-transport-https && \ 

    apt-get -qq update && \ 

    apt-get -qq install -y dotnet-sdk-2.1 && \ 

    mkdir /mcr-install && \ 

    mkdir /opt/matlab_runtime && \ 

    cd /mcr-install && \ 

    wget https://ssd.mathworks.com/supportfiles/downloads/R2019b/Release/3/deployment_files/installer/complete/glnxa64/MATLAB_Runtime_R2019b_Update_3_glnxa64.zip && \ 

    unzip -q MATLAB_Runtime_R2019b_Update_3_glnxa64.zip && \ 

    ./install -destinationFolder /opt/matlab_runtime -agreeToLicense yes -mode silent && \ 

    cd && \ 

    rm -rf /mcr-install 



COPY . /src/dotnet-function-app 

RUN cd /src/dotnet-function-app && \ 

    mkdir -p /home/site/wwwroot && \ 

    dotnet publish *.csproj --output /home/site/wwwroot && \ 

    chmod 777 /home/site/wwwroot/bin/MWArray.dll && \ 

    chmod 777 /home/site/wwwroot/bin/MagicSquareComp.dll && \ 

    cp -rf /opt/matlab_runtime /home/site/wwwroot/bin/runtimes 



# To enable ssh & remote debugging on app service change the base image to the one below 

# FROM mcr.microsoft.com/azure-functions/dotnet:2.0-appservice  

FROM mcr.microsoft.com/azure-functions/dotnet:2.0 

ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ 

    AzureFunctionsJobHost__Logging__Console__IsEnabled=true 



ENV LD_LIBRARY_PATH=/opt/matlab_runtime/v97/runtime/glnxa64:/opt/matlab_runtime/v97/bin/glnxa64:/opt/matlab_runtime/v97/sys/os/glnxa64:/opt/matlab_runtime/v97/extern/bin/glnxa64 



ENV XAPPLRESDIR=/opt/matlab_runtime/v97/X11/app-defaults 



COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"] 

MagicSquareComp.dll 是使用在 Windows 上运行的 MATLAB 生成的。但这似乎不会导致问题,因为我能够让 Azure 函数运行并正确引用 Linux VM 中的 MagicSquareComp.dll 和 MWArray.dll。

对此的任何帮助将不胜感激!

标签: c#.netmatlabazuredocker

解决方案


推荐阅读