I am trying to reference a native dll from an asp.net 5 project using pinvoke. The recommended solution is to wrap the dll in a nuget package and reference that. However I am also developing the native dll, so it is impractical to constantly create new nuget packages when I make changes.
If I have the native dll location in the path variable it is found through pinvoke and works fine. But I would like a solution which is contained in the Visual Studio solution so that other developers could download the VS solution and have it work without changing the path variable.
I tried using the bin syntax as described in this topic:
How do you add references to locally created NON ASP.Net 5 dlls to an ASP.Net 5 project
but that seems to only work for managed assemblies as I get an error stating that the assembly manifest cannot be found.
Thanks
Related
There are so many questions posted about this issue. I believe i have read them all and still not having much luck. I have a VS2015 project where i can do all that i have described below without issue. Also note that the process i went through to move the applications and class libraries from 2015 to 2017 was to fully rebuild them in 2017. In any case, i created a new solution in 2017 with new application and class library to prove my issue.
I created a new library call ClassLibrary and a new asp.net web app called WebApplication.
In ClassLibrary i then added a nuget for testing (i have also tested this with a file reference to a 3rd party DLL). In this case i added the AjaxControlToolkit 18.1.0 for testing.
Then in WebApplication project i reference ClassLibrary
I would expect this simple solution and project referencing to copy the dependent DLLS of ClassLibrary to WebApplication project bin. It does not. Prior to moving to VS 2017 this would (and still does in VS 2015) copy the dependency DLL of ClassLibrary in WebApplication's BIN. In the VS2017 project i described above the AjaxControlToolkit is not copied to the BIN, i only see a ClassLibrary.dll but none of its dependencies.
I have tried to "use" the AjaxControlToolkit in the WebApplication project code as some of the posts suggest. This does not work as the assembly is not available in the project and i get build errors. I've modified the .csproj files in so many different ways based on suggestions i have found on Stack, nothing works.
Whats even more strange is that i see the DLL in the bin but not listed in the project References. Even though the DLL is in the bin i am unable to work with it.
What is going on? This is such a simple exercise of referencing and after spending two days on Google searching on this it seems crazy that so many have this issue. Why are these DLLs not flowing all the way to the parent application BIN???
Thanks
In my post i mentioned that these projects originally in VS2015 were then recreated in VS2017. This process converted the original Web Site projects to Web Application projects.
As mentioned here, in a Web Site project when a ClassLibrary is referenced as a project reference "Copies of dependent assemblies, XML document files, license files, resource files, and so on." (this includes installed Nugets in the ClassLibrary) are copied to the project BIN. This is not the case with Web Applications. See here for the differences. Installing the NuGet wherever it is needed is required, it will not be inherited from child references as it is with Web Site projects.
I am in the process of creating a library to be used in Windows desktop applications. The core part of the library will be a C/C++ DLL that can be used in Win32 applications or other C/C++-based apps. I'll just call it libtest.dll for easy referencing here. I also want to make it available to Windows Forms applications (C#), so I am creating a C# class library (DLL) as a wrapper around the C/C++ library using P/invoke (DllImport) to call into the C/C++ code. I'll refer to this library as Test.dll.
Additionally, I have a Windows Forms project set up in Visual Studio for testing that depends on the C# class library project. The C# class library depends on the C/C++ library project. However, for Test.dll to find libtest.dll (in the DllImport statements), libtest.dll needs to either be in the same directory as Test.dll or in one of the DLL search paths in the system.
To make this easy when debugging in Visual Studio, I thought adding the libtest.dll project as a reference to the Test.dll project would automatically copy libtest.dll into the output directory of the Test.dll project. When I add the reference and then build the solution, the reference ends up broken and the libtest.dll file is not copied into the output directory of Test.dll.
When I add the reference to the Test.dll project in Visual Studio, the properties of the reference look fine:
"Copy Local" is set to True
The path is correct
"Resolved" is True
After building, however, the reference itself has a warning icon on it, and the path is now empty and "Resolved" is False. Any idea why, or how to fix this? How do I set up the project reference between these two library projects so the C/C++ DLL gets copied into the output directory of the C# class library project? I have verified that the build order of the two projects is correct: first libtest then Test.
I am currently working with 2 C# projects inside one VS 2017 solution. One project contains all the business logic and the other simply the UI. The UI, of course, references the Core project.
The Core solution uses RestSharp package on it's code, so I installed it using NuGet. When I run the solution and I reach the part of the code where RestSharp is used, the following exception is thrown: "System.IO.FileNotFoundException: Could not load file or assembly 'RestSharp'".
The problem gets solved if I also install 'RestSharp' package inside the UI project. But, I am trying to avoid that as the UI does not need to use that package.
Is there a way to avoid that? Regards
Check if Copy Local is set to true for the RestSharp reference. When it is set the RestSharp dll will be copied along with the Core dll.
Sorry if this is an obvious question, but I'm new to C# and am trying to use 2 dll's for a project. Where do I put them?
I tried to add SoundTouch.dll as a reference in an empty C# project, but Visual Studio said to make sure that the file is accessible and that it is a valid assembly or COM component. I was suggested to put the dll's into the \bin\debug path, but where is that? Thanks for your help.
Each project should have it's own \bin\debug (and \bin\release) directory structure under it's project directory, but they will not be created until you Build the project. As for the invalid assembly, it sounds like your dll has either become corrupted or is not a valid .NET assembly.
Edit: Looking at the SoundTouch website, someone has already created a wrapper for this dll suitable for use with C# and it can be found here
If your DLL is a .NET or COM DLL, you can simply add a reference in Visual Studio. For details on that procedure see
http://msdn.microsoft.com/en-us/library/wkze6zky(v=vs.110).aspx
If the DLL is a Win32 DLL (does not expose a COM interface) you can interface with it via the Platform Invoke (P/Invoke) mechanism.
http://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx
When using P/Invoke the DLL must reside in a location that can be resolved by the .NET runtime, and you use the DllImport attribute to "pull" entry points from the DLL into your C# code, e.g.
[DllImport("msvcrt.dll", EntryPoint="puts")]
.NET will search for the Win32 DLL in the same directory where the .exe is located. If you build under Visual Studio, a folder will be created under your project folder
bin\debug
for a debug build, or for a release build
bin\release
I've got a Solution with lots of projects and all but one of them is behaving. The one that is not working is a ConsoleApplication, and it relies on C# Class Library project. I've added a reference to the library project, and add the namespace (which I've checked is correct), but everywhere I reference the classes in my library, I get:
The type or namespace 'MyClass' could not be found (are you missing a using directive or an assembly reference?).
The library project is building successfully (I can see the DLLs appear in the bin folder) and I've tried a project reference, and also a reference to the DLL itself. Neither works.
Also, all projects are set to build with a platform target of 'Any CPU'.
I've tried pretty much every suggestion I've come across on forums with no success. Can anyone shed some light on what's going wrong?
Thanks
This solved the problem:
The console application had a Target framework of .NET Framework 4 Client Profile, whereas the library just had .NET Framework 4. I set the console app to .NET Framework 4 and it all builds perfectly.
My bet is on a framework mismatch between your library and you app...
Check if your library is not building with a superior version than you app, or if your app is building with a Client profile flavor
It is probably that one of your DLLs references some part of the .net framework that is not referenced in your console application. For example if one of your class library projects has asp.net server controls in and references System.Web, but your console application does not reference System.Web it will not build and you will get that error. But it is not obvious because the DLLs referenced are stored in the GAC so they would never appear in your bin folder.
I had to simply restart visual studio for reference to work but make sure you have reference added in .csproj file.
If you still experience the issue, make sure the class you're referencing is public and that Asp.net core Framework version match.
Sounds weird,
Have you tried to remove the reference of the project and add it again? Check if your console app has got all the right references.
You could also inspect the .csproj file and see if everything is correct in there.
Just Check that you "Class Library" project has classes in it or if it is a data access layer project which include only a .edmx Model check the Model designer is found and it generates fine.
Good Luck
I worked with syncing the framework, but still, it was giving issue.
So I tried another way.
Right-click on the dependency, and select Add project reference. I added the required project then the error was gone.