Edi Wang

.NET and Azure Developer


Use ICMP Ping in .NET Core 2.0

In classic .NET Framework applications, we have used System.Net.NetworkInformation.Ping class to Ping a host address. This API is not included by default in .NET Core, even in version 2.0. Here is how to do it in a .NET Core way. First, we need to reference a package System.Net.Ping Install-Package System.Net.Ping This will give us the same API set as .NET Framework. Then, in your .NET Core code, …

Use NLog in ASP.NET Core 2.0

There's official update for NLog targeting ASP.NET Core 2.x, for the latest methods please refer to https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-2 Recently I am porting a classic ASP.NET MVC 5 project to .NET Core 2.0, in order to run it on Linux. One of the parts that has differences between .NET Fx and .NET Core is logging. I choose NLog as my logging providor, let's …

Get Client IP Address in ASP.NET Core 2.x

In classic ASP.NET we used to get client IP Address by Request.UserHostAddress. But this does not apply to ASP.NET Core 2.0. We need a different way to retrieve HTTP Request information. 1. Define a variable in your MVC controller private IHttpContextAccessor _accessor; 2.  DI into the controller's constructor public SomeController(IHttpContextAccessor accessor) { _accessor = accessor; …

Configure Continuous Delivery for ASP.NET Core Website on Azure

Today I am trying new DevOps tools in VS2017 and Azure, my goal is to automate the process from development to production for an exsiting ASP.NET Core project. I have encountered some issues, and with the help of Microsoft Support, I was able to solve them and share with you guys. First, there are two ways to configure CD for Azure Web Apps. I prefer create a website first, then configure it from …
.NET ASP.NET Azure DevOps

Read AppSettings in ASP.NET Core 2.x

Today, I was rewriting an old ASP.NET MVC5 Demo project to ASP.NET Core, and found that the way we used to read Web.config by ConfigurationManager.AppSettings[] is no longer working. .NET Core has many new ways to achieve this. I picked one that suitable for my project. Here is how I do it. The Classic ASP.NET Code web.config Controller private static CloudBlobContainer GetBlobContainer() …

How to Wrap Event-based Asynchronous Method into Task-based async Method

自从.NET4.5推出async/await语法,我们又多了一个炫耀的工具,可以用它来炫耀自己的编程水平,然后再嘲笑Java。然而,有些老旧的API只提供了基于事件的异步方法,而没有提供返回Task的异步方法,这会影响我们的炫耀效果。就像Windows Phone 8的webclient,只提供了基于事件的DownloadStringAsync方法,写的时候就像这样: var client = new WebClient(); client.DownloadStringCompleted += (s, e) = { ... }; client.DownloadStringAsync(...); 为了保住我们的逼格,大微软早就为我们提供了解决方法:TaskCompletionSource,非常适合把基于事件的异步封装成Task方法。这个类型返回是一个可以被await的Task,事件 …
.NET Async Async Await

如何手动将一个普通.NET类库转换成可移植类库(Portable Class Library)

可移植类库是.NET程序员装逼的必备良药,至于它有多牛逼,我也不知道,大家可以自己看一下 http://msdn.microsoft.com/en-us/library/vstudio/gg597391%28v=vs.110%29.aspx 但是万一你装逼的时候手一滑,建成了一个普通的Class Library肿么办?普通青年的做法是删掉再建一个,这当然是OK的。但是,作为文艺青年,我们可以继续装逼:自己动手把这个类库转换成Portable Class Library。 为了演示,我先建一个普通的.NET类库:AVeryNBClassLibrary 在这个项目的属性页里面,自然是看不到Portable Class Library的选项的。 接下来,在项目上点右键,选择Unload Project,然后再次右键选择Edit AVeryNBClassLibrary.csproj。细心的观众 …