顯示具有 VS2008 標籤的文章。 顯示所有文章
顯示具有 VS2008 標籤的文章。 顯示所有文章

2013年9月4日 星期三

AnyCPU, x86, x64, ARM

今天終於稍微了解了 AnyCPU的用意了。特地記錄下來,避免記憶體 reset 的老問題。

Visual Studio.NET 2003

在這個年代之前,並沒有64位元的 CPU(應該說不普及)。此時 Visual Studio 並不提供 CPU 相關的選項。回想一下,這是多麼美好的事。

Visual Studio 2005,2008

也才再過個兩年,64 位元的 CPU 開始在伺服器上普遍了起來。到了2007年前後,連桌上電腦也愈來愈普及。為了面對問題,微軟 Visual Studio 2005 開始有了 AnyCPU 的平台目標選項。

image

那到底使用 AnyCPU 的 assembly 會如何的被地行呢?此時的決策是非常簡單,而且看來也十分有效:

當 assembly 使用的是 AnyCPU 平台目標時,且

  • 執行在 32 位元Windows作業系統時,會以 32 位元的方式執行,IL 會被編譯成 x86 的 native code。
  • 執行在 64 位元Windows作業系統時,會以 64 位元的方式執行,IL 會被編譯成 x64 的 native code。

問題

由於目前的64位元電腦 (Windows) 都是32位元相容的,因此一個 assembly 在64位元的電腦只能以 64 位元的方式來執行,恐怕不妙。我常碰到的例子是 Oracle Client。

當我們的存取 Oracle 資料庫的程式以 AnyCPU 編譯後,執行在 64 位元的電腦,會以 64 位元的方式來執行。但如果安裝的 Oracle Client 是32位元的呢?32 位元的 Oracle Client 載入 64 位元的程式,就會拋出 System.BadImageFormatException 例外。

Visual Studio 2010

上面的問題持續了3~4年,到底 32 位元與 64位元應用程式好壞也漸漸地有了較明確的選擇。

除非明確地需要大量的記憶體,或者需要長整數,否則32位元就已經足夠。64位元的效能還會有一些些下降。

因此,在 Visual Studio 2010,建立新專案時,Target Platform(平台目標) 預設就是 x86。
但是,AnyCPU 的assembly 執行方式仍然沒有變。

Visual Studio 2012

到了 Visual Studio 2012又有變化。這個時間點,正巧 ARM CPU (也是32bits)也要進來,因此 32bit === x86 時代正式終結。

Visual Studio 2012 建立新專案後的預設選項為 AnyCPU32BitPreferred。
注意一下, Prefer 32-bit 被 disable 掉,是指不能修改,但在.NET Framework 4.5 就可以設定了!

image

當 assembly 使用的是 AnyCPU 平台目標時,且

  • 執行在 32 位元Windows作業系統時,會以 32 位元的方式執行,IL 會被編譯成 x86 的 native code。
  • 執行在 64 位元Windows作業系統時,會以 64 位元的方式執行,IL 會被編譯成 x64 的 native code。
  • 執行在 ARM Windows作業系統時,會以 32 位元的方式執行,IL 會被編譯成 ARM 的 native code。

因此,Perfer 32-bit 可以說是特地為 ARM 設計的。

結論

AnyCPU 從一開始,到目前其定義仍然沒有變動過。也就是「碰到ppp的作業系統,就執行成ppp的程式」,其中 ppp 可以是 32bit, 64bit, ARM。

改變的是「建立專案時的預設選項」。

也因此,上述 Oracle Client 的問題還是很可能會發生。建議儘可能地明確地指定平台目標(Target Platform),以減少不確定性。

2011年8月18日 星期四

在 Windows 7 上開發 Windows Mobile 6.1 安裝步驟

最近因專案的緣故,需要開發 Windows Mobile 6.1 的應用程式。

這是我第一個 Mobile 的應用程式,沒想到必須使用舊的系統來開發。(心聲:真的有點老舊的系統,好想直接開發 WM 7,就可以使用 Silverlight 了)

無論如何,Visual Studio 2010 是無法開發這麼老舊的系統了。因此必須回頭灌 VS2008,沒想到,過程有點辛苦。

首先,我用了 Hyper-V 上的虛擬機 (Windows 2003)來開發,沒想到 Windows Mobile Emulator 無法模擬 PDA 上的網路。想了一下,在虛擬機上再虛擬一個網路,果然有些不對勁。

