Showing posts with label ASP.NET Core 1.0. Show all posts
Showing posts with label ASP.NET Core 1.0. Show all posts
Saturday, April 7, 2018

Video Recording : Webinar on ASP.NET Core on Linux

I have done a Webinar for DotNetTricks.com about ASP.NET Core on Linux. There were lots of curiosity about it and lots of people asked for the recording of Webinar so here we go Following is a link of recording of Webinar for ASP.NET Core on Linux. You can watch the full webinar on youtube at following.

https://www.youtube.com/watch?v=Hf0F7nZCTXM&t=706s



I would also like to Thanks Shailendra and Whole DotNetTricks team for the having me on this webinar.

Thank you. Stay tuned there were lots of stuff coming from ASP.NET core and Node.js.
Share:
Saturday, March 10, 2018

Solution: (Error)Unable to Correct problems, You have held broken packages–Install .NET Core on Ubuntu

Recently, I have been using Linux(Ubuntu) machine on my virtual box quite a bit and as  Microsoft.NET Developer I want to install .NET Core on Ubuntu. So I tried following the instruction from below link to install .NET core on Ubuntu 16.04.

https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x

But I was not unable to install .NET Core and I was getting following errors.

The following packages have unmet dependencies:
  dotnet-sdk-2.1.4 : Depends: dotnet-runtime-2.0.5 but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
You can see the same error in the following screenshot.

error-ubuntu-net-core-214

I was a little bit curious about that and then I have read carefully the documentation mentioned on the above link and I have found that .NET Core require following require following Ubuntu distributions as Prerequisites.

  • libunwind8
  • liblttng-ust0
  • libcurl3
  • libssl1.0.0
  • libuuid1
  • libkrb5-3
  • zlib1g
  • libicu52 (for 14.X)
  • libicu55 (for 16.X)
  • libicu57 (for 17.X)

Most of the time it should be installed by default with sudo apt-get update and sudo apt-get upgrade command but sometimes it does not. So what I did install all these distributions one by one via sudo apt-get install command and in my case, I was not having liblttng-ust0 distribution on my machine. So I have installed it via the following command.

sudo apt-get install liblttng-ust0
You can also see below screenshot for the same.

install-ubuntu-distribution-for-netcore

Then I was able to install .NET Core on my machine via following above link on documentation.

install-netcore-ubuntu

And now it’s running .NET Core v 2.1.4 on my Ubuntu VirtualBox as you can see it in below dotnet version command.

net-core-version-command

I was able to solve my problem this and so I thought it will be a good idea to share it on my blog so it will help people who are having the same problem.

Stay tuned for more there is a lot more coming for asp.net core and node.js on Linux on future blog posts.
Share:
Saturday, January 28, 2017

How to upgrade your ASP.NET Core application from 1.0 to 1.1

TL;DR;

In this blog, We are going to learn how we can upgrade your ASP.NET Core application from 1.0 to 1.1.  Now Microsoft has release 1.1 before few months, So in this blog, we will see how you can upgrade your ASP.NET 1.0 application to ASP.NET 1.1 core application.

Where to download 1.1 SDK and Tooling for Visual Studio 2015:

You can download 1.1 SDK and Tools from the following link.

https://www.microsoft.com/net/download/core?tduid=(42c8fb30096a508e70a0e7f0d6bdaf55)(256380)(2459594)(TnL5HPStwNw-1.6L2HANlqPr5.uZiHJ1MQ)()#/current

where-to-download-tooling-and-sdk-for-core1.1

Here different tooling is also available for the Visual Studio 2015 and Visual Studio 2017. For this blog post, We are going to use Visual Studio 2015.

How to upgrade to ASP.NET core 1.0 to ASP.NET core 1.1:

So let’s get started. I’m going to create a new ASP.NET Core 1.1 application and then we will move it to ASP.NET 1.1. So let’s create an ASP.NET core 1.0 application and then let’s upgrade it to 1.1. I have created a new application like following.

creating-new-aspnet-core-app-to-upgrade-to-11

Once you click ok it will have a dialog selecting application type and I’m going to select web application.

web-application-aspnet-core-11

Once you click Ok, It will create an application like below and you can see that it’s an ASP.NET Core 1.0 application.

aspnet-core-1.0-app-in-solution-explorer

And here’s how it’s project.json looks like.
{
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.1",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.1",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Routing": "1.0.1",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
  },

  "tools": {
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

And here’s how the global.JSON looks like.
{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003131"
  }
}
So the first thing we need to is to upgrade the global.json to latest version like below.
{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-1-003177"
  }
}


So here in project.json, you can see that all packages belong to 1.0. Now there is multiple ways we can do upgrade either we can manually upgrade all packages via manually upgrading it or we can do it with Visual Studio tooling. I’m going to use Visual Studio tooling as that is the easiest way to do it. You can do this via Right Click your application and Select “Manage Nuget Packages for this solution”. It will load the following dialog. You need to goto Update tab of that dialog.

upgrade-nuget-package-to-1.1

