Tag Archive: .Net


if you get this error, it’s because Sliverlight assembly is not in GAC. Do this to fix:


cd C:Program FilesMicrosoft Silverlight4.0.50524.0
gacutil -i System.Core.dll
gacutil -i system.dll
gacutil -i system.net.dll
gacutil -i system.xml.dll
gacutil -i System.Runtime.Serialization.dll

check out what other people said.

廣告

If you get something like this:

Server Error in '/applicationname' Application.
---------------------------------------------------------------------------
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Requested registry access is not allowed.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:
[SecurityException: Requested registry access is not allowed.]
Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) +473
....

I’m not sure there’s a secured way, but adding permission for iis daemon user solve the problem for me.

1. Find out which deamon user your app is using, by looking up App_Pool and secruity section in event log.

2. Give the user permission to read/write access for the eventlog registry entry. To do so, open regedit and navigate to the key:

HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesEventlog

.
Right click and select Permissions, and add permission for the user.

3. restart iis and give it a try.

MySQL 5.1.x & unicode

In our recent upgrade from 5.0 to 5.1.47, the new version behaves differently and it screw up character set when user submit info from our web app powered by .NET.

After digging around for a afternoon, this is how to fix it:

vi /etc/my.cnf

And add those lines to the config:

init_connect='SET collation_connection = utf8_general_ci'
init_connect='SET NAMES utf8'
default-character-set=utf8
character-set-server=utf8
collation-server=utf8_general_ci
skip-character-set-client-handshake

There are other options for collation besides utf8_general_ci such as utf8_unicode_ci. See this article:
http://dev.mysql.com/doc/refman/5.1/en/charset-unicode-sets.html

Reference: http://rhyous.com/2009/11/05/how-to-create-a-utf-8-unicode-database-on-mysql/

C#基礎概念25問

1.靜態變數和非靜態變數的區別?
2.const 和 static readonly 區別?
3.extern 是什麼意思?
4.abstract 是什麼意思?
5.internal 修飾符起什麼作用?
6.sealed 修飾符是幹什麼的?
7.override 和 overload 的區別?
8.什麼是索引指示器?
9.new 修飾符是起什麼作用?
10.this 關鍵字的含義?
11.可以使用抽象函數重寫基類中的虛函數嗎?
12.密封類可以有虛函數嗎?
13.如果基類中的虛屬性只有一個屬性訪問器,那麼繼承類重寫該屬性後可以有幾個屬性訪問器?如果基類中有 get 和 set 兩個呢?
14.abstract 可以和 virtual 一起使用嗎?可以和 override 一起使用嗎?
15.介面可以包含哪些成員?
16.類和結構的區別?
17.介面的多繼承會帶來哪些問題?
18.抽象類別和介面的區別?
19.別名指示符是什麼?
20.如何釋放非託管資源?
21.P/Invoke是什麼?
22.StringBuilder 和 String 的區別?
23.explicit 和 implicit 的含義?
24.params 有什麼用?
25.什麼是反射?

I finally powered up again with my newly purchased dual-core laptop, which also enables me to refresh software installations that I’ve been wanting to do for a long time.  As I’m putting things back together, I kept running into this error with my projects:

The current identity (xxx\ASPNET) does not have write access to ‘C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files’.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Here’s a quick fix to the problem (do this only to your dev box):

1. From the framework directory (Usually c:\windows\microsoft.net\framework\v2.0.50727\Config), modify machine.config:

<processModel userName="SYSTEM" password="autoGenerated"/>

2. Execute this command:

> aspnet_regiis -ga “ASPNET"

By applying MS’s Web updates (often times including security patches), or putting you box into domain (which I am), the security constriant of your windows install drive got stricted.  If you read the prompt from aspnet_regiis carefully, switch -ga is granting the daemon user access to the IIS metabsae and other directories used by ASP.NET, which is exactly what I’m looking to do..