以下,是我在實體機(Windows 7)上的安裝步驟。

  1. 安裝 Visual Studio 2008
  2. 安裝 Team Explorer 2008
  3. 安裝 Visual Studio 2008 Service Pack 1
  4. 安裝 VSTS 2008 相容 2010 套件(2~4 的步驟是為了使用 TFS 2010 )
  5. 安裝 drvupdate-amd64.exe (Windows Mobile Device Center for Vista)
  6. 安裝 Windows Mobile 6 Professional SDK Refresh.msi
  7. 安裝 Windows Mobile 6 Professional Images (CHT).msi
  8. 由於我已經安裝了 Windows Virtual PC,而VS2008 上的PDA需要安裝舊版的 Virual PC 2007。為了解決兩個版本不能同時安裝的問題,見 http://www.brianpeek.com/post/2009/05/02/windows-virtual-pc-and-the-microsoft-device-emulator.aspx

2010年7月18日 星期日

對 .NET Framweork 進行除錯

有時候,我們程式寫著寫著效能不佳,就可能怪起微軟的 .NET Framework了。微軟看來也怕了這一點,趕緊公佈 .NET Framework 的 Source Code,並且還可以在 Visual Studio 2010 進行除錯。

以下就介紹如何設定。

步驟1:下載

請到 http://referencesource.microsoft.com/netframework.aspx , 下載 .NET 4 的 Source Code 及 Symbol.

image

步驟2:安裝

下載回來的是 Net_4.msi。安裝時設定指定路徑為個人檔案的路徑,如 C:\Users\yourname\Documents\RefSrc,我就用了這一個路徑。

步驟3:設定

設定的步驟比較多。打開 Visual Studio 2010,執行功能表Tools/Options,選擇 General 頁。
不要勾選 "Enable Just My Code (Managed only)"。
勾選 "Enable .NET Framework source stepping"。
勾選 "Enable source server support"。
不要勾選"Require source files to exactly match the original version"


如下圖:

image

再選到 Symbols 路徑

按新增目錄,輸入 C:\Users\yourname\Documents\RefSrc\symbols
在 Cache symbols in this directory 中輸入 C:\Users\yourname\Documents\RefSrc\symbols\SymbolCache

完成後按確定。
image

大功告成。之後您就可以使用 F11來除錯了。

2010年5月17日 星期一

連接 Visual Studio 2008 到 TFS 2010 的詳細步驟

之前提到了 舊版 Visual Studio (2005, 2008) 連接到 TFS 2010。其中內容太過簡潔。下面則是 VS2008 完整的安裝步驟,順序不能錯誤,否則無法連接到 TFS2010。

安裝

  1. 安裝 Visual Studio 2008
  2. 安裝 Team Explorer 2008
  3. 安裝 Visual Studio 2008 Service Pack 1
  4. 安裝 VSTS 2008 相容 2010 套件

設定

接下來,就可以設定 Team Foundation Server。使用 Tools \  Connect to Team Foundation server…

image

輸入時,必須輸入 http://tfsServer:8080/tfs/CollectionName 如下的測試範例

image

如果沒有照順序安裝,則上一步輸入 Url 時,Port number 不會變成 disabled 的狀態。

2010年5月14日 星期五

舊版 Visual Studio (2005, 2008) 連接到 TFS 2010

在 Visual Studio 2010 中,Team Explorer 已經是內建的了。換句話來說, Visual Studio 2001 不必安裝其他元件,就可以連接到 TFS 2010。

然而,舊版的 Visual Studio,如  2005 及 2008 要使用 TFS 2010,該怎麼辦呢?

簡單地說,就是安裝新的軟體,以支援連接到 TFS 2010.

VSTS 2008 相容 2010 套件 ( 請升到 SP1 )

http://www.microsoft.com/downloads/details.aspx?displaylang=zh-tw&FamilyID=cf13ea45-d17b-4edc-8e6c-6c5b208ec54d

VSTS 2005 相容 2010 套件 ( 請升到 SP1 )

http://www.microsoft.com/downloads/details.aspx?displaylang=zh-tw&FamilyID=22215e4c-af6f-4e2f-96df-20e94d762689

後記1

[20100518]雖然微軟出了這兩個套件,但我只試成功了 Visual Studio 2008的部份,見連接 Visual Studio 2008 到 TFS 2010 的詳細步驟。Visual Studio 2005 想要比照辦理,卻試不出來。不知道微軟是不是有測過呢?

