How to reference the native Onnxruntime.dll in my app installed from Nuget in my app .Instead of from System32

Vishal2 Bansal 145 Reputation points
2024-11-29T12:30:32.1733333+00:00

Requirements:-

  1. Hi i have created a .net framework class library. It uses inference session from ML.Onnxruntime nuget package to load a model and run inferencing on it.
  2. Now i am using that library in my WPF UI app. I don't want to install ML.onnxruntime in my wpf UI app as it is already installed in library. In UI app i will only pass some model test data (some sentences) to the the class library . That library should handle it and run inferencing.

Error facing:-

  1. Not able to achieve 2nd point. As my UI app is forcing me to download onnxruntime Ml package from nuget.
  2. If i install it then i am not able to run this UI app on other laptop(having windows 10). Their i guess it is referring to the onnxruntime.dll present in System32. Which is wrong. So help to me achieve my requirements.
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,965 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,795 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,093 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ki-lianK-7341 500 Reputation points
    2024-11-29T12:52:05.58+00:00
    1. Copy DLLs: Ensure onnxruntime.dll is copied to your WPF app’s output directory.
    2. Set DLL Path: Add this code at the start of your app to set the DLL search path:
    
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool SetDllDirectory(string lpPathName);
    
    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "path_to_your_dlls");
    SetDllDirectory(path);
    																																														
    
    
    
    1. Deploy Properly: Ensure all necessary DLLs are included in your deployment package.

    This should help your app reference the correct onnxruntime.dll.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.