System.IO.Compression in ASP.NET VNext full CLR - c#

I'm trying to use System.IO.Compression.ZipArchive in a ASP.NET VNext class library in VS2015 Preview. I added the System.IO.Compression package using NuGet, and it added it to my project.json as a aspnetcore50 dependency.
When I try to use the ZipArchive, the intellisense says is not available in ASP.NET 5.0 but it is available in ASP.NET Core 5.0. If I switch to use ASP.NET Core using the drop down in the top bar, then my code works as expected, but when I choose normal ASP.NET it doesn't work.
I tried manually adding it as a dependency to aspnet50 in the project.json, but that didn't fix it.
I need to use the full CLR over the Core CLR as I need to load assemblies into the AppDomain at run time, and I believe this isn't supported in the Core CLR.
Please could someone explain what's going on here, maybe point me to some articles or blog posts, show me how to fix this.
Update:
I guess a better way or wording this is - the ZipArchive is not available in aspnet50, but it is available in aspnetcore50 when I add the System.IO.Compression NuGet package. Why is this?

They only way that I get the project to compile and work was doing the following in the project.json. I'm not too familiar with the compression library so I did not spend time trying to compress a file. Below you will a sample code that will compile with no issue.
{
"version": "1.0.0-*",
"dependencies": {
},
"frameworks": {
"aspnet50": {
"dependencies": {
},
"frameworkAssemblies": {
"System.IO.Compression": "4.0.0.0"
}
},
"aspnetcore50": {
"dependencies": {
"System.Runtime": "4.0.20-beta-22231",
"System.IO.Compression.ZipFile": "4.0.0-beta-22231",
"System.IO": "4.0.10-beta-22231",
"System.IO.FileSystem": "4.0.0-beta-22231"
}
}
}
}
Sample Code
public static void ZipFile(string path)
{
var data = new MemoryStream(File.ReadAllBytes(path));
var zip = new ZipArchive(data, ZipArchiveMode.Create,false);
zip.CreateEntry(path + ".zip");
}

Related

How to create a chocolatey package for a .NET Core cli application?

I have a very basic cli application(that basically prints "hello world") written in C# and that uses the .net core runtime.
I tried to create a chocolatey package by:
running choco new hcli
modifing the generated .nuspec file manually to supply info(version, author...)
running choco pack
This produced a .nupkg file, when I run choco install hcli.0.0.1.nupkg I get ERROR: This package does not support 64 bit architecture.
I am suspecting that chocolatey does not support project.json based projects, the documentation does not mention anything about .net core.
What am I doing wrong?
project.json file:
{
"version": "0.1.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true,
"outputName": "hcli"
},
"dependencies": {},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
},
"runtimes": {
"win7-x64": {}
}
}
Chocolatey does not support visual studio projects nor project.json at the time of this post.
Fix the error
What you are seeing is a pretty common error if you have not set up or adjusted any of the packaging.
Have you reviewed the contents of tools\chocolateyInstall.ps1 after you generated the packaging? I would review and adjust those automation scripts that were generated (and review the readme).
If you don't need the automation scripts, simply remove them and have your binaries in the package.
As you have indicated, there is a much more drawn out article at https://chocolatey.org/docs/create-packages
Alternative Option - Use NuGet to pack
You can always look to use NuGet to generate the package and then consume it with Chocolatey. As long as it is compatible with NuGet v2 (currently), you should be good to go. The other aspect of that is that if you have dependencies at the DLL level, please include them in the packaging - dependencies are really at the application level. Like a dependency on dotnetcore package.

netcoreapp1.0 does not find net461 specific package

I am using VS Code to build a net core app. I need to use a NuGet package that is not supported, and therefore changed my project.json file's framework part to the following:
"frameworks": {
"net461": {
"dependencies": {
"ScrapySharp": "2.6.2"
}
},
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
}
}
}
Restoring the project seems to work and the package (ScrapySharp) is installed. However when I use the package, it appears that both netcoreapp and net461 look for it. While net461 finds and references it properly, netcoreapp throws the following error:
The type or namespace name 'ScrapySharp' could not be found
Is there anything I can do to work around this?
If package available only for one framework of two - you should modify your program code and do not use this package when compiled under netcoreapp. Effectively, you will lost some functionality of your app when compiled under netcoreapp.
If this suitable for you, then use preprocessor directives like this:
public void function DoSomething()
{
#if NET461 then
// do something with ScrapySharp
#else
// Say to your user that this feature is not available
throw new Exception("This feature is not available on this platform");
#endif
}

Xamarin Forms .NETStandard 1.4 resources issue

