none
Azure Function using unmanaged/native dll RRS feed

  • Question

  • I have a very simple project built in Visual Studio 2017. It contains an Azure C# Function that uses an HttpTrigger to pass two values to be added together and return the result. Another project contains a mixed-mode C++ assembly. It creates an instance of an unmanaged C++ class and delegates the summation to it. This works fine in debug/release mode 32 bit on my development machine. When I publish to Azure it fails to load the mixed mode assembly. The assembly and native dll are located in a bin folder under the root of the function folder. This is the absolute simplest example I could think of to try and test out interop. Any input you could provide would be greatly appreciated.

    I am trying to get an understanding of how to do this properly. We have a large legacy base of libraries that I need to leverage if this works.

    Exception while executing function: UnAdder
    Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: UnAdder ---> System.IO.FileNotFoundException : Could not load file or assembly 'UnAdderWrapper.DLL' or one of its dependencies.

    UnAdder.cs

    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.Azure.WebJobs.Host;
    using UnAdderWrapper;
    
    namespace UnFunction
    {
      public static class UnAdder
      {
        [FunctionName("UnAdder")]
        public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "UnAdder/val1/{val1}/val2/{val2}")]HttpRequestMessage req, int val1, int val2, TraceWriter log)
        {
          log.Info("C# HTTP trigger function processed a request.");
    
          // 
          Adder aw = new Adder();
          int sum = aw.Sum(val1, val2);
          return req.CreateResponse(HttpStatusCode.OK, $"Hello Sum={sum}");
        }
      }
    }
    

    UnAdderWrapper.h, .cpp

    // UnAdderWrapper.h
    
    #pragma once
    
    using namespace System;
    
    #include "..\Adder\Adder.h"
    
    namespace UnAdderWrapper {
    
      public ref class Adder
      {
      public:
        Adder() : m_adder(new UnAdder) {}
        ~Adder() { delete m_adder; }
    
        int Sum(int Val1, int Val2);
    
      protected:
        !Adder() { delete m_adder; }
    
      private:
        UnAdder * m_adder;
      };
    }
    
    // cpp
    #include "stdafx.h"
    
    #include "UnAdderWrapper.h"
    
    int UnAdderWrapper::Adder::Sum(int Val1, int Val2)
    {
      return m_adder->DoSum(Val1, Val2);
    }
    

    Native Adder.h, .cpp

    // Adder.h
    #pragma once
    
    #define EXPORT_API __declspec(dllexport)
    
    class EXPORT_API UnAdder
    {
    public:
      int DoSum(int val1, int val2);
    };
    
    // cpp
    // Adder.cpp : Defines the exported functions for the DLL application.
    //
    
    #include "stdafx.h"
    #include "Adder.h"
    
    int UnAdder::DoSum(int val1, int val2)
    {
      return val1 + val2;
    }
    


    Could not load file or assembly 'UnAdderWrapper.DLL' or one of its dependencies. The specified module could not be found.
    Could not load file or assembly 'UnAdderWrapper.DLL' or one of its dependencies. The specified module could not be found.
    Thursday, March 29, 2018 5:03 PM

All replies

  • Loading native assemblies is currently not supported in Azure Functions. Kindly see, https://github.com/Azure/Azure-Functions/issues/622
    Saturday, March 31, 2018 12:20 AM
  • Thanks for your feedback. That issue and related threads, unless I missed it, nor anywhere else in all the docs of Azure Functions do I see a definitive statement that loading unmanaged dlls is not supported. I do see several questions about how to do it and MS techs trying to figure it out. And I see a feature request from 2017. I would suggest adding a clear statement in the sand box restrictions.

    Monday, April 2, 2018 12:56 PM
  • So, maybe look at it differently...what exactly does UnAdderWrapper.dll do?

    FYI, I vaguely remember a question last year at a talk on something similar and someone from somewhere on some Azure them said "so long as it's managed code, sure".  At this point, I would presume unmanaged code is not supported because they only say they support manage code.

    If you want to run unmanaged code, a container is what you'd likely need.

    Monday, April 2, 2018 2:55 PM