後記2

[20100519]試出來了,過程見 連接 Visual Studio 2005 到 TFS 2010 的詳細步驟。我又錯怪了微軟。

2010年5月3日 星期一

CodeRushExpress for VS2010

可到這裡下載

http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/ 

經我測試,仍然有些不穩定。例如,如果有安裝 Visual Studio 2008, 並不會自動將CodeRushExpress 9.0 版自動移除,造成一登入 Visual Studio 2008 會出現一堆的 addin not found 的錯誤訊息。

今天早上在 VS2010 上Rename  一個 class 名稱時,也出現了 Critical 的錯誤,導致 VS2010 要求重起。

但,大致來說還能接受,還是安裝了,並不移除。

[20100518]

新版本似乎比較穩定一些

CodeRushXpress-10.1.2.exe

[20100524]

取得最新的版本

2010年1月13日 星期三

自訂 Visual Studio Project Template (3)

繼前兩篇自訂 Visual Studio Project Template (1)自訂 Visual Studio Project Template (2)後,這次來到最後一篇。

到了第(2)篇後,已經可以新增多個專案。缺點是,使用 Project template 時,無法指定專案的名稱,namespace 等問題。最後一篇就來說明如何解決。

(1)新增一個類別庫 (Class library) 並命名為CCWebSolutionTemplateWizard。
(2)在類別庫中新增一個Windows Form 並命名為 VariablesForm

image
(3)修改VariablesForm,拖曳成如下的畫面
image
(4)修改 VariablesForm.cs 如下方的程式

using System;
using System.Windows.Forms;

namespace CCWebSolutionTemplateWizard
{
  public partial class VariablesForm : Form
  {
    public VariablesForm()
    {
      InitializeComponent();
    }

    private void btnOk_Click(object sender, EventArgs e)
    {
      this.Hide();
    }

    public string NameSpace
    {
      get { return txtNamespace.Text; }
    }

    public string WebSiteName
    {
      get { return txtWebSiteName.Text; }
    }

    public string BusinessComponentsName
    {
      get { return txtBusinessComponents.Text; }
    }
  }
}

(5) 新增參考 Microsoft.VisualStudio.TemplateWizardInterface, EnvDTE
(6) 新增一個 class 並命名為 CCWizard.cs ,內容如下

using System;
using System.Collections.Generic;

namespace CCWebSolutionTemplateWizard
{
  class CCWizard : Microsoft.VisualStudio.TemplateWizard.IWizard
  {
    public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem) { }

    public void ProjectFinishedGenerating(EnvDTE.Project project) { }

    public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem) { }

    public void RunFinished() { }

    public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, Microsoft.VisualStudio.TemplateWizard.WizardRunKind runKind, object[] customParams)
    {
      var form = new VariablesForm();
      try
      {
        form.ShowDialog();
        replacementsDictionary.Add("$NameSpace$", form.NameSpace);
        replacementsDictionary.Add("$WebSiteName$", form.WebSiteName);
        replacementsDictionary.Add("$BusinessComponentsName$", form.BusinessComponentsName);
      }
      catch (Exception ex)
      {
        System.Windows.Forms.MessageBox.Show(ex.ToString());
      }
    }

    public bool ShouldAddProjectItem(string filePath) { return true; }
  }
}
(7) 為了在新增專案時能使用自訂的Wizard,必須讓CCWebSolutionTemplateWizard類別庫註冊到 GAC中,因此必須有 strong name。 請到CCWebSolutionTemplateWizard的「property」內Siging頁設一個 key吧,如下圖。再 build 一次,讓CCWebSolutionTemplateWizard.dll 具有 strong name
image
(8)將CCWebSolutionTemplateWizard.dll 註冊到 GAC中。簡單的方法是使用檔案總管將 dll 拖曳到 c:\windows\assembly 中。或者使用下面語法
gacutil /i CCWebSolutionTemplateWizard.dll

(9) 在上一篇中,Solution folder 中的 MyTemplate.vstemplate 中,WebSiteName 是 hard code 的。這一次就必須在使用自訂的 project template 時,使用我們自訂的 wizard,指定 Web Site Name。於是,我們修改 MyTemplate.vstemplate 如下

<?xml version="1.0"?>
<VSTemplate xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Version="2.0.0" Type="ProjectGroup">
    <TemplateData>
        <Name>CCWebWebSolution</Name>
        <Description>CC web solution starter template</Description>
        <Icon>__TemplateIcon.ico</Icon>
        <ProjectType>CSharp</ProjectType>
        <ProjectSubType>Web</ProjectSubType>
    </TemplateData>
    <TemplateContent>
        <ProjectCollection>
            <ProjectTemplateLink ProjectName="$WebSiteName$">CCWebApplication\MyTemplate.vstemplate</ProjectTemplateLink>
            <ProjectTemplateLink ProjectName="$BusinessComponentsName$">BusinessComponents\MyTemplate.vstemplate</ProjectTemplateLink>
        </ProjectCollection>
    </TemplateContent>
  <WizardExtension>
    <Assembly>CCWebSolutionTemplateWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=182050d61235b264, processorArchitecture=MSIL</Assembly>
    <FullClassName>CCWebSolutionTemplateWizard.CCWizard</FullClassName>
  </WizardExtension>
</VSTemplate>
其中 <Assmebly>…</Assembly>的值,可由 gacutil /l CCWebSolutionTemplateWizard 指令取得。
(10) 再由上一篇中介紹的,將檔案壓縮成 zip 檔並放到 ProjectTemplate中。
(11) 大功告成了!新增專案時,就會使用我們的 wizard 了。可替的文字,可以是在 vstemplate檔中,也可以是 cs 的原始程式碼。最後的範例可下載

2010年1月12日 星期二

自訂 Visual Studio Project Template (2)

繼上一個自訂 Visual Studio Project Template (1),接下來說明的是多專案的 template.

通常,一個 project template 只會產生一個專案。但最適當的結構,卻是一個解決方案(Solution),至少分個三層(3-layer)。以下僅以兩個專案的 solution 來作為範例。

如上一次的例子,這一次多了一個 BusinessComponents的 class library(類別庫)。而 WebApplication4會 reference 到 BusinessComponents

image

分別將 BusinessComponents 及 WebApplication4以上一次的方法匯出 project template。解壓縮zip檔後,在 My Exported template 目錄下,增加一個 MyTemplate.vstemplate 及 __TemplateIcon.ico 檔。結構如下圖(已將 zip 檔刪除掉)

image

檔案結構看來如下

Directory \Documents\Visual Studio 2008\My Exported Templates
2010/01/12  上午 11:38    <DIR>          CCBusinessComponents
2010/01/12  上午 11:38    <DIR>          CCWebApplication
2010/01/12  下午 03:45               714 MyTemplate.vstemplate
2003/05/06  下午 03:49             5,430 __TemplateIcon.ico

Directory \Documents\Visual Studio 2008\My Exported Templates\BusinessComponents
2010/01/12  上午 11:36               235 Class1.cs
2010/01/12  上午 11:36             2,510 ClassLibrary1.csproj
2010/01/12  上午 11:36             1,128 MyTemplate.vstemplate
2010/01/12  上午 11:36            10,134 __TemplateIcon.ico

Directory \Documents\Visual Studio 2008\My Exported Templates\CCWebApplication
2010/01/12  上午 11:36               452 Default.aspx
2010/01/12  上午 11:36               323 Default.aspx.cs
2010/01/12  上午 11:36               772 Default.aspx.designer.cs
2010/01/12  上午 11:36             1,854 MyTemplate.vstemplate
2010/01/12  上午 11:36               749 Vender.aspx
2010/01/12  上午 11:36               468 Vender.aspx.cs
2010/01/12  上午 11:36             1,141 Vender.aspx.designer.cs
2010/01/12  上午 11:36             7,868 Web.config
2010/01/12  上午 11:36             4,755 WebApplication4.csproj
2010/01/12  上午 11:36            10,134 __TemplateIcon.ico

簡單來說,就是 solution folder + 下面的 project folder。每個 folder 都要有 .vstemplate 檔案。project folder 可以使用 project template 的 zip 來解壓縮即可。而 solution folder 下的 .vstemplate 就必須就必須自行編輯。如下範例

