Edi Wang

.NET and Azure Developer

.NET C# Programming Language and .NET Platform

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, …
.NET ICMP

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 …
.NET NLog

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; …
.NET ASP.NET

Windows 10 UWP: Undo / Redo on InkCanvas

The InkCanvas in UWP only got pens by default, it can not perform Undo or Redo. To implement this, we will need to code for ourselves. Official document covered Undo functionalilty, but not redo. Today, I have successfully done it, and I'd like to share with you. First, you need to add two custom buttons on the InkToolbar for Undo / Redo 1. Undo the Ink We need a few APIs. To …
UWP

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() …
.NET ASP.NET

Azure Website Showing 404 for .woff Fonts in IE

If you are using Website on Microsoft Azure (Currently renamed to Web Apps), you will find the .woff web font is not working in IE, it returns 404. Based on my experience, this is because IIS is not configured to use the correct MimeType. However, we can not operate the IIS on Azure, there's no RDP into an Azure Web Apps backend machine. How can we do that? In fact, after IIS7, the MineType is …
IIS Azure

如何高逼格读取Web.config中的AppSettings

先插句题外话,下版本的ASP.NET貌似把web.config撸掉了,都变成json了。所以本文讨论的内容可能以后用不到了,但是一些设计思想还是可以用的~ 直接进入正题,在ASP.NET网站里(也包括其他有web.config, app.config)的.NET工程里,读AppSettings的值是个很常见的场景。比如: 在代码里读的时候就会用到: ConfigurationManager.AppSettings["EnableAzureWebTrace"]; 这个[]索引器返回的是string类型。所以下一步我们通常需要类型转换才能在代码里拿来用。比如这个例子里,我们就要转换成bool。其他时候,可能要转换为int等类型。 string enableAzureWebTraceConfig = ConfigurationManager.AppSettings[" …
ASP.NET

Portable Class Library中如何调用WCF OData Service

上个月发布了一个WP8.1应用:NuGet Search,并在GitHub开了源:https://github.com/EdiWang/WP-NuGetSearch NuGet的服务接口是个WCF OData Service,为了装逼,我尝试使用了跨Framework的PCI工程,结果no zuo no die了。和一般.NET类库不同,PCL有些tricky的地方要爆。下面是开荒成功的代码: public async Task GetDataAsync(string searchTerm, int pageIndex, bool includePreRelease = false) { try { IDictionary queryOptions = new Dictionary { { "filter", …
WCF OData

Run Scheduled Tasks in ASP.NET Application

在ASP.NET里运行定时任务,这是个老生常谈的话题了,撇开那些用per request搞定屌丝办法,目前最好的解决办法只有2种: 1. 如果你有大微软的Azure,可以直接在网站服务中找到Jobs,自己看一下就会了 2. 如果你是屌丝,买不起Azure,就用本文介绍的WebBackgrounder搞 由于ASP.NET是服务器端Web框架,所以一般而言,一个操作的往往是只有收到客户端Request之后才能执行的,如果网站一直没人访问,没有Request进来,如何执行代码呢?定时任务就是这种坑爹场景。 还好,大微软的MVC帝、ASP.NET小王子haacked蜀黍给我们写了个 http://www.nuget.org/packages/WebBackgrounder/ 专门捣鼓这种场景。作为一个屌丝程序猿,和大牛的区别就在于“好编程,不求甚解”。所以我们不必在意它是怎么实现的,只要会用就行 …
ASP.NET Jobs

ASP.NET MVC ActionFilterAttribute的执行顺序

ASP.NET MVC里面我们要自定义ActionFilter的时候会发现有4个方法可以override:OnActionExecuting,OnActionExecuted,OnResultExecuting,OnResultExecuted。他们分别在什么时候执行一直是困扰人类的一个问题。我代表人类做了一个简单的实验: 首先自定义一个ActionFilter,每个方法执行的时候都在VS的OUTPUT窗口输出信息: public class TestActionFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { Debug.WriteLine("OnActionExecuting"); …
ASP.NET MVC