title: How to import native DLL on Service Fabric?
body: <p>I do want to import a native DLL into a microsoft service fabric service. Is this possible? If so, how can I achieve that? I guess I am stuck at how I tell the deployment to also deploy the native DLL to be able import it later at runtime. Currently I get a runtime error, that the dll was not found. As the whole application is deployed to a service fabric cluster I guess I need to also somehow get the needed native DLLs there as well.</p>
  
  <p>Any help is much apprecciated! Thank you in advance!</p>
  
  <p>Best regards</p>
  
  <p>Mario</p>
  
  <p><strong>Edit:</strong>
  Sorry, I may had to little information about my approach and the error. I try to import the native DLL in my C# service like this:</p>
  
  <pre><code>[DllImport("Plugin.dll", 
      EntryPoint = "DoSomeStuff",
      CallingConvention = CallingConvention.Cdecl)]
  public static extern int DoSomeStuff();
  </code></pre>
  
  <p>Then calling the imported function in the inhereted function RunAsync of StatelessService:</p>
  
  <pre><code>protected override async Task RunAsync(CancellationToken cancellationToken)
  {
      DoSomeStuff();
  }
  </code></pre>
  
  <p>Naively my first approach was to simply put the needed Plugin.dll file into the bin folder where the other executables are sitting. But they either do not get moved to the cluster or cannot be found by the runtime.</p>
  
  <p>At runtime I do get the following exception:</p>
  
  <pre><code>System.DllNotFoundException: Die DLL \"Plugin.dll\": Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E) kann nicht geladen werden.
  bei DoSomeStuff()
  bei Application.&lt;RunAsync&gt;d__2.MoveNext() in C:\\SolutionPath\\ProjectPath\\Application.cs:Zeile 45.
  --- Ende der Stapelüberwachung vom vorhergehenden Ort, an dem die Ausnahme ausgelöst wurde ---
  bei System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  bei System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  bei Microsoft.ServiceFabric.Services.Runtime.StatefulServiceReplicaAdapter.&lt;ExecuteRunAsync&gt;d__e.MoveNext()"
  </code></pre>
  
  <p>I am sorry for the german error message (original), I try to translate:</p>
  
  <pre><code>System.DllNotFoundException: The DLL \"Plugin.dll\": The specified Module was not found. (Exception of HRESULT: 0x8007007E) cannot be loaded.
  at DoSomeStuff()
  at Application.&lt;RunAsync&gt;d__2.MoveNext() in C:\\SolutionPath\\ProjectPath\\Application.cs:Zeile 45.
  --- End of stack trace of the exception origin, where the exception was raised ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at Microsoft.ServiceFabric.Services.Runtime.StatefulServiceReplicaAdapter.&lt;ExecuteRunAsync&gt;d__e.MoveNext()"
  </code></pre>