Further Reading:
Microsoft’s KB: http://support.microsoft.com/kb/315158/

《作者簡介》蔡學鏞,清華大學資訊工程碩士、元智大學資訊系兼任講師、美商 O’Reilly 出版社技術編輯、臺灣微軟特約專欄作家、寰震科技技術顧問、研討會講師。蔡學鏞先生著譯有多本 Java 書籍,並在臺灣和大陸的多本 IT 雜誌開闢技術專欄,專長的語言為 C/C++、C#、Java、REBOL。他的聯繫方式:xy.cai@msa.hinet.net

想要讓開發出來的程式碼穩固且容易維護,除了有必要適當地採用設計模式(Design Patterns)之外,也有必要採用編程指導方針(Coding Guidelines),做為寫程式時依循的準則。設計模式是在設計階段進行的,主要由系統設計者(System Designer)負責,所以和程式員的關係比較不是那麼密切。至於編程指導方針,就和程式員的關係相當密切了。對C#來說,最重要的編程方針是美國微軟MSDN網站上的〈Design Guidelines for Class Library Developers〉。

從他人的經驗中,學習程式技巧和好習慣
我的工作有一部份是在Code Review(Code Reading),也就是檢視別人寫的程式碼。當我在讀別人的程式碼時,深深覺得:「程式編譯成功,不代表就沒問題」。語法使用正確,且API呼叫正確,不代表程式碼就是好的程式碼。好的程式員,除了熟悉語法和API之外,也應該要熟悉許多良好的程式習慣和技巧,這些習慣和技巧,可以隨著經驗的累積慢慢培養,或者藉由學習別人累積的經驗快速培養。當然,我傾向於使用快速的方式,所以,只要遇到這一類的書,我都不會錯過。多讀這一類的好書,你會發現程式技巧進步很快。目前這一類的書,以C/C++最多,Java次之,至於C#最少(畢竟C#是比較新的語言)。C/C++以Scott Meyers的《Effective C++》和《More Effective C++》為代表作,Java以Joshua Bloch的《Effective Java Programming Language Guide》為代表作。至於C#,除了可以參考前面提到的MSDN 〈Design Guidelines for Class Library Developers〉,今年也有數本評價不錯的書出版了,包括了:
* 《.NET Gotchas》(O’Reilly)
* 《Effective C#》(Addison-Wesley)
* 《Practical Guidelines and Best Practices for Microsoft Visual Basic and Visual C# Developers》(Microsoft Press)

如果這三本書你都想讀的話,我建議先讀《.NET Gotchas》,這本書讀起來比另外兩本更有趣。75個改善.NET程式的技巧 《.NET Gotchas》共有八章,共計75個Gotchas。Gotcha就是Got You的意思,有別於Effective書籍喜歡將每一個技巧名為Item,.NET Gotchas將這些技巧俏皮地稱為Gotcha。 每個Gotcha都包括了:

  • 編號
  • 名稱:使用一個完整的句子,所以很容易理解。
  • 敘述:詳細地描述此技巧。
  • 範例:同時提供C#和Visual Basic .NET版本的範例。
  • In a Nutshell:用一段文字作結論。

相關參考條文第一章介紹的主題是CLR/.NET Framework,第二章是Visual Studio和編譯器,第三章是語言和API,第四章是不同語言之間的合作,第五章是垃圾收集,第六章是繼承和多型,第七章是多執行緒,第八章是COM和.NET企業服務。當我最近在Review別人的程式時,我常常會覺得可惜:「如果這個程式員讀過《.NET Gotchas》就不會犯這樣的錯誤了」。《.NET Gotchas》將許多常犯的疏失一一指正出來。 想要改善.NET程式碼的品質,除了讓程式員多多閱讀像《.NET Gotchas》這類的好書之外,也可以採用一些自動化的工具幫助檢查,減少不好的編程風格。

