Running .NET Core on Opensuse 13.2 - c#

I have tested .NET Core on Windows & Ubuntu succesfully ( Console & ASP.NET 5 app).
I am now trying to run a simple console app, followed by a web app on OpenSUSE 13.2 x64. I have followed the tutorial found here , but with no success.
I have installed the DNX using the dnvm tool, and tried a simple hello world program:
program.json
{
"version": "1.0.0-*",
"dependencies": {
},
"frameworks" : {
"dnx451" : { },
"dnxcore50" : {
"dependencies": {
"System.Console": "4.0.0-beta-*"
}
}
}
}
program.cs
using System;
public class Program
{
public static void Main (string[] args)
{
Console.WriteLine("Hello, Linux");
Console.WriteLine("Love from CoreCLR.");
}
}
Running
dnvm list
Active Version Runtime Architecture OperatingSystem Alias
------ ------- ------- ------------ --------------- -----
1.0.0-rc1-final coreclr x64 linux
1.0.0-rc1-final mono linux/osx default
* 1.0.0-rc1-update1 coreclr x64 linux
1.0.0-rc2-16237 coreclr x64 linux
The problem is that neighter dnu restore / dnu build / dnx run does anything, it throws no error and does nothing.

According to the guys developing .NET core, this is due to an incorrect version of libicu. So, I have downloaded the source files from here and used this tutorial to build and install libicu52.1.
unzip icu4c-52_1-src.zip
cd icu
mkdir build
cd build
../source/runConfigureICU Linux/gcc CXXFLGS="-D__STRICT_ANSI__ "
make -j4
sudo make install
export LD_LIBRARY_PATH=/usr/local/lib64

Related

Why is Winforms template missing from my .NET 7?

I have a Linux laptop and there is already installed .NET. This is result of dotnet --list-sdks command line:
5.0.404 [/usr/share/dotnet/sdk]
6.0.101 [/usr/share/dotnet/sdk]
6.0.404 [/usr/share/dotnet/sdk]
7.0.101 [/usr/share/dotnet/sdk]
and I have global.json file in my CWD like this:
{
"sdk": {
"version": "7.0.101",
"rollForward": "latestFeature"
}
}
But when I am running dotnet new winforms command in my terminal I get this response:
No templates or subcommands found matching: 'winforms'.
How can I fix this problem?

Call python script from .Net Core using pythonnet