You need to select upgrade all packages and update your application. Once you did with that there is once place where you need to manually upgrade it in project.json. GoTo following part of project.json.
"frameworks": {
  "netcoreapp1.0": {
    "imports": [
      "dotnet5.6",
      "portable-net45+win8"
    ]
  }
},
Now upgrade netcoreapp1.0 to 1.1 like below.
"frameworks": {
  "netcoreapp1.1": {
    "imports": [
      "dotnet5.6",
      "portable-net45+win8"
    ]
  }
},
Now once you do that and build your application. You might get the error like following.

error-for-runtime-application-net-core

That is because of windows runtime is not there for 1.1. So you need to add following in project.json.
"runtimes": {
  "win10-x64": {}
},
Now once you add that in project.json. It will restore the runtime for your operating system and it will start building your application.
And here’s how my project.json looks like after upgrading all stuff.
{
  "dependencies": {
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "BundlerMinifier.Core": "2.2.306",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.Extensions.Logging.Debug": "1.1.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
    "Microsoft.NETCore.App": "1.1.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0",
    "Microsoft.AspNetCore.Diagnostics": "1.1.0",
    "Microsoft.AspNetCore.Mvc": "1.1.1",
    "Microsoft.AspNetCore.Routing": "1.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.AspNetCore.StaticFiles": "1.1.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
    "Microsoft.Extensions.Configuration.Json": "1.1.0"
  },

  "tools": {
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.1": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },


  "publishOptions": {
    "include": [
      "wwwroot",
      "**/*.cshtml",
      "appsettings.json",
      "web.config"
    ]
  },

  "runtimes": {
    "win10-x64": {}
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

And in solution explorer, you can see that now it’s targeting to 1.1.

targeting-11-upgrade-to-11-solutionexplorer

That’s it. It’s pretty easy to upgrade your application to latest build. Hope you like it. Stay tuned for more!!
Share:
Tuesday, January 17, 2017

How to use NancyFx with ASP.NET Core Application

TL;DR;

In this blog post, we are going to learn how we can use Nancy framework with ASP.NET Core Application.

NancyFx Introduction:

NancyFx is a lightweight, low-ceremony framework for building HTTP based services on .NET and Mono. It is inspired by Sintara Framework for Ruby and hence Nancy was named after the daughter of Frank Sintara. Nancy Framework is a great alternative to ASP.NET APIs. It follows “Super duper happy path” phrase. It has following goals.
  • It just works – You should just use it without learning so much thing from it. Create a Nancy module and that’s it.
  • Easily Customizable – There are tons of customization available and then you can easily customize it.
  • Low-ceremony- With the minimal code you will able to run NancyFx.
  • No Configuration Required – There is no configuration required and very easy to setup.
  • Host-agnostic and Runs anywhere-  It will run on any server, self-hosted etc.
  • Low Friction- When you build software with NancyFx APIs it will help you where you want to go rather than coming in your way. 

How we can use NancyFx in ASP.NET Core:

So let’s see how we can use NancyFx in ASP.NET Core, Let’s create  ASP.NET Core API Application via File –> New project in visual studio.

create-project-nancy-api

Once you are done with creating an ASP.NET Core API application delete the controller folder and add following nuget packages.
    • Microsoft.AspNetCore.Owin: “1.0.0”
    • Nancy: “2.0.0-barneyrubble”
You can directly put  in project.json like following.

project-json-nancyfx

Now once we are done with adding packages, We need to make sure our application uses NancyFx and handles requests instead of ASP.NET MVC. So remove “app.UseMVC” in the startup.cs file and add following code in configure method.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseOwin(n => n.UseNancy());
}
Now we need to create a new Nancy module that will handle request. For this blog post, we are going to create a Home Nancy module like following.
using Nancy;

namespace NancyCoreAPI.Module
{
    public class HomeModule : NancyModule
    {
        public HomeModule()
        {
            Get("/", args => "Hello world from nancy module.");
        }
    }
}
Here in the above code, You can see that it is a standard Nancy module where you just need to write methods in the constructor and it will return text or HTML based on requirement. In our case here it will return “Hello World from nancy module” text. Once you run application browser it will look like following.

hello-world-from-nancy-module

That’s it. You can see that Even with ASP.NET Core it is very easy to use and almost no configuration required at all.

You can find complete source code of this blog post at following location on GitHub- https://github.com/dotnetjalps/CoreNancyAPI
Share:
Saturday, October 22, 2016

Opening ASP.NET Core Visual studio solution in Visual Studio code

In this blog post, We are going to see how we can open an ASP.NET Core solution in visual studio code.  So what we are waiting for. First, we are going to create an ASP.NET Core web application like below.

ASPNETCoreApplication

Then select web application like below.

ASPNETCoreWebApplication

Now once you created Ok it will create an ASP.NET Core Web Application like below.

SolutionExplorer

Now let’s run that application to make sure that its work fine.

edgeASPNETCoreApplication

Open ASP.NET Core Solution in Visual Studio Code:

For those who don’t know What is Visual Studio code is here is the definition from the Wikipedia.

Visual Studio Code is a source code editor developed by Microsoft for Windows, Linux and macOS. It includes support for debugging, embedded Git control, syntax highlighting, intelligent code completion, snippets, and code refactoring
It is a cross-platform open source editor for writing code from Microsoft. You can find more information about that from the following links.

https://en.wikipedia.org/wiki/Visual_Studio_Code
https://code.visualstudio.com/

Now we are going to see how we can open the same application with Visual Studio code. But before doing that we need to make sure that the computer have .NET Core installed on your machine and Also make sure that Visual Studio Code C# support is there in Visual Studio code. You can find more about that from the following link.

https://code.visualstudio.com/Docs/languages/csharp
https://code.visualstudio.com/docs/runtimes/dotnet

Since Visual Studio Code is not a full fledge IDE(Integrated Development Environment) like Visual Studio. We need to open the folder which contains the solution and source code. You can do that from Visual Studio Code.

OpenFolderInVisualStudioCode

Then select the src folder of your application like below.

SRCFolderApplicationinVisualStudioCode

Once you click on “Select Folder” it will open source code like below.

SourceCodeInVisualStudioCode


Now when you open any CSharp file on the right bottom you will see a text like installing OmniSharp.  OmniSharp is a set of tooling and editor integrations and Libraries that together creates great IntelliSense and  other tooling support for popular editors like Code, Sublime Text, Brackets, Atom,Emacs and Vim etc. You can find more information about that on the following link.

http://www.omnisharp.net/

Once you click on that text you can also see the progress of installation of Omnisharp libraries in output windows like below.

OmniSharpOutputWindowVisualStudioCode

Once it installed it will automatically select the project which you have in solution. If you have multiple projects then you can click on that and it will ask you to select the project for which you need all IntelliSense .

SelectApplicationInVisualStudioCode

That’s it. Now you have most of the experience that you got in Full Visual Studio and full fledge IntelliSense to all the .NET Features.

VisualStudioCodeExperience

Disclaimer: I have tested it on the windows machine and its works perfectly fine. For macOS and Linux I would expect the same. For further information, you can follow a GitHub Issue which is already created by the community. You can find that GitHub issue at the following link.

https://github.com/OmniSharp/omnisharp-vscode/issues/283

That’s it. It’s very easy to use light weight Visual Studio Code for same development experience like Full Fledge Visual Studio.
Share:
Tuesday, June 14, 2016

How to create Rest API(Web API) with ASP.NET Core 1.0

Recently Microsoft has released ASP.NET Core 1.0 RC2, I am getting lots of request from readers that about creating Web API. So I thought it will be a good idea to write a blog post about how to create Rest API(Web API) with ASP.NET Core 1.0.

So let’s get started via creating an ASP.NET Core 1.0 Web Application like following.

creating-aspnet-core-project

Once you click on ASP.NET Web Application, It will ask whether you need to create Web Application or Web API application. We are going to Web API so I am going to select Web API Application like following. Please note that in ASP.NET Core 1.0 there is no separate libraries or DLLs required for creating web APIs. This is just a project template.

creating-aspnet-core-api-project

Now once you click on OK It will create a Web API application with default values controller and program.cs.  As you know Program.cs is now starting point for the ASP.NET Core 1.0 application so It contains all the required configuration and startup items. Following is a code for that.
using System.IO;
using Microsoft.AspNetCore.Hosting;

namespace CoreWebApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }
    }
}

