LinqBridge Breaks Razor Views: .NET Version-Based File Output Restriction - visual-studio

Our project uses several NuGet packages, a few of which reference LinqBridge, a library that re-implements LINQ to Objects for C# 2.0. The LinqBridge.dll file lives under /packages/PackageName/lib/20/LinqBridge.dll, so it clearly is supposed to only apply to .NET 2.0.
The problem is that, even though every project in the solution is configured to build to .NET 4.0, the LinqBridge.dll binary gets copied over to the final /bin directory and wreaks havoc in Razor views. If I perform .Select() on an IEnumerable, there is an ambiguous call between the built-in LINQ call and the re-implemented one that LinqBridge provides.
I clearly do not need the re-implemented version; if I simply delete LinqBridge.dll from the output /bin directory, everything works just fine. However, that is not an acceptable permanent solution.
Is there any way I can configure something to quit copying that file, which is for an old .NET version, into the /bin output?
Edit: I duct-taped together a solution by adding this to the "Post-build event command line:" commands under "Build Events" in my solution properties:
del $(SolutionDir)\bin\LinqBridge.dll
It's still far from ideal, but at least it lets my project run for now.

NuGet has support for different binaries for different .NET versions so I would suggest that the packages you are using are built badly.
I would contact the authors of the packages and see if they can fix them so that only the net11 or net20 versions include LinqBridge.
Supporting Multiple .NET Framework Versions and Profiles
Many libraries target a specific version of the .NET Framework. For example, you might have one version of your library that's specific to Silverlight, and another version of the same library that takes advantage of .NET Framework 4 features. You do not need to create separate packages for each of these versions. NuGet supports putting multiple versions of the same library in a single package keeping them in separate folders within the package.
(more...)

A useful approach we found was using the LinqBridge.Embedded Nuget package instead of the standard LinqBridge package. This embeds Linqbridge as a C# file within your project, and hence does not get copied over to the bin folder and loaded into the context of the Razor view.
This was useful to us because an assembly we reference still needs to be built in .Net 2.0, as it is also referenced by a 2.0 application. Hence that assembly uses LinqBridge.Embedded, and the LinqBridge assembly does not end up in our 4.0 servers' bin folders.

Related

Where to specify <RunCommand> tag in .Net Core .csproj file

Where to define this command in csproj file?
bin\Debug\net47\MyApp.exe
I m using .net core 2.0.0 SDK. I m not getting RunCommand in Intellisense. Also while building my solution, I have 7 projects and main project is of type console application and its Output Type is EXE. Solution gets built successfully. While I press F5 it gives me MessageBox containing message like "Unable to run your project. The RunCommand is not defined."
How to solve this?
This is typically set by the SDK. Most likely you are targeting netstandard2.0 (was it a Class Library project that you later added a Main() to?). Try targeting the specific runtime+version - in your case netcoreapp2.0 or net47 - instead.
If you want to build both Core and Full Framework you can specify multiple target frameworks by separating them with a semicolon and changing the TargetFramework tag name to plural (TargetFrameworks):
<TargetFrameworks>net47;netcoreapp2.0</TargetFrameworks>
Alternatively, you should be able to specify a <RunCommand> inside <PropertyGroup> in your .csproj file. FWIW I can't seem to get anything to show up in Intellisense except the generic <PropertyName> and anything I've already added to the list.
An example would be:
<RunCommand>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).exe</RunCommand>

Assembly Not Being Recognized

I built a VB Windows Forms application a while back using VS05 (or VS08? Not exactly sure) that I've recently converted to use VS10. I reference a .dll called ExcelPackage (another article, usage) so that I can create/manipulate Excel docs serverside. This app has worked fine on my old computer (PC/Vista) for a number of years. However, I have tried to move it to my new computer (PC/Win7 64-bit), and I can't get it to recognize the ExcelPackage .dll.
I have tried recompiling the .dll in VS10 and dropping the new .dll in my bin folder and re-referencing it. When I do this, before I try building, all my errors go away and I am actually able to navigate the class using VS10's built in ability (mouse over Imports OfficeOpenXml and you get a dropdown arrow that allows you to go through the classes). After I build, I get a green squiggly under my Imports OfficeOpenXml statement (can't find the reference).
I did some research and discovered that the .dll containing System.IO.Packaging has been moved around in .NET 3.0 and even re-referenced the new .dll, rebuilt, re-added, re-referenced, still no dice.
Am I missing something, or how do I get my application to recognize this assembly so that I can compile and continue working?
Thanks.
I don't see anything special about that project. Do note that the solution and project need to be converted. When that happens, you'll end up targeting the .NET 2.0 framework. That won't work out well, it has an assembly reference to WindowsBase, a 3.0 assembly. Make sure you update the target.