<?xml version="1.0"?>
<VSTemplate xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Version="2.0.0" Type="ProjectGroup">
    <TemplateData>
        <Name>CCWebWebSolution</Name>
        <Description>CC web solution starter template</Description>
        <Icon>__TemplateIcon.ico</Icon>
        <ProjectType>CSharp</ProjectType>
        <ProjectSubType>Web</ProjectSubType>
    </TemplateData>
    <TemplateContent>
        <ProjectCollection>
            <ProjectTemplateLink ProjectName="WebSite">CCWebApplication\MyTemplate.vstemplate</ProjectTemplateLink>
            <ProjectTemplateLink ProjectName="BusinessComponents">BusinessComponents\MyTemplate.vstemplate</ProjectTemplateLink>
        </ProjectCollection>
    </TemplateContent>
</VSTemplate>

重點放在結構上。不難的。
在檔案總管上選取所有檔案,傳送到壓縮檔並取名為 CCWebSolutionTemplate.zip,並複製到 \Documents\Visual Studio 2008\Templates\ProjectTemplates 下。

image

重新啟動 Visual Studio 2008, 新增專案時,就可以看到我們的 template 了

image

可參考 msdn 的說明 HOW TO:建立多專案的範本
範例下載

Visual Studio 執行 Web Test Record 時,在 IE8 下會出現無法錄製網頁的問題

困擾了一個下午。
同事使用Visual Studio 2008,在錄製 Web Test 時(Windows 7 + IE 8),發現某一些網頁無法被錄製下來。

這個網頁是使用簡易的 window.open 來開啟的。已知 window.showModalDialog 很可能無法錄製的 case 並不是這次的原因。

查了許久,實在找不到原因,而且我的電腦(Win7 + IE8)也有相同的問題。就懷疑是作業系統或 IE8 的問題。因此開了一台 Windows Server 2003 (IE6) 來看,是可以錄製該「消失」的網頁。

原來, IE8為了讓瀏覽器執行「看起來」更穩定,採用了多Process 的運作。也就是不同的網頁,很可能執行在不同的 Process 下。這與 IE7 以前的運作模式大為不同。IE7 的某一個網頁掛了(例如受攻擊),就會連帶的使其他執行在同一 Process 的網頁一起掛掉,而且連救回來的時間也沒有。而IE8 一開始來就使用了2個 processes,其中一個就負責「即時救援」的任務,一旦網頁發現掛掉,立刻救援。使用者只覺得網頁一下子就掛掉,但馬上又被還原了,感覺好多了。

IE 8 這樣的新模式,固然對瀏覽器使用者非常友善,但對於 VS2008 這個在2007年底發佈的開發工具是意想不到的行為。Web Test Recorder 9.0 只會針對同一個 process 進行錄製,因此 window.open 造成新的 process ,是無法錄製的。

解法

這個問題,是要讓新的 tab 開啟時,不要新增一個 process。方式是在使用 regedit。如下步驟

  1. 執行 RegEdit
  2. 瀏覽到 HKEY_CURRENT_USER\SOFTWARE\Microsoft\ Internet Explorer\Main
  3. 新增一個 dword,命名為TabProcGrowth,並設其值為 0
  4. 關閉所有 IE 視窗。下一次執行時就可以讀取到新的設定了。
  5. Visual Studio 2008 執行 Web Test 開始執行錄製。

完工。

     

2010年1月5日 星期二

WebTest 的Form Parameter 與日期有關

以Visual Studio 2008 進行網頁測試(WebTest)錄製後,通常不會有問題。但過一段時間後,再回來執行 WebTest,通常會發生一些錯誤。其中一部份與時間有關。以下是一個例子。

國外旅遊查詢網頁,出發日期只能選最近的10天來查詢。

image

錄製 WebTest後,可看到送出的日期為 2010/01/06 日。

image

過了幾天後,到了 2010/01/07 時,再來跑這個 WebTest,就會發生錯誤。

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation

原因是到了2010/01/07 時,dropdownlist 只會產生 01/07 ~ 01/16 這10天的資料,並不會有 2010/01/06 的選項。預設,asp.net 2.0 會檢查這個問題,稱為 event validation。

解決方法呢?有兩種,一種是產生 Coded Test, 可使用工具列中的 Generate Code 來產生程式碼,再到該程式碼中,改成 DateTime.Today.ToString(“yyyy/MM/dd”),這樣一來,就解決了問題。

image

image

另外一種較佳的解決方法,則必須安裝 codeplexWeb Test PlugIn 1.1。使用方法,可見說明。於是,只需要修改 Form Parameter 為 <%=DateTime.Today.ToString(“yyyy/MM/dd”)%>即可。

image

結論