Here you can see that there is a WebHostBuilder class which hosts the application and there is some configuration for using this application on IIS and Kestrel which is  a cross-platform server from Microsoft.

Now let’s create our model class first. I have created an Employee model class like following.
namespace CoreWebApi.Model
{
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Designation { get; set; }
    }
}
Since now our model is ready, It’s time to create the controller. You can create web API controller via add new item like following.

creating-aspnet-core-api-controller-class

And here the code for our get Method.
using System.Collections.Generic;
using CoreWebApi.Model;
using Microsoft.AspNetCore.Mvc;

namespace CoreWebApi.Controllers
{
    [Route("api/[controller]")]
    public class EmployeeController : Controller
    {
        // GET: api/values
        [HttpGet]
        public IEnumerable<Employee> Get()
        {
           var employees = new List<Employee>
           {
               new Employee {EmployeeId = 1,FirstName = "Jalpesh",LastName = "Vadgama",Designation = "Technical Architect"},
               new Employee {EmployeeId = 2,FirstName = "Vishal",LastName = "Vadgama",Designation = "Technical Lead"}
           };
            return employees;
        }
    }
}

Here you can see that I have created a get method that returns a List of Employee. Now Let’s run this in our browser and it will work like following.

aspnet-core-api-browser-sample
You can find complete source code of this blog post at following location on Github-https://github.com/dotnetjalps/ASPNetCoreWebAPI
That’s it. Hope you like it. Stay tuned for more!
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews