Edi Wang

Microsoft MVP for Azure

.NET C# Programming Language and .NET Platform

如何让应用程序要求以管理员身份运行(C#)

从Windows Vista开始,为了防止操作系统被恶意软件破坏,Windows加入了UAC机制,在没有关闭UAC的时候,用户的程序都没有管理员权限去执行。但是如果软件需要做一些操作,比如读写注册表,就需要以管理员身份启动。然而普通用户并不清楚这一点,这有可能导致我们的程序被差评。幸好,用.NET开发的程序可以很方便的做到自动要求以管理员身份运行。首先为了达到演示效果,我需要在程序界面上通知当前环境是不是以管理员身份运行,为此我找了一个UACHelper,这个东西挺实用的,就算不是以管理员身份运行的,也不会在用户面前直接爆掉。
C# Windows UAC

巧用委托简化重复代码

在C#编程中通常会碰到许多重复劳动的情况,而面向对象编程的原则就是消除这些重复劳动,增加代码重用性。我们虽然有很多基本的模式去做抽象和封装,但还是会碰到一些看似不得已的重复劳动的情况。比如你在写一个数据访问层的类,按照规范,每个方法都需要加try-catch,就像这样: public void Method1() { try { // logic... } catch (DataException ex) { // fuck the ex... } } public void Method2() { try { // logic... } catch (DataException ex) { // …
C# lambda Delegate

如何手动将一个普通.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。细心的观众 …
.NET

Performance tips for Entity Framework

自从我用了EF,每次都很关心是否有潜在的性能问题。所以每次我写LINQ查询,都会使用SQL Profiler看一下实际生成的SQL语句,以便发现潜在的性能问题。也强烈建议大家这么去做,以免日后软件大了出了问题很难查。 一、只选择某列或某些列 有些时候,在C#里写LINQ虽然看着舒服,但性能不一定好,所以有必要做一些调整。比如这种情况: 我需要知道一篇文章的点击数,仅此而已,我可能会写: context.Post.FirstOrDefault(p => p.Id == postId).Hits; 或者: context.Post.Find(postId).Hits; 我期待着他们只去数据库里筛选Hits这一列的数据,然而,通过SQL Profiler会发现,这两条语句居然把全部列都给select出来了,访问Hits的操作实际是在内存中进行的。 虽然小表看不出性能问题,但万一你的表里有一列是存 …
C# ADO.NET Performance

How to Manually Upgrade Entity Framework 4.0 to 5.0

I have been working on the next version of my blog recently, and one of the updates is upgrading Entity Framework to version 5.0. When I initially set up the blog, I used EF4 that came with VS2010. The upgrade process from version 4.0 has to be done manually; using NuGet for the upgrade can only automatically update to versions 4.1 and above. Yesterday, I worked on it for a long time, and finally got the website up and running. Here's what I learned from the experience.
Entity Framework ADO.NET

给ASP.NET Membership增加权限(Rights)功能

首先说明,本文用的是非常屌丝的办法,针对SQL Server Membership的实现的一个很有局限性的权限模块,而不是真正对ASP.NET Membership本身的拓展。不喜慎入。 一、为什么要给Memebership增加权限系统 我们知道,ASP.NET Membership是基于Role的,其实是个RBAC,没有权限(Rights)功能。也就是说,我们只能够指定某个Role可以做什么事,丧失了一定的灵活性。比如,网站后台有个编辑文章的功能。如果基于Role判断,我只能认为:所有Admin、Editor、Teacher可以编辑文章。在MVC3里,可以用Authorize(Role=”Admin, Editor, Teacher”)的属性做到这一点。然而如果想要动态可配,则比较难了。然而,如果是基于Rights的验证,那我就可以认为:所有具有Edit权限的Role可以编辑文章。这样的 …
ASP.NET Membership RBAC

Store and Read Images via SQL Server in ASP.NET MVC 3

First of all, I don't recommend storing images in the database, especially in a web project. Because file systems can provide static file access and get many benefits such as performance. Storing images in the database has certain limits. But if you must store images into SQL Server, here's how to do it.  SQL Image files are in fact, binary data. SQL Server provides us the image type for …
ASP.NET MVC SQL Server

Model Validation in ASP.NET MVC3

ASP.NET The model in MVC3 is self-validating, which is via the System.ComponentModel.DataAnnotations namespace from NET4. All we need to do is add the corresponding Attributes to the attributes of the Model class, and then the MVC3 framework will do the validation for us. I will use the login that comes with the MVC3 project template as an example to explain the validation of the model. 1. Enable Client Validation Client-side validation is mainly to improve the user experience, and the validation is completed without the web page being brushed back. The first step is to enable client validation in web.config, which is already available in the template project that comes with MVC3
ASP.NET MVC

在SQL Server完整版上部署ASP.NET Membership

平时做练习的时候,用的Membership虽然是SQL方式的,但版本是SQL Express,连接的是网站App_Data文件夹里的mdf文件。如果要把同样的Membership部署到SQL完整版上是会爆的。比如你开发的时候自己用的Express,部署到你的主机服务商时,人家当然用的付费购买的完整版。这样就会爆。网上查了些资料,说Membership是SQL Express独有的功能,我不相信。所以自己开荒了一下,并且成功了。分享给大家。 首先,连接网站App_Data目录的mdf文件,确实只有Express版可以。所以完整版的SQL Server必须在数据库引擎里建立数据库,不要放在网站目录下。 建完数据库以后,打开VS2010的命令行环境,运行aspnet_regsql,你会看到下面这个界面。 选择第一项,即在SQL Server里配置Application Services,然后点
ASP.NET Membership SQL Server

ASP.NET MVC3和传统WebForm共存时,Chart控件无法显示的解决办法

最近把博客的前台改到了MVC3上去,后台还是WebForm做的。一直有个小bug,今天才刚刚解决。后台页面里有个地方用了ASP.NET 4自带的那个Chart图表控件。原来没用MVC的时候是可以显示的。换了MVC之后,WebForm的代码没有改过,其他都好用,就这个Chart爆了。具体表现为,网页没有报错,就Chart的部分显示不出来。今天检查了一下,并最终解决了这个问题。 首先我在firebug里发现,Chart控件生成的图片是来自一个handler的,但加载失败了: 根据最近玩MVC被坑的经验,可能是URL路由引起的。我去后台日志查看了一下,果然是这样的: 解决办法很简单,在Global.asax里写一条忽略URL路由的规则: routes.IgnoreRoute("Console/Dashboard/ChartImg.axd");   但我很奇怪,MVC3默认已经在Global. …
ASP.NET Chart MVC WebForms