近來大家目光的焦點都在Design Patterns,卻忽略了程式技巧的重要性。有好的Design Pattern架構,但是卻沒有好的程式技巧,依然無法產生高品質的程式。我建議所有的.NET程式員都去閱讀本文所提到的3本.NET程式設計技巧書籍,並採用devAdvantage以及FxCop這兩個工具。

誰都會寫程式碼!幾個月的編程經驗可以讓你寫出「可運行的應用程式」。但在開發團隊的運行模式下,以最有效率的方式編碼、能被小組成員順暢承接的程式碼就必需要下更多的功夫!

要知道,大多數程式設計師在寫「可運行程式碼」,而不是「高效率程式碼」。寫高效程式碼是一項藝術,必須靠紀律及學習來實踐。建立一個高效率、有默契的開發團隊更需有一定的規範及標準流程。

想要讓開發出來的程式碼穩固且容易維護,除了有必要適當地採用設計模式 (Design Patterns) 之外,也有必要採用編碼原則 (Coding Guidelines),做為寫程式時依循的準則。設計模式是在設計階段進行的,主要由系統設計者 (System Designer)負責,所以和程式員的關係比較不是那麼密切。至於編碼方針,就和程式員的關係相當密切了。

程式碼命名及格式規範的目的為建立開發團隊的默契以及效率。檢視一篇與自己撰寫風格相同的程式碼不但能減少無謂的猜測,也能快速暸解程式碼,立即進入程式撰寫的狀況。

輔助工具
在Google搜尋keyword :: “Code formatter, code beautifier",你可找到一票自動化的輔助工具。對C#來說,最重要的編程方針是美國微軟MSDN網站上的Design Guidelines for Class Library DevelopersreSharper則是目前我們使用的自動化工具。

編碼原則
Ascertaint C# Coding Standard

相關文章
迅速提升.NET程式設計技巧 – 75個訣竅幫助你減少程式中潛在的問題

In a Team development environment where parallel development and testing goes we find a requirement of getting most recent build to test as well as some specific build to give the QA to do testing on. This post describes what all are needed to set up an automated source code integration and build system for an environment where we use Visual Studio for development, source control (SVN in our case) for source depot, and doc/ tracking system (trac in our case).

Why do we need a continuous source code integration and build system? well, this helps the QA team by allowing then to not spend more time in syncing the source and doing a build everytimes or even working on a relatively older build.  This works as a perfect backup for your Source Control and also the deployment server. This also allows to align the builds numbers to be same as your assemblyfile version and also to the source control label there by making a particular build to be a complete unit in itself in terms of readiness for deployment or using the same for development.

Now coming back to build server where we can use cruise control .NET for source integration along with a couple of batch scripts to control the build number and assembly version. CCNET pings the source control (SVN) after every 30 mins (this is a default value and can be changed) to see if there are any modifications. If there are then it triggers a build and publishes it on the Build Server. Once done sleeps again for 30 mins unless it is Forced for a build in between i.e. you can manually force a build in between sleeps.

The core components of build server are:

1) Cruise Control .NET (CCNET):
Its an open source build and integration engine which can be downloaded freely from http://confluence.public.thoughtworks.org/display/CCNET/DownloadOnce. Once the installation is done CCNET needs to be configured according to your requirements.  The config file can be found at “%SYSTEMDRIVE%Program FilesCruiseControl.NETServerccnet.config”.

2) Batch Scripts:
You might have some tasks to do before the automated build.  In our case, we want to shift the version in every new build. It can be done by altering AssemblyVersion attribute defined in AsseblyInfo.cs file(s). Also you might need to get all the green lights in NUnit testings before you build.  CCNET integrates with NUnit & NAnt nicely for executing test cases and more powerful automated scripts.

Fpr complete reference on the configuration blocks, check out the project home at http://ccnet.sourceforge.net/CCNET/.  If you like to do the same on Linux, you might wanna check out luntbuild