Deploy ASP.NET with full .NET - c#

I'm wonderinf if it possible to deploy my asp.net core application if I also use some library from .NET 4.5.2.
To describe my problem, in my app I use SyndicationFeed which comes from full .NET
and in my project.json in "framework" section I have:
"frameworks": {
"net452": {
"frameworkAssemblies": {
"System.ServiceModel": ""
},
"dependencies": {
}
},
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": [
"dotnet5.6",
"portable-net45+win8"
]
}
}
In the other sections I use ASP.CORE packages like:
"Microsoft.EntityFrameworkCore.Design": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
"Hangfire.AspNetCore": "1.6.8",
and more.
So the question is is it possible to deploy to the IIS. Should I deploy to server with run with ASP.CORE or full .NET.

In short: yes, it's possible. But full .NET Framework required (on server).
Long story:
Having two frameworks in projects.json effectively creates two different apps (one for net462, other for netcoreapp1.1) during compilation/publishing. This is two different applications, compiled for different frameworks from same source code.
To run first (for net462) you need machine with .NET Framework installed. For other (for netcoreapp) you need .NET Core installed. You can't "swap" (run net462-build app on .NET Core and vice versa).
But looking at your project.json I can't believe your app compiles successfully. You need System.ServiceModel for your app to work. But it's available only for net462. This means that during compilation first app (for net462) compiles successfully, while second (net netcoreapp) should fail (class not found, namespace not found, etc).
Run dotnet build or dotnet publish from command line in project/solution folder. See any errors?
So, you can't create/build/run under .NET Core while you need packages/classes not available for .NET Core.
Possible solutions:
Replace package for netcore-compatible (if any exist, I don't know), or re-build (port) existing to be compatible (as Joel says);
Completely remove this package (implement required code yourself);
Do not target netcoreapp1.1;
Add conditional compilation (#if) where you use this package, so you will use if only in net462-version of your app. Otherwise (#else) add NotImplementedException, null result or something other (it depends) - effectively you will have two different apps after compilation: full-functional for net462 and restricted-functional for netcoreapp.

Related

Why does my .Net Core console application have the wrong build target? [duplicate]

Every time I build a project using the new .NET Core RC2 templates I am not provided with a runnable .EXE file. If I hit F5 for debugging my console application it runs fine through the
C:\Program Files\dotnet\dotnet.exe
application. And if I use the
dotnet run
command in the folder, it runs fine as well. But I see no way to run the application without the .NET Core CLI tools.
The contents of my
bin\Debug\netcoreapp1.0\
folder looks like this:
As you can see there is no .EXE file available. Just the dll.
Am I overlooking something? Or is there something wrong with my project.json file?
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Thanks!
There are actually 2 app models in .NET Core:
Portable apps: heavily inspired by "DNX console apps", these apps don't produce .exe files and are instead executed by the .NET Core shared runtime (whose version is defined by the Microsoft.NETCore.App package, thanks to its special type: platform attribute). The corresponding .NET Core runtime must be installed on the machine to be able to use portable apps. If the exact version cannot be found, an exception is thrown when running dotnet run.
Standalone apps: standalone apps are really similar to good old .NET console apps as they produce .exe files. The .NET Core runtime doesn't have to be installed on the machine, because it is directly embedded with the application itself.
You're currently using the first model. To use the standalone model, you need to tweak your project.json:
Add a runtimes section to list the environments your app will target (e.g win7-x64 or ubuntu.14.04-x64). You can find the complete list here.
Remove the Microsoft.NETCore.App dependency. You can replace it by this package instead: "NETStandard.Library": "1.5.0-rc2-24027".
Here's an example of a standalone app:
{
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.CommandLine": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
"Microsoft.Extensions.DependencyInjection": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging": "1.0.0-rc2-final",
"Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {
"net451": { },
"netcoreapp1.0": {
"dependencies": {
"System.Net.Ping": "4.0.0-rc2-24027"
},
"imports": [
"dnxcore50",
"dotnet5.6",
"portable-net451+win8"
]
}
},
"runtimes": {
"win7-x64": { }
}
}
The answer is in the documentation with complete steps now.
You can create two types of deployments for .NET Core applications:
Framework-dependent deployment
Self-contained deployment
For a runnable .EXE file, the Publish self-contained should be used.
To create a runnable application from a .NET Core console application you can use the dotnet tool. Just run in your project directory:
dotnet publish --runtime win7-x64
This creates a standalone app (self-contained deployment; includes all necessary libraries consuming at least 60MB on your disk). Of course you can also choose other runtimes, like osx.10.11-x64 or ubuntu.16.04-x64.
If you used the default configuration (New Project -> Console App (.NET Core)), there is no modification of any configuration file necessary.
step 1: remove "type": "platform", from Project.json under frameworks section
step 2: add run time section to your project.json. Note each section is separeted by a comma. Add your runtime. below is just an example for win 10.
"runtimes": {
"win10-x64": {}
}
Step 3: dotnet restore command on your project. ( open cmd, go to your project folder wherever src folder is there, run dotnet restor)
step 4: dotnet pack
step 4: dotnet build -r win10-x64 - or just build.
Step 5: you can notice .exe created under debug/netcore/win10/
In ASP.NET Core try changing your app type to default, in project.json:
"Microsoft.NETCore.App": {
"type": "default",
"version": "1.0.0-*"
}

Why does .Net Core CLI in VSCode put release folder in the debug directory

I feel like I'm missing something obvious, but searching google, this site and the .Net Core SLI issues section on GitHub did not immediately return an answer, nor did reading the documentation for the .Net Core project.json format.
In plain old C# projects (regular .Net, not Core) scaffolded by Visual Studio (not VSCode), usually running a build will put files in
%project root%/bin/Debug
out of the box, or
%project root%/bin/Release
if you choose publish.
In VSCode with .Net Core, by default build puts files in
%project root%/bin/Debug/netcoreapp1.0.
however if you run
dotnet publish
on the command line, it will put the files in a release folder inside
%project root%/bin/Debug/netcoreapp1.0.
resulting in a structure like
%project root%/bin/Debug/netcoreapp1.0/release.
If you have specified to build for a specific platform target in your project.json then it will similarly put the files in
%project root%/bin/Debug/netcoreapp1.0/PlatformName.
For example
%project root%/bin/Debug/netcoreapp1.0/win7-x64.
My question is, why does .Net Core put the release folder inside the debug folder and since I prefer the old directory structure, is there a way I can tell .Net Core to do it that way instead, say via some project.json property or cli flag similar to how say typescript allows you to specify an outDir?
Testing this with the default hello world project provided by 'dotnet new', my modified project.json looks like this:
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
//"type": "platform",
"version": "1.0.0"
}
},
"imports": "dnxcore50"
}
},
"runtimes": {
"win7-x64": { }
}
}
According to the documentation (bold is mine):
dotnet publish [--framework] [--runtime] [--build-base-path] [--output] [--version-suffix] [--configuration] []
...
-c, --configuration [Debug|Release]
Configuration to use when publishing. The default value is Debug.
So you need to use:
dotnet publish -c Release
(there's also the --output parameter to specify the destination folder: the documentation also states the default, which matches what you are seeing)

Dependency issues migrating project from ASP.NET 5 RC1 to ASP.NET Core 1.0

I am having a hard time converting my asp.net (core) app from dnx46 to .netcoreapp1.0 because of two particular dependencies ( Microsoft.Azure.ServiceBus and System.IO.Ports.SerialPort )
Being positive, I'm making the bet that these feature will eventually land on .net core one day.. but in the meantime, I found that converting my app from moniker dnx46 to .netstandard1.3 allows me to resolve the ServiceBus dependency.
Resolving System.IO.Ports.SerialPort however is still an issue and I don't understand how to make this work. I was hoping that importing net462 framework in .netstandard1.3 moniker, would allow to find the System.IO.Ports.SerialPort object but it does not.
What am I missing ?
For reference, there's my project.json :
{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0-rc2-3002702",
"type": "platform"
},
"Microsoft.NETCore.Platforms": "1.0.1-*",
"Microsoft.EntityFrameworkCore": "1.0.0-rc2-final",
"Microsoft.EntityFrameworkCore.Sqlite": "1.0.0-rc2-final",
[...more stuff...]
},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
// To be restored when they'll become available on .net core
// "Microsoft.WindowsAzure.ConfigurationManager": "3.2.1",
// "WindowsAzure.ServiceBus": "3.2.1",
}
},
"netstandard1.3": {
"buildOptions": {
"define": [ "INCLUDE_WINDOWSAZURE_FEATURE" ]
},
// Imports of net462 fixes loading of
// - NewtonSoft.Json
// - System.Runtime.Loader for "Microsoft.NETCore.App"
"imports": [
"net462"
],
"dependencies": {
"Microsoft.NETCore.Portable.Compatibility": "1.0.1-rc2-24027"
"Microsoft.WindowsAzure.ConfigurationManager": "3.2.1",
"WindowsAzure.ServiceBus": "3.2.1",
}
}
}
}
Resolving System.IO.Ports.SerialPort however is still an issue and I don't understand how to make this work. I was hoping that importing net462 framework in .netstandard1.3 moniker, would allow to find the System.IO.Ports.SerialPort object but it does not.
You can't reference System.IO.Ports.SerialPort when targeting .NET Core or .NET Standard, because this contract only exists in the full .NET Desktop framework.
This library might be eventually ported but in the meantime, you'll have to use .NET Desktop (e.g net462) instead of .NET Core.
Remove netcoreapp1.0 and netstandard1.3 and add net462 and it should work.
If you plan on deploying to a windows box and targeting net452, then simply take on a dependency on net452. I put together a migration guide to share my upgrading experiences, perhaps it might help? I initially had this misunderstanding that I would take a dependency of netstandard1.* and then "import": "net4*", David Fowler laughed at me and said something to the extent of "dude that's do wrong!". :P
You should change your project.json frameworks to look like this:
"frameworks": {
"net462": { }
}