Whats the difference between <RequiredTargetFramework> and <targetframeworkversion>?

We upgraded our .net 3.5 projects (c#) to .net 4.0. When you look at the project file there are two tags that I'm trying to make sense out of:
<RequiredTargetFramework>3.5</RequiredTargetFramework>
<TargetFrameworkVersion>4.0</TargetFrameworkVersion>
Why are there two seemingly similar tags with different values?
The <RequiredTargetFramework> element was already present in your 3.5 project. It's associated with the assembly <Reference> and only present on assemblies that are not available in .NET 2.0
I don't buy much stock in the single mention of it in MSDN, I don't see how batch building has anything to do assembly references. Nor is it used in any of the 3.5 MSBuild .target files. I think the IDE simply uses it to put the warning icon next to the reference in the References node when you change the Target Framework to a version less than what's needed to support the assembly.
There are other elements like this in a project file that don't affect MSBuild but have an effect in the IDE. Like <SubType> and <DependentUpon> in the <Compile> element.
Have you found this one link? link text. TargetFrameworkversion is easy, that's the one you can change in the project properties to say which framework to build against. The article says that RequiredTargetFramework is used to batch items (but it's still not clear on it' real purpose other than it's not used a lot)
batches the Reference items by their RequiredTargetFramework metadata. The output of the target looks like this:
Reference: 3.5;3.5
Reference: 4.0
Target batching is seldom used in real builds. Task batching is more common. For more information, see MSBuild Batching.

Including required DLLs into a windows project solves a perceived problem?

We have recently decided to start shipping the versions of some of the DLL files that a product requires with the product itself.
This is to guard against the situation where (for example) the MVC DLL file is updated on the server to which the software is deployed and the product fails to work as it was written against the now previous version of the DLL.
If the MVC DLL file of the specific version is included in the product and "locally" referenced this prevents this problem from happening. (In an ideal world every product which will be installed onto the destination server would be updated to the most recent version but this is not always practical)
My concern and question is whether this is going to give a false sense of security or not actually cope with the problem in the following situation: if Version 1.0 of the MVC DLL file is relying on method X of standard Microsoft DLL library Y and this DLL file Y is updated we will be in the same situation of having a broken product?
Assuming you meant the MSVC (Microsoft Visual C++) DLLs, the correct solution is to use an application manifest. The MSVC DLLs support Side by Side installation (SxS). This means that a new version of those DLLs does not replace an old version. Your application manifest tells Windows which DLL version(s) you want.
I don't know about MVC specifically, but you have a bigger problem if the conflicting version of a dependency dll is installed to the GAC on your target machine, as it will be used in preference to the local file.
Depending on how big it would make your final product, consider statically linking your program. This will prevent the much dreaded DLL hell since you wont have to give DLLs out.

Avoid loading .Net Dlls in a C++/CLI project?

I have a project written in C++/CLI. Some of the types there are in managed code, and some are in completely native code. Let's say I have the produced DLL on a machine that dosen't have any version of the .Net framework installed, is there a way that another, native application will link with my "mixed-mode" Dll and use only the native types? I've noticed that the minute I add the "/clr" switch, my Dll automatically depends on several .Net Framework Dlls (mscorjit, mscoree etc.), and when I actually try to use the 100% native types defined in it, the application still tries to load those .Net Framework Dlls (even though I don't use the framework in that part of the code).
So, is it possible to avoid loading those Dlls in such case? (as I see it, the other option is to create another, native project, that will contain all of the native types, without the managed ones).
Thanks
No. When you load a mixed mode assembly (/clr), right after DllMain runs, the .cctor runs and initializes the framework, if it hasn't already been setup for the application.
Without this, there would be a big hit as soon as you called a function that required a managed API. For details, see "Initialization of Mixed Assemblies" on MSDN.
The best option would be to make your native API a separate DLL, and have the mixed mode assembly a separate project, so you can load it separately if required.

Resources