兩種方法,當然以第二種較為方便,而且有彈性。第一種方法,在轉成 Code 之後,失去了Web Test 的UI,無法有直覺性地了解測試結果,但也因為轉成code 之後,任何的測試目的都可以達成。

2009年11月16日 星期一

無法載入 DLL 'VSCover90': The specified module could not be found. (發生例外狀況於 HRESULT: 0x8007007E)

開發程式後,執行測試。由於沒有耐心等測試跑完,就急著放 assembly 到正式機上了。哪知一執行,網頁竟跑出

無法載入 DLL 'VSCover90': The specified module could not be found. (發生例外狀況於 HRESULT: 0x8007007E)

 

原來,我在執行測試時,也同時收集 code coverage (程式覆蓋率)。為了能收集該資料,Visual Studio 看來有放一些 sampling 的程式到 assembly上。
而 Visual Studio 正執行測試時,我就急著放 assembly到正式機了。因此會出現這樣的怪事。

解決方法呢?等測試執行完後,再部署 dll 即可。

2009年10月1日 星期四

如何將 Visual Studio Load Test 的結果匯出成報表

Visual Studio 2005 Team System Tester Edition 上,可以執行 LoadTest,但如何將其結果匯出成 Excel,給客戶作為壓力測試報告呢?

這就必須使用 CodePlex 上的開源碼程式 Microsoft Visual Studio Team System Load Test Reporting Sample 了。

解開壓縮檔後, 在 doc 目錄下有 InstallationGuide.doc 說將如何安裝。安裝的過程簡單,只是需要有 Report Service 的概念。

進行壓力測試時,需指定 Load Test Result Store 設定到 Database Server, database 設為 LoadTest.

image

而 LoadTest 的 Run Settings, 也需將 Storage Type 設為 Database,才能將結果寫到資料庫中。

image

執行完壓力測試後,可使用 結果匯出到http://server/VSTSCustomReports/Default.aspx 檢視結果。如下圖。

 

clip_image002

clip_image004

2009年9月11日 星期五

Run Code Metrics 出現錯誤

在使用 VSTS 的 run code metrics 功能時,發生了無法分析的錯誤。

An error occurred while calculating code metrics for target file ‘xxx.dll’  The following error was encountered while reading module 'Microsoft.SharePoint': Could not resolve type: T ObjectModel

這裡有個特殊符號,可不是我隨便打的。整個錯誤訊息 copy 下來就有的。

這該怎麼解呢?還好之前的 SandCastle 的安裝及使用 的經驗,知道這類的程式,一定程度上需要了解 dll 的關聯。我的例子,是我reference了 Sharpoint 的 元件 Microsoft.SharePoint.dll。在我的 project 中,是可以被 compile 並執行的。但是 Microsoft.SharePoint.dll 在執行時期,需要引用 Microsoft.SharePoint.Security.dll。
Microsoft.SharePoint.Security.dll 並不在我引用的範圍中。因此分析出錯了。

要解決這個 issue,只需要手動地再加入 reference Microsoft.SharePoint.Security.dll 即可。

2009年8月13日 星期四

Windows 7 (x64) 上執行 Visual Studio 2008,執行單元測試時,發生 Code coverage collection error

歡天喜地地安裝完 Windows 7 64 位元版後,就跟著安裝一堆開發用的軟體。
還真的不少。已過了三天,還裝不完呢。

在 Visual Studio 2008 上執行之前的單元測試,全掛了!錯誤原因如下:

Code coverage collection error: The Visual Studio performance and coverage logging engine is already running on the computer. Therefore, the test run cannot continue. Close the pending performance or code coverage session and then rerun the tests.

是什麼意思呢?原來不知為何,Visual Studio 的效能程式 VSPerfMon.exe 無法自行關閉,因此也無法再起一個。

碰到這種情形時,請手動到 task manager 上強制 end process 即可。

2009年7月28日 星期二

Microsoft Visual Studio Tools for the Office System Power Tools v1.0.0.0

開發 Office 2007 的程式,如果是以 xml 的方式來處理文件時, 最為麻煩的一件事,是必須先改名為 .zip 檔,然後解壓,然後再觀察 xml 的 layout,relations。
難道沒有一個好的工具嗎?
有的。

Microsoft Visual Studio Tools for the Office System Power Tools v1.0.0.0

安裝完畢後,打開 Visual Studio 2008,之後直接在檔案總管以拖拉方式拖進 Visual Studio後,就會打開如下圖的視窗。

image

觀察一下 document.xml 吧!這是一個 word 文件的入口。Hello.docx 是一個超簡單的 word 2007 文件,裡面只有一句 Hello,及一個註解「This is good start」

image

打開 document.xml,可看到如下的 xml。看到了 Hello 的文字呢。而且,該段文字是包在一段註解 <w:commentRangeStart w:id=”0” /><w:commentRangeEnd w:id=”0” /> 。

image

而 w:id=”0” 的註解,需參考 comments.xml,如下圖。注意到反白的 <w:comment w:id=”0”  ,就是id=0 的註解。

image

這個小小的範例,可以了解MS2007 的 xml 格式是強大且充滿彈性的。了解了 xml 的格式後,就可以對 office  文件進行自動化的文件產生,對於客戶的需求,又多了一分把握。

2009年5月14日 星期四

SandCastle 的安裝及使用

以往在 Visual Studio 2003 時,會使用 NDoc 來製作原始碼的說明文件。但這個 Ndoc已經許久沒出新版啦!
現在則被 SandCastle 所取代!

以下是安裝及使用過程

安裝 SandCastle

這裡 下載後安裝之。SandCastle 是一個 command line 的工具,因此使用起來特別難過。

安裝 Sandcastle Help File Builder

Sandcastle Help File Builder 是一個 Sandcastle 的 UI 輔助工具。這才是我們最常用的。以下簡稱 SHFB

在 C# 原始碼寫說明

這一點不必再說明了吧!如果需要複習,請見 XML Documentation

打開 xml output

專案在 build 時,預設不會寫出 xml document。必須到每個專案下設定 property。如下圖。

image

執行 Sandcastle Help File Builder GUI

執行 SHFB 後,首先在右方的 Project Explorer,選取 Documentation Sources 上按右鍵,點擊 Add Documentation Source….,將 solution 加入。

image

再來設定一些基本的資訊

image

製作

最後,就開始製作啦!(工具列)

image

Add Reference

通常,我們會用一些特別的元件,如 SilverLight 等,或在 GAC 上的 assembly。這些不在 solution 目錄,或 bin 目錄下的元件預設是抓不到說明的。此時,很快地會發生錯誤。如下

MREFBUILDER : error : Unresolved assembly reference: System.Windows (System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e) required by ProjectStatisticsSL
    Last step completed in 00:00:04.6335
-------------------------------

SHFB: Error BE0043: Unexpected error detected in last build step.  See output above for details.

因此,需要在 Project Explorer 上新增 Reference

image 

那要增加什麼呢?看原來的 assembly 資訊。如下圖,將System.Windows對應的 assembly Path 加入即可

image

image

看成果

按一下工具列上的放大鏡。就會看到文件囉!

image

2009年4月26日 星期日

我常用的 C#

ShortCut key

Goto Definition: F12
Find all references: Shift + F12
Definition Stack: Ctrl+Shift+F7, Ctrl+Shift+F8

Next-Windows: F8
Goto Opened File: Ctrl+Tab (Preview)
                          Ctrl+Alt+down

 

Ctrl + .

向前移動一個Word : Ctrl + <-
向後移動一個Word : Ctrl + –>
向前選取一個Word : Ctrl + Shift + <-
向後移動一個Word : Ctrl +Shift +  –>

 

Option 設定

字型 Fixedsys

Projects and Solutions/General => check the option “Track Active Item in Solution Explorer”

2009年4月19日 星期日

Team Foundation Server 2008 實戰

Shoot! 安裝過 Project Server 2003 後,我還以為沒有比這個更難的了。
沒想到 Team Foundation Server 更難!

目前還正克服中。想要在公司內推這套系統,看來還有的試了。

困難1:安裝 Single Server, Windows 2003 Standard, SQL Server 2008 Standard, TFS 2008 Standard

我是按照TFSInstall-SP1-RTM-v080918.chm 去安裝的。注意到光安裝手冊就寫得非常複雜。我之前想所一頁一頁跟著作就不會錯了。於是由最上方照著順序看,照著順序實作。但不管安裝 TFS WorkGroups 版,或 Standard 版,總會在安裝 TFS 時,被「執行系統健康檢查」這一個步驟檢查出「不相容的 SQL Server版本」。花了好多天,最後也請教了微軟的 Terry Lin,才知道必須整合 TFS 2008 SP1 才能這樣安裝。也就是在下圖的反白標示的「安裝 Team Foundation Server 與 Service Pack 1」。