.NET Core Dependencies and Frameworks Understanding

I have been working on .NET Core from a few weeks now. At first it was a great overhaul from Microsoft to the old .NET way of doing things. But now its getting on my nerves. Below is my original global.json:
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-rc1-update1",
}
}
This is my project.json:
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNetCore.StaticFiles": "1.0.0-*",
"Microsoft.NETCore.Platforms": "1.0.1-*"
},
"commands": {
"web": "Microsoft.AspNet.Server.Kestrel"
},
"frameworks": {
"dnxcore50": { }
},
"exclude": [
"node_modules"
],
"publishExclude": [
"**.user",
"**.vspscc"
]
}
And here is my hosting.json:
{
"webroot": "wwwroot"
}
I have created an Empty ASP.NET Core 1.0 Web Application Project, all I am trying to do is create an AngularJs application. But I cannot access the wwwroot directory since I am not able to:
app.UseDefaultFiles();
app.UseStaticFiles();
I removed the dnx451 from my original project.json and also changed the global.json to:
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-rc1-update1",
"runtime": "coreclr",
"architecture": "x64"
}
}
Error:
Error NU1002 The dependency Microsoft.AspNetCore.StaticFiles 1.0.0-rc2-20248 in project QMS.UI does not support framework DNXCore,Version=v5.0. QMS.UI ..\QMS.UI\src\QMS.UI\project.json
I have read a lot of blogs and stackoverflow answers. Can anybody tell me in very high level way how does we:
Effectively find a valid package, that will suite our target framework.
Effectively make all frameworks (if listed in the project.json) happy with that dependency notation? (Sometimes I get an error, its available for dnx451 but not dnxcore50) (Should I use the #if dnxcore50... etc notations?)
Please help! I am going through a similar trouble in another project based on .NET Core.
The static file dependency is form RC2 and is not compatible with RC1 you use in rest of your project.
Either you use all packages from RC1 or all from RC2, but be aware that RC2 dependencies often break (one package requires version xyz, where other package is not yet updated and requires old library but due to API changes can't work with the newer version).
This are RC1 packages
"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
This are RC2 packages (you can recognize them on their name, Microsoft.AspNet.* packages got renamed to Microsoft.AspNetCore.* some time during RC2 cycle
"Microsoft.AspNetCore.StaticFiles": "1.0.0-*",
"Microsoft.NETCore.Platforms": "1.0.1-*"
Effectively find a valid package, that will suite our target framework.
First step is to target only the same versions. If you need a stable environment use RC1 until RC2 is released. However, be aware that there are quite a few breaking changes. Check out the ASP.NET Core GitHub annoucements (mostly breaking changes) for more information.
Effectively make all frameworks (if listed in the project.json) happy with that dependency notation? (Sometimes I get an error, its available for dnx451 but not dnxcore50) (Should I use the #if dnxcore50... etc notations?)
Depends on the assemblies you need. Some have compatible api with both target platforms, but most don't. Usually that's how you would switch out platform dependent assemblies/code.
Be aware that there is a difference if you use a Class Library (Package) or .NET Core application (Webproject, unit test project).
For RC1, the monikers are dnx451 (or dnx452, dnx46) for full .NET Framework targeting and dnxcore50 for .NET Core. For Class Libraries however, you have to use net451 (or net452/net46) for full .NET Framework target and dotnet5.x (x=1-4, i RC2 also 5) for .NET Core.

Launching ASP.NET Core 1 app from console application

While dotnet cli is not yet ready to work with Visual Studio and dnu/dnx gives me huge amount of extra unneeded libraries I want to do the following.
I want to launch ASP.NET Core 1 project either using command line or console app using traditional .csproj approach.
I created a ASP.NET 4 project, included all dependencies and was able to compile it.
1st Attempt.
At first I tried to compile it into console app and launch through standard entry point:
// Entry point for the application.
public static void Main(string[] args) {
WebApplication.Run<Startup>(args);
}
It says: "No service for type 'Microsoft.Extensions.PlatformAbstractions.IApplicationEnvironment' has been registered."
Which means that dnx.exe creates additional dependency injection rules probably through PlatformServices.Create.
Does somebody know how to do it properly?
2nd Attempt
I compiled it into dll and tried to launch it through dnx.exe which gave me the error "Error: Unable to load application or execute command 'Microsoft.AspNet.Hosting'. Available commands: web.".
Here's my project.json:
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"webroot": "wwwroot",
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.Kestrel --server.urls http://*:5004"
},
"frameworks": {
"dnx451": {}
},
"entryPoint": "ManagementConsoleWeb",
"loadable": false
}
So what's the proper way? Is is possible at all?
Here're some thoughts.
ASP.NET Core 1 requires DNX bootstrap to work. Yes, it works under .NET 4.5.2 but DNX is a must. So basically it's strange you can reference ASP.NET Core 1 libraries in .csproj net451.
DNX bootstrap libraries are not available under Nuget for net451 moniker. So you can't reference them from .csproj
That means you can't DEBUG ASP.NET Core 1 using traditional .csproj class library or executable approach. Of course you can always connect to a running app but that's not convenient.
So our team now is working with DNX bootstrap for NET CORE 1 but we deliver outputs from .csproj build until RC2 release.
ASP.NET Core 1.0 uses IHostingEnvironment hostingEnvironment instead of IApplicationEnvironment applicationEnvironment.
Go through the ASP.NET Core Migration document for full detail.

Categories