I've had converted my PCL project from classic Xamarin Forms to .NETStandard 1.4
and I have a issue with resources(*.resx)
Error CS0103 The name 'Resource' does not exist in the current
context Prog1Utilities D:\MyProjects\Pro1\Pro1Utilities\WebUtilities\ApiService.cs 38 Active
How can I fix it?
Project.json
{
"supports": {},
"dependencies": {
"Newtonsoft.Json": "9.0.1",
"System.Net.Http": "4.3.0",
"Xam.Plugin.Connectivity": "2.2.12",
"Xamarin.Forms": "2.3.3.168"
},
"frameworks": {
"netstandard1.4": {
"imports": "portable-win+net45+wp8+win81+wpa8"
}
}
}
To make .resx work, you need to add Microsoft.NETCore.Portable.Compatibility NuGet package. However, there are other problems with building after that. This is a well-known problem already and folks in Microsoft work on it. I'll update this post once I have any updates from them.
Update (Dec '16)
As of now, Xamarin is not fully compatible with .NET Standard according to Microsoft support. I'll be happy if someone prove me wrong.
Update (Mar '17)
The problem seems to be gone in Visual Studio 2017.
Using dnxcore50 in imports provide best possible api's support as per my experience so far. Try it.
"frameworks": {
"netstandard1.5": {
"imports": "dnxcore50"
}
}

.NET versions in ASP.Core

I have a asp core web app with
"frameworks": {
"net452": {}
},
And I have also Class Library (.NET Core).
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.5": {
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"net452": {}
}
}
}
The question is, how can I add web app project reference to class library? Should I import some .net framework or use .net standard ?
Now I have a error message when I was trying to add reference to class library project.
The following projects are not supported as references:
"webprojectname" has target frameworks that are incompatible with
targets in current project "classlibraryprojectname"
"classlibraryprojectname": .NETCoreApp, Version=v1.0 "webprojectname":
.NETFramework, Version=v4.5.2
EDIT: Updated in accordance with changes to the question.
So I don't keep filling the comments section, your project.json's should look like this:
Web App (you will also have other dependencies not shown here):
"dependencies": {
"YourClassLibrary": "1.0.0-*"
},
"frameworks": {
"net452": { }
}
Class Library:
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.2": { }
}
}
Note: if your class library depends on something higher than netstandard1.2, then you will either have to increment the .Net Framework in your web app to match the NETSTandard you're using (as per previously linked docs), or you will need to cross-compile the class library like so:
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.5": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
},
"net452": { }
}
}
Using the first suggestion, your application produces one assembly for your class library, using this second method your class library will be compiled twice (one for netstandard1.5, one for net452). When your web app tries to resolve the class library, it realises it can't use netstandard1.5 (which targets net462), so falls back to the net452 assembly. Note however that using this second option is usually indicative of something not being right with your project structure, and should only be used if you know what you're doing and why (in my opinion).
The NETStandard.Library dependency shown is essential a NuGet bundle that pulls down the necessary CoreFX packages. If you get errors like "Cannot find Microsoft.CSharp" it's usually because you haven't pulled down the default packages. You can instead pull down only the CoreFX packages you want, such as System.Collections but this is a more advanced technique.
Hopefully that helps clear things up?

How do I use System.Data in a .NET Core RC2 console app (Linux, Debian 8)?

I've installed .NET Core RC2 on a Debian 8 amd64 system and would like to test if it's possible to query an instance of Microsoft SQL Server.
So I'd like to add to my project a dependency on the System.Data.SqlClient assembly.
Presently my project file created by running the dotnet new CLI tool looks like this:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Using this answer to a similar query, I was able to add a reference to System.Data.Common changing the
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
fragment to
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50",
"dependencies": {
"System.Data.Common": "*"
}
}
}
which made dotnet restore use NuGet to download a bunch of stuff.
I then tried to change that fragment to read
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50",
"dependencies": {
"System.Data.SqlClient": "*"
}
}
}
but NuGet says it's
Unable to resolve 'System.Data.SqlClient' for '.NETCoreApp,Version=v1.0'.
If I change the version string to read "4.1.0-rc3-*" the error message just gets more specific:
Unable to resolve 'System.Data.SqlClient (>= 4.1.0-rc3)' for '.NETCoreApp,Version=v1.0'.
What I'm puzzled about is that the NuGet package gallery dedicated to .NET Core explicitly lists System.Data.SqlClient as available.
So what could I do to add a reference to System.Data.SqlClient assembly to my project and have NuGet download it?
On a side note, I'm currently playing around in a plain console with only the dotnet CLI tool. Is there any way to manage project dependencies for a .NET Core project without resorting to installing IDEs?
Like poke already annotated in the comment is correct. Specify a version to System.Data.SqlClient makes your restore happy ;)
Why is that? System.Data.SqlClient exists in the http://nuget.org gallery. Not specifying a version ("") is not allowed outside of the boundaries of a project (like a nuget feed package) and specifying solely an star "*" (you should never do that, it allows breaking changes) restore the highest available version. Since there is no stable, star will not find anything (there is some magic with the dashes behind). The RC2 version of that library is the mentioned 4.1.0-rc2-24027 and when you ask with 4.1.0-rc2-* it will take the highest of the RC2 builds (but there is only one). In comparison System.Data.Common has a public release on nuget.org for the Universal Windows Platform and is found for that reason.
The RC3 is the next release and only available on developer feeds from the .NET Core and ASP.NET Core team and not the public nuget feed. You should not play with them.
if you are in project.json file, the intellisense guide you now if you have updated the Visual studio with the latest tooling avaiable..
I added the following in the dependencies element and it works perfectly..
"System.Data.SqlClient": "4.1.0-rc2-24027",
On my MINT 19 Tara I do not use System.Data.SqlClient but the newer version Microsoft.Data.SqlClient.
Check nuget
After typing the .NET-cli command, the package was downloaded and the reference was added to the project csproj file.
In the code, use the newer namespace:
using Microsoft.Data.SqlClient;
The "regular" code works fine:
public static void CreateCommand(string queryString, string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader() ;
while( reader.Read() ) {
for (int i = 0; i < reader.FieldCount; i++)
{
Console.WriteLine( "Column name={0}, Value={1}",
reader.GetName(i),
reader.GetValue(i) );
}
}
}
}

Categories

Resources