因此,學到了一個教訓:一次看完所有的安裝手冊,再開始動手,是比較保險(但不 make sense)的方法。

image

終於安裝「成功」了。因為沒有出現錯誤訊息。

困難2:

使用 Visual Studio 2008 Team Suite,想要連到 TFS 2008。就必須將 Source Control 設定成 TFS。

image

我在某一個 Solution 上按右鑵,執行「Add Solution to Source Control」,馬上收到了另一個問題

imageimage

另外,在建立一個 Team Project 時,最後也出現了如下圖的錯誤

image

找了一篇文件 http://billwg.blogspot.com/2009/03/troubleshooting-team-project-creation.html,說明是TF30207: Initialization for plugin "Microsoft.ProjectCreationWizard.Reporting" failed的問題。也是無解。

另外找了一篇 http://www.thejoyofcode.com/TF03177_Team_Project_Creation_Failed.aspx , 這篇說明在安裝 Team Explorer 後,Visual Studio 2008 應該重裝 SP1。天啊!又花掉了我1小時的時間重新安裝 VS2008 SP1。不過,這的確解決了我的問題。

問題3:不允許不能 Compile 的程式碼 Chek-in

這應該是必備的吧!否則使用 TFS 作什麼呢?因此作了了如下的設定。

image

寫了一個不能 compile 的程式。天啊!竟然可以check-in.

原來,這個功能必須依靠 Code Analysis,即使 Code Analysis 沒有勾選任何的 rules.

image

然後,在 Solution Explorer 上選到 solution, 按右鍵執行 「Code Analysis Settings for Solution/Merge with Check-in Policy」

image

終於出現了我要的結果:不能 compile ,就不能 check-in

image

(20090420 後註:原來我想錯了,真正的 builds check-in policy 使用方了, 可見 How Do I: Use the Builds Check-in Policy, 設定上還是有些麻煩)

2009年4月13日 星期一

Visual Studio Team Test Quick Reference Guide 1.0

A quick reference for users of the Team Testing features of Visual Studio Team System 2008

http://vstt2008qrg.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=25504

這裡主要說明了 How to do xxxxx 的事情。
可惜並沒有一些開發 tests 的規則說明。例如 TDD Pattern: Do not cross boundaries , Unit Test Boundaries 的解說。

2009年4月3日 星期五

CAT.NET 初用

一直被問到,SQL Injection 及 XSS attact 如何防範?
雖然知道,但要一個個解釋,總是一個大工程,最好有一個工具,能幫忙檢查同事的程式。檢查出來後再來解釋為何「這段程式容易被攻擊」。

CAT.NET 正好是我所需要的。畢竟我都是使用 Visual Studio 來開發啊!

安裝後,打開 Visual Studio, 在功能表 tools 下會多出一個 CAT.NET Code Analysis.

image

執行後,按下方圈選的圖示,以進行設定

image

為何要進行設定呢?以我的solution為例,共23個專案。CAT.NET 是分析專案輸出的 Assembly 來進行檢查的。它甚至會把測試專案也拿來分析。
因此,設定太多專案的話,目前很容易 Out of memory 的。
 image

按Targets頁,可以看到多個Assembly輸出 。使用 Add/Remove  來移除不甚重要的 assembly。如果不信邪,就會發生如下的訊息。

Code analysis output report: C:\MySolutionFolder\MicrosoftACECodeAnalysisReport.xml
Executing code analysis...
Error: Exception of type 'System.OutOfMemoryException' was thrown.

進行分析後,就會出現一堆的 Issue。天啊,怎麼會這麼多?

image

雙擊 Issue 後,Source Code Editor 就會出現有問題的那一行原始碼。由於我們的程式,都會進行重構/模組化,因此,以人工的方式進行分析是相當辛苦的。
CAT.NET 會將這個 Issue 的呼叫過程,由Data Flow Start 直到 Data Flow End ,整個歷程輸出到右方區塊。(如下圖)

image

因此,如上例,該段程式經過了9個 function call,才完成了可能被攻擊的漏洞。

天啊!再資深的天才,也沒有辦法以人工的方式將所有的漏洞一個個找出來的。

目前只是 CTP 版,因此效能上比較差,記憶體也用的很兇。

未來應該會整合到 Visual Studio 的 Code Analysis 中

Share with Facebook