Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically add references to core framework assemblies when targeting .NET Framework #379

Merged
merged 3 commits into from
Nov 16, 2016

Conversation

dsplaisted
Copy link
Member

Right now, if you create an ASP.NET Core application targeting .NET Framework, you won't get references to any framework assemblies except mscorlib. This means a bunch of types (Uri, Linq, etc) won't be available to you.

We could add these to the templates, but in the interest of keeping the .csproj files concise I think it's better to reference them automatically in the SDK targets. That's what this PR does.

The list of assemblies comes from what you get by default when creating a new .NET Framework Class Library. For parity with .NET Standard and .NET Core projects, I'd suggest we should reference all assemblies that contain types that are in .NET Standard 2.0. @weshaggard @ericstj @terrajobst can you provide a list of assemblies you would need to reference in .NET Framework to get all the types that are in .NET Standard 2.0?

<_TargetFrameworkMinorVersion Condition="$(_TargetFrameworkVersionSegmentCount) > 1">$(TargetFrameworkVersion.Split('.')[1])</_TargetFrameworkMinorVersion>
</PropertyGroup>

<ItemGroup Condition=" '$(DisableImplicitFrameworkReferences)' != 'true' and '$(TargetFrameworkIdentifier)' == '.NETFramework'">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is bigger than the original set of default references in project.json/DNX. How were these chosen? Based on the .NET Framework default template today? Can we make this a bit smaller?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the .NET Framework default template today?

Yes, that's what I based it on. However, I'm suggesting that we make it even bigger by default, to include all the types that are in .NET Standard 2.0. That way you'll have access to all .NET Standard 2.0 types by default whether you are targeting .NET Core or .NET Framework.

Copy link
Contributor

@nguerrera nguerrera left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migration currently adds the default refs from project.json. If we land on a superset of that, then we should adjust the migration. File a bug on CLI for that.

<_TargetFrameworkMajorVersion>$(TargetFrameworkVersion.Split('.')[0])</_TargetFrameworkMajorVersion>
<_TargetFrameworkMajorVersion Condition="$(_TargetFrameworkMajorVersion.StartsWith('v'))">$(TargetFrameworkVersion.Substring(1))</_TargetFrameworkMajorVersion>
<_TargetFrameworkMinorVersion Condition="$(_TargetFrameworkVersionSegmentCount) > 1">$(TargetFrameworkVersion.Split('.')[1])</_TargetFrameworkMinorVersion>
</PropertyGroup>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this string manipulation is necessary. IIRC, @rainersigwald mentioned elsewhere that MSBuild can will automagically compare System.Version-parseable strings and compare them as such.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true, though System.Version.TryParse will fail on a v-prefixed string so you'll probably need to do that still.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rainersigwald What's the syntax to compare version numbers in MSBuild? Or do strings that parse as version numbers automatically get compared as such?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter: strings that parse as version numbers get compared that way with the usual comparison operators.

<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Net.Http" Condition="($(_TargetFrameworkMajorVersion) > 4) Or ($(_TargetFrameworkMajorVersion) == 4 And $(_TargetFrameworkMinorVersion) >= 5)"/>
<Reference Include="System.Xml"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: sort these alphabetically.

<Reference Include="System.Data.DataSetExtensions"/>
<Reference Include="Microsoft.CSharp"/>
<Reference Include="System.Data"/>
<Reference Include="System.Net.Http" Condition="($(_TargetFrameworkMajorVersion) > 4) Or ($(_TargetFrameworkMajorVersion) == 4 And $(_TargetFrameworkMinorVersion) >= 5)"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other conditions needed for version < 4, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests won't pass with version < 4 but that's only because of dotnet/msbuild#1333 combined with tests always running with core msbuild as of now. We keep getting requests for it, so we shouldn't discount version < 4 here.

Also update the test to make sure there aren't any warnings about references that can't be found
@dsplaisted
Copy link
Member Author

I've updated the list of references to be based on what is necessary to make the types in .NET Standard available, based on this list: https://github.com/dotnet/standard/tree/master/platforms/net461

From that list, I've excluded System.ComponentModel.Composition, which is only there to support a type forward for Lazy<T, TMetadata>, and System.Web, which is only there for HttpUtility, but has a whole bunch of other stuff we don't want to bring in.

I've also added conditions to the references so that warnings won't be generated when you target .NET Framework 3.5 or 4.0, which don't have all of these assemblies. I've also sorted them.

@dsplaisted
Copy link
Member Author

@nguerrera This is ready for you to re-review now

<Reference Include="System.Core"/>
<Reference Include="System.Data"/>
<Reference Include="System.Drawing"/>
<Reference Include="System.IO.Compression.FileSystem" Condition=" '$(_TargetFrameworkVersionWithoutV)' >= '4.5' "/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth a comment that these comparisons actually do the right thing in msbuild

