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

Add new library manual draft and samples #110

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
484 changes: 484 additions & 0 deletions docs/tutorials-samples/targeting-core-for-new-libraries.md

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions samples/core-projects/libraries/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# .NET Core Library Samples

These samples can be built and run using the .NET Core rc1 toolchain.

They are small and intended to demonstrate how to target and build NuGet packages for different targets.

To build/use any of these (using `new-library` as an example):

1. Open your favorite Command Line Interface.

2. Navigate to the top-level directory:

`$ cd new-library`

3. Restore packages by typing the following:

`$ dnu restore -s "https://myget.org/F/xunit"`

`$ dnu restore`

Note: The first `dnu restore` is needed right not to allow for unit tests to run. This will not be required in the future.

4. To build and package the library as a NuGet package, type the following:

`$ cd src/Library`

`$ dnu build`

`$ dnu pack`

Check out the `/bin/Debug` directory to see the generated artifacts and `.nupkg`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a note on how to build release?


5. To run unit tests (only applicable to `new-library`):

`$ cd ../../test/LibraryTests`

`$ dnx test`

And that's it!

## new-library

The project under `/new-library` is a project targeting **only** .NET Core. Backwards compatibility is not a goal.

It also showcases two other things: how to use multiple projects and how to test.

There are two libraries showcased. The first, `DependencyLibrary`, contains functionality that the second, `Library` uses. The `LibraryTests` test project takes a dependency on `Library` to test that project.

## net45-library

**IMPORTANT:** This project requires Windows and .NET Framework installed on your machine.

The project under `/net45-library` is for targeting any of the following:

* .NET Framework 4.5 and above
* Windows Phone 8.1
* Whindows Phone Silverlight 8
* Universal Windows Platform
* DNX Core
* Xamarin
* Mono

It uses the new `dotnetXX` Target Framework Moniker introduced with the .NET Platform Standard.

## net40-library

**IMPORTANT:** This project requires Windows and .NET Framework installed on your machine.

The project under `/net40-library` is for compatibility with .NET Framework 4.0 and above. It also demonstrates how to use `#if` compile guards to multi-target for a .NET 4.0 target.

## pcl-library

**IMPORTANT:** This project requires Windows and .NET Framework installed on your machine.

The project under `/pcl-library` is for compatibility with a supported PCL Profile (e.g. 3344). It shows how to structure the `project.json` file to allow for targeting a PCL. It also demonstrates how to use `#if` compile guards and how to set those up in conjunction with the `project.json` file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Text.RegularExpressions;

#if NET40
using System.Net;
#else
using System.Net.Http;
using System.Threading.Tasks;
#endif

namespace Library
{
public class Net40CompatLibrary
{
#if NET40
private readonly WebClient _client = new WebClient();
private readonly object _locker = new object();
#else
private readonly HttpClient _client = new HttpClient();
#endif

#if NET40
public string GetDotNetCount()
{
string url = "http://www.dotnetfoundation.org/";

var uri = new Uri(url);

string result = "";

lock(_locker)
{
result = _client.DownloadString(uri);
}

int dotNetCount = Regex.Matches(result, ".NET").Count;

return $"Dotnet Foundation mentions .NET {dotNetCount} times!";
}
#else
public async Task<string> GetDotNetCountAsync()
{
string url = "http://www.dotnetfoundation.org/";

var result = await _client.GetStringAsync(url);

int dotNetCount = Regex.Matches(result, ".NET").Count;

return $"dotnetfoundation.org mentions .NET {dotNetCount} times in its HTML!";
}
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"frameworks": {
"dotnet55":{
"dependencies": {
"System.Runtime":"4.0.0-rc1-*",
"System.Net.Http":"4.0.0-rc1-*",
"System.Threading.Tasks":"4.0.0-rc1-*",
"System.Text.RegularExpressions": "4.0.0-rc1-*"
}
},
"net40":{
"frameworkAssemblies": {
"System.Net":"",
"System.Text.RegularExpressions":""
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Library
{
public static class Net45CompatLibrary
{
public static string Hello => "Hello .NET Core and .NET Framework 4.5!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"frameworks": {
"dotnet51":{
"dependencies": {
"System.Runtime":"4.0.0-rc1-*"
}
},
"net45":{}
}
}
5 changes: 5 additions & 0 deletions samples/core-projects/libraries/new-library/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"projects": [
"src", "test"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace DependencyLibrary
{
public static class NewDependencyLibrary
{
public static string ImportantMessage => "Live long and prosper";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"frameworks": {
"dotnet55":{
"dependencies": {
"System.Runtime":"4.0.0-rc1-*"
}
}
}
}
13 changes: 13 additions & 0 deletions samples/core-projects/libraries/new-library/src/Library/Library.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using DependencyLibrary;

namespace Library
{
public static class NewLibrary
{
public static string Hello => "Hello, .NET Core!";

public static string GenerateImportantMessage(string name) =>
$"{NewDependencyLibrary.ImportantMessage}, {name}!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"dependencies": {
"DependencyLibrary":""
},
"frameworks": {
"dotnet55":{
"dependencies": {
"System.Runtime":"4.0.0-rc1-*"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Xunit;
using Library;
using System;

public class LibraryTest
{
[Fact]
public void HelloTest()
{
string expected = "Hello, .NET Core!";

string actual = NewLibrary.Hello;

Assert.Equal(expected, actual);
}

[Fact]
public void ImportantMessageTest()
{
string expected = "Live long and prosper, Captain Kirk!";

string actual = NewLibrary.GenerateImportantMessage("Captain Kirk");

Assert.Equal(expected, actual);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"commands": {
"test":"xunit.runner.dnx"
},
"frameworks": {
"dnxcore50":{
"dependencies": {
"Library":"",
"System.Runtime":"4.0.0-rc1-*",
"xunit":"2.1.0",
"xunit.runner.dnx": "2.1.0-rc1-*"
}
}
}
}
22 changes: 22 additions & 0 deletions samples/core-projects/libraries/pcl-library/src/Library/Library.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

#if !PORTABLE259
// .NET Core libraries can go here.
#else
// Use a library here that is compatible with PCL Profile 329.
#endif

namespace Library
{
public static class PCL329CompatLibrary
{
public static void Foo()
{
#if !PORTABLE259
// Using a .NET Core library unavailable to PCL Profile 329
#else
// Need to use a a PCL Profile 329-compatible library here.
#endif
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"frameworks": {
"dotnet55":{
"dependencies":{
"System.Runtime":"4.0.0-rc1-*"
}
},
".NETPortable,Version=v4.0,Profile=Profile344":{
"compilationOptions": {
"define": [
"PORTABLE259"
]
},
"frameworkAssemblies":{
"mscorlib":"",
"System":"",
"System.Core":""
}
}
}
}