I'm trying to get pythonnet to work in my .Net Core app running on Linux.
I've made a reference to Python.Runtime.dll (which I got from nuget) in my .Net Core project.
My code is:
using System;
using Python.Runtime;
namespace pythonnet_w
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start");
using (**Py.GIL()**) {
// blabla
}
Console.WriteLine("End");
}
}
}
I get this runtime error:
Unhandled Exception: System.MissingMethodException: Method not found: 'System.Reflection.Emit.AssemblyBuilder
System.AppDomain.DefineDynamicAssembly(System.Reflection.AssemblyName, System.Reflection.Emit.AssemblyBuilderAccess)'.
at Python.Runtime.CodeGenerator..ctor()
at Python.Runtime.DelegateManager..ctor()
at Python.Runtime.PythonEngine.Initialize(IEnumerable`1 args, Boolean setSysArgv)
at Python.Runtime.PythonEngine.Initialize(Boolean setSysArgv)
at Python.Runtime.PythonEngine.Initialize()
at Python.Runtime.Py.GIL()
at pythonnet_w.Program.Main(String[] args) in D:\Development\~.Net libraries (3.part)\phytonnet\.Net Core test (phytonnet)\c#\pythonnet_test\Program.cs:line 10
/usr/sbin/pythonnet_w: line 5: 19487 Aborted dotnet "/usr/share/pythonnet_wit/pythonnet_w.dll"
Tried to find a solution in these threads but without any luck:
How do I run a py file in C#?
Call Python from .NET
UPDATE:
I tried to open \pythonnet-master\src\runtime**Python.Runtime.csproj** in Visual Studio to see if I can compile it to .Net or .Core, but I can only compile to .Net framework.
I found this article "How to port from .net framework to .net standard"
Is that what I have to do?
I finally had success by using a self-compiled Python.Runtime.dll as of version 2.4.0. There are two options to create a working DLL:
Remove the other target framework net461 from the respective project file (leaving only netstandard2.0).
Run dotnet build using the appropriate options
For option 2, the following works(in Windows, Mac and Linux):
Clone the pythonnet repo (https://github.com/pythonnet/pythonnet)
In the pythonnet folder, cd src\runtime
Run dotnet build -c ReleaseWinPY3 -f netstandard2.0 Python.Runtime.15.csproj in Windows(in Mac/Linux, replace ReleaseWinPY3 with ReleaseMonoPY3 because the former use python37 and the later use python3.7)
Set DYLD_LIBRARY_PATH in Mac or LD_LIBRARY_PATH in linux(Windows skip):
export DYLD_LIBRARY_PATH=/Library/Frameworks/Python.framework/Versions/3.7/lib
Use the built DLL bin\netstandard2.0\Python.Runtime.dll as DLL reference in your Visual Studio .NET Core project (mine targets netcoreapp2.2, netcoreapp3.1 is also tested ok), e.g. in conjunction with the following code,
using System;
using Python.Runtime;
namespace Python_CSharp
{
class Program
{
static void Main(string[] args)
{
using (Py.GIL())
{
dynamic os = Py.Import("os");
dynamic dir = os.listdir();
Console.WriteLine(dir);
foreach (var d in dir)
{
Console.WriteLine(d);
}
}
}
}
}
You can host the IronPython interpreter right in your .NET application. For example, using NuGet, you can download the right package and then embed the script execution (actually the IronPython engine) right into your application.
Ref:
https://medium.com/better-programming/running-python-script-from-c-and-working-with-the-results-843e68d230e5

System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Security, Version=4.0.0.0

I have created WebAPI project in ASP.NET Core with targeted framework dnxcore50. There are four projects in my solution.
WebAPI - Contains API methods
Core - Contains IRepository, IDataContext interfaces and base classes to communicate with Postgresql with dapper
DataAccess - Contains Repository classes for domain
Domain - Contains domain models
My application is using Postgresql database to store the data and at application side I used dapper and npgsql nuget package for database operation with Postgresql.
Everything was worked in windows environment and after that I tried in Ubuntu 14.04 and faced 'Timeout issue' while performing select query in npgsql but I resolved that by using the solution mentioned in github issue and after that everything worked perfectly in Ubuntu machine also.
So, I thought lets try to run WebAPI in docker and for that I created docker image also and successfully able to run also but unfortunately I am getting some new (compare to ubuntu 14.04) error when I call select API method which call domain repository to select data from Postgrespl by using dapper and npgsql.
fail: Microsoft.AspNet.Server.Kestrel[13]
An unhandled exception was thrown by the application.
System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Net.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.IO.FileNotFoundException: Could not load the specified file.
File name: 'System.Net.Security'
at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName)
at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.Open()
at Npgsql.NpgsqlConnectorPool.GetPooledConnector(NpgsqlConnection Connection)
at Npgsql.NpgsqlConnectorPool.RequestConnector(NpgsqlConnection connection)
at Npgsql.NpgsqlConnection.OpenInternal(NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnection.Open()
Below are the some project.json files and docker file which used in solution.
Core project.json file
{
"version": "1.0.0-*",
"description": "Core Class Library",
"authors": [ "Jignesh" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"dependencies": {
"Dapper": "1.50.0-beta8",
"Dapper.Contrib": "1.50.0-beta8",
"Npgsql": "3.1.0-alpha6",
"System.Net.Security": "4.0.0-beta-*",
"Microsoft.NETCore.Platforms": "1.0.1-beta-*"
},
"frameworks": {
"dnxcore50": {}
}
}
Docker file
FROM jignesh/aspnet:1.0.0-rc1-update2-coreclr
RUN printf "deb http://ftp.us.debian.org/debian jessie main\n" >> /etc/apt/sources.list
# Add the PostgreSQL PGP key to verify their Debian packages.
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
# Add PostgreSQL's repository. It contains the most recent stable release
# of PostgreSQL, ``9.3``.
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
# There are some warnings (in red) that show up during the build. You can hide
# them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
COPY . /app
WORKDIR /app/src/WebAPI
RUN ["dnu", "restore"]
EXPOSE 5002/tcp
ENTRYPOINT ["dnx", "-p", ".", "web"]
Note jignesh/aspnet is my local docker image file which I have created for 1.0.0-rc1-update2-coreclr as hub.docker.com doesn't have that.
global.json
{
"projects": [
"wrap",
"src",
"test"
],
"sdk": {
"version": "1.0.0-rc1-update2",
"runtime": "coreclr",
"architecture": "x64"
}
}
Can someone help here ? What should I do to resolve this issue.
Let me know if more information is required.
Additional Information
dnx version information in ubuntu
Microsoft .NET Execution environment
Version: 1.0.0-rc1-16609
Type: CoreClr
Architecture: x64
OS Name: Linux
OS Version: ubuntu 14.04
Runtime Id: ubuntu.14.04-x64
dnx version information in docker
Microsoft .NET Execution environment
Version: 1.0.0-rc1-16609
Type: CoreClr
Architecture: x64
OS Name: Linux
OS Version: 8 debian
Runtime Id: ubuntu.14.04-x64
As mentioned in https://github.com/StackExchange/StackExchange.Redis/issues/350 I changed RUN ["dnu", "restore"] line to RUN ["dnu", "restore","--runtime=ubuntu.14.04-x64"] in dockerfile and issue gets resolved.
Note aspnet docker image already has line ENV DNX_RUNTIME_ID ubuntu.14.04-x64 but don't know why this only is not sufficient and required to set runtime in dnu restore.
Anyway I am happy that adding runtime in dnu restore solved my problem.

Run NUnit tests in .NET Core

I am trying to run unit tests for my C# project with .NET Core.
I am using a Docker container for the runtime.
Dockerfile
FROM microsoft/dotnet:0.0.1-alpha
RUN mkdir /src
WORKDIR /src
ADD . /src
RUN dotnet restore
"NUnit" and "NUnit.Runners" have been added into project.json
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23811",
"NUnit": "3.2.0",
"NUnit.Runners": "3.2.0"
},
"frameworks": {
"dnxcore50": { }
}
Run dotnet restore successfully with the following output
...
log : Installing NUnit.ConsoleRunner 3.2.0.
log : Installing NUnit.Extension.NUnitV2ResultWriter 3.2.0.
log : Installing NUnit.Extension.NUnitV2Driver 3.2.0.
log : Installing NUnit.Extension.VSProjectLoader 3.2.0.
log : Installing NUnit.Extension.NUnitProjectLoader 3.2.0.
log : Installing NUnit.Runners 3.2.0.
info : Committing restore...
log : Restore completed in 4352ms.
I tried to run the tests with:
dotnet nunit
dotnet nunit-console
But it doesn't work.
How am I going to call the runner? Or is there another unit testing framework that works with the current version of .NET Core?
Update 4: The NUnit3TestAdapter v3.8 has been released, so it is no longer alpha.
Update 3:
With NUnit3TestAdapter v3.8.0-alpha1 it is possible now to run the tests using dotnet test command. You just need to have these dependencies in your test project:
<PackageReference Include="nunit" Version="3.7.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.8.0-*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.*" />
You can try it out!
Update 2: Visual Studio 2017 and the move from project.json to csproj made the dotnet-test-nunit test adapter obsolete, so we needed to release another updated adapter to run .NET Core tests. Please see Testing .NET Core with NUnit in Visual Studio 2017 if you are using VS2017 and the new .NET Core tooling. See the update below if you are using project.json.
Update: NUnit now has support for dotnet test, so you no longer have to use NUnitLite. See testing .NET Core RC2 and ASP.NET Core RC2 using NUnit 3.
NUnit console (and the underlying NUnit Engine) do not support running unit tests against .NET core yet. Hopefully we will get that support in NUnit 3.4.
In the meantime, you can use NUnitLite to switch your tests to a self-executing test runner.
I wrote a blog post on the process at Testing .NET Core using NUnit 3. A quick summary is;
Create a .NET Core Console application for your test project.
Reference NUnit and NUnitLite from your test project. You do not need the runner.
Modify main() to execute the unit tests.
It should look like this;
using NUnitLite;
using System;
using System.Reflection;
namespace MyDnxProject.Test
{
public class Program
{
public int Main(string[] args)
{
var writter = new ExtendedTextWrapper(Console.Out);
new AutoRun(typeof(Program).GetTypeInfo().Assembly).Execute(args, writter, Console.In);
}
}
}
For more complete information, see my blog post.
I did as Rob-Prouse suggested, with minor changes. It finally works now.
using System;
using System.Reflection;
using NUnitLite;
using NUnit.Common;
........
public class Program
{
public static void Main(string[] args)
{
var writter = new ExtendedTextWrapper(Console.Out);
new AutoRun(typeof(Program).GetTypeInfo().Assembly).Execute(args, writter, Console.In);
}
}
Some tweaks to the answer, to make it compile.
using System;
using System.Reflection;
using NUnit.Common;
using NUnitLite;
namespace NetMQ.Tests
{
public static class Program
{
private static int Main(string[] args)
{
using (var writer = new ExtendedTextWrapper(Console.Out))
{
return new AutoRun(Assembly.GetExecutingAssembly())
.Execute(args, writer, Console.In);
}
}
}
}
Note that you may also have to convert your project from a class library to a console application.

Read textfile (stored in Project) to string in ASP MVC (DNX CORE)

I have a DNX class library project in my ASP.NET MVC Project under DNX Core. In this class library I want to implement classic HTMLHelper Functions that return a string after some HTML manipulations. (No TagHelper at the moment.)
In the sense of a simple template approach I would like to first read a HTML template from a txt or html file that is stored within the project (just to make formatting, etc. more simple) to a string variable, do some string manipulations and then return the string.
I started to develop a traditional System.IO approach but recognized that it is not part of DNX Core so far. I wonder if there is a better way to access a Textfile stored within the project and read it as a string. IHostingEnvironment for instance?
Happy for any suggestions.
Regards
Marco
Did you have a look at this issue? Looking at the reverse package website there are alternatives for System.IO.
Example
Program.cs
using System;
namespace ConsoleApp1
{
public class Program
{
public static void Main(string[] args)
{
string content = System.IO.File.ReadAllText("MyTextFile.txt");
#if DNX451
Console.WriteLine("NET451");
#elif DNXCORE50
Console.WriteLine("DNXCORE50");
#endif
Console.WriteLine(content);
Console.ReadLine();
}
}
}
project.json
{
"version": "1.0.0-*",
"description": "ConsoleApp1 Console Application",
"compilationOptions": {
"emitEntryPoint": true
},
"commands": {
"ConsoleApp1": "ConsoleApp1"
},
"frameworks": {
"dnx451": { },
"dnxcore50": {
"dependencies": {
"System.Console": "4.0.0-beta-23516",
"System.IO.FileSystem": "4.0.1-beta-23516"
}
}
}
}
Use dnvm to install and select the default framework and use dnu restore to ensure all packages are restored. Execution of dnx run from the project directory wil start the program.
dnvm list
dnu restore
dnvm upgrade -r clr -arch x64
dnx run
dnvm upgrade -r coreclr -arch x64
dnx run

Categories

Resources