user9393635
user9393635

Reputation: 1429

target framework considerations with nuget packages?

I'm fairly new to the process of creating NuGet packages. I recently created a NuGet package via NuGetPackageExplorer. The "Package Metadata" view has a "Framework Assembly References" section with a value of "Microsoft.CSharp (Any,Verion=0.0)." This is what the nuspec file looks like:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>My.Cool.NuGet.Package</id>
    <version>1.0.2</version>
    <title></title>
    <authors>John Smith</authors>
    <owners>John Smith</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>My cool NuGet package</description>
    <frameworkAssemblies>
      <frameworkAssembly assemblyName="Microsoft.CSharp" />
    </frameworkAssemblies>
  </metadata>
</package>

Can you please explain relationships, dependencies and constraints for the following dimensions:

The frameworkAssembly value in the nuspec above is displayed as follows in the NuGetPackageExplorer:

Assembly name: Microsoft.CSharp

Supported frameworks: Any,Version=v0.0

Does this mean that the NuGet package is intended to be .NET framework version agnostic? Are there any scenarios where I would want to explicitly state a specific version(s) in this section? The reason I'm asking is that I installed a NuGet package in an MVC project yesterday and I was experiencing some weird behavior. Specifically:

However, when I attempted to run the app, the compiler displayed an error of "type of namespace not found" for these using directives. But then the compiler errors disappeared but the app wouldn't run b/c of the previous compiler errors. After I fiddled with the version settings with the MVC project and NuGet package source project, I was able to get around this error. So now I'm trying to get an understanding of the finer details of what I need to consider and configure in regards to target frameworks.

Upvotes: 4

Views: 1989

Answers (1)

Aleš Doganoc
Aleš Doganoc

Reputation: 12052

The TargetFamework of the source project for the NuGet package should be the lowest framework version you need by projects that will install it. For example if you know you will have projects in the 4.5 and 4.6 framework you make the source for the package 4.5. You can also put multiple builds for multiple frameworks inside a package. Here is the documentation about that.

The TargetFramework for the projects installing the package should be greater or equal that the version of the package. Since higher versions of the framework are compatible with lower versions. It should not allow you to install a package that is not supporting the framework you are using in the project.

The frameworkAssemblies sections just defines which framework assemblies a package is using so the references are automatically added. Check the documentation for more detailed info.

Upvotes: 3

Related Questions