<Reference Include="System.Numerics" Condition=" '$(_TargetFrameworkVersionWithoutV)' >= '4.0' "/>
<Reference Include="System.Runtime.Serialization"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Xml"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

project.json had Microsoft.CSharp, should we do that if we're in a C# project?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a strong opinion, but I left it out because it's not currently included in .NET Standard.

@nguerrera
Copy link
Contributor

Also, is there a bug on the CLI to remove now redundant references from migration. What will be the behavior for migrated projects from the current preview when they get the next one and start to have dup references?

@dsplaisted
Copy link
Member Author

Also, is there a bug on the CLI to remove now redundant references from migration. What will be the behavior for migrated projects from the current preview when they get the next one and start to have dup references?

I filed dotnet/cli#4695 to remove the redundant references. Duplicate references don't appear to be a problem, I think MSBuild ends up merging them.

@dsplaisted dsplaisted merged commit 0908e35 into dotnet:master Nov 16, 2016
@dsplaisted dsplaisted deleted the AutomaticFrameworkReferences branch November 16, 2016 01:29
sbomer pushed a commit to sbomer/sdk that referenced this pull request Sep 19, 2017
GangWang01 pushed a commit to GangWang01/sdk that referenced this pull request Jun 27, 2022
* Tags become choices, lots of help changes

* Rebased on rel/sv2017/post-rtw

* Post-Rebase cleanup of outputs for various situations. Moved tags to symbols on 2.0 templates

* Reverted to tags in 1.x templates and Item templates

* Removed extra space from tags secifications in 1.x templates

* Reverted to tags in 2.0 templates

* Fixed misspelling of 'language' in 2.0 template.json files

* Changes based on review comments (not including case-insensitive reads yet)

* Rebased on rel/vs2017/post-rtw

* Made reading template config & cache info be case-insensitive.

* Made cache parameter properties get only

* Cleanup of help & ItemplateInfo

* Minor cleanup, mostly removing no longer needed async tasks

* More cleanup

* Tags become choices, lots of help changes

* Rebased on rel/sv2017/post-rtw

* Post-Rebase cleanup of outputs for various situations. Moved tags to symbols on 2.0 templates

* Reverted to tags in 1.x templates and Item templates

* Removed extra space from tags secifications in 1.x templates

* Reverted to tags in 2.0 templates

* Fixed misspelling of 'language' in 2.0 template.json files

* Cleaning up UI interactions. Better experience for: problems with extra args files (dotnet#327, dotnet#329); default not in choice list dotnet#271

* Added support for split template configuration files (dotnet#150)

* Cleanup of nulls in the template cache

* fixed a merge mistake

* More fixes from merge issues

* Make fallback host files work correctly

* Added wildcard to host matched. Fixed a bug with wildcard matching in zips

* Add filter switch, hide all switch, add logic for accepting explicitly set filters

* Change filter to type

* Give instructions to call --help instead of showing help on error

* Remove unused using

* Fixes dotnet#380 and dotnet#379

* Respond to review feedback

* Make the message shown for the invalid params match between help and non-help cases

* Set IsPackable=false for new test projects (dotnet/templating#376).

The rationale for this is that creating NuGet packages from tests is probably not the more-common use case and so by default these project should not participate when "dotnet pack" is run at the solution level.

This doesn't prevent users from opting into "dotnet pack", it simply won't be the default.

* Make IsPackable configurable for test project template (dotnet/templating#377).

* Use '-p' as the short form for '--enable-pack' in MSTest v1 project template.

* Implement "--enable-pack" parameter for test project templates (dotnet/templating#377).

This parameter, if set to "true", marks the project as packable (i.e. participates in solution-level calls to "dotnet pack", producing a NuGet package).

* Build for non-Windows platforms

Refactor to use latest cli

Add RestoreSource for dotnet-new3 project

Build with framework version and runtime id

Fix test cases

* Move testbase location to local directory

* Updating the identity of the 2.0 templates

* Better matching across templates with same group identity.

* Added precedence to all template.json files

* Reading & writing template Precedence

* Template matching uses Precedence to help disambiguate templates

* fix for dotnet#395

* Updated package refs in test projects

* fix for exclude, include, & copy only configuration in sources

* Mikes test harness for testing dotnet new commands. Tests for precedence disambiguation

* Fixes per Mike's CR

* renamed the mvc precendence tests

* Add 3PN for nuget.exe

* Allow runtime to be passed in

* Handling reading different cache formats, and upgrading the cache aut… (dotnet#407)

* Handling reading different cache formats, and upgrading the cache automatically as needed

* Cleanup for cache versioning

* Improvements to cache versioning. Language display fix.

* Cache rebuild enhancement. Only scan mount points for templates and langpacks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants