2010年2月3日 星期三

要開發 VS2010,最好記憶體多多

Visual Studio 2010 預計在 2010/04/12 發佈正式版。
其中最引我注意的,是開發機器硬體等級需求大幅提高。
據可靠消息,最好有 3GB 的記憶體,跑起來才會順。

2010年1月27日 星期三

自動翻譯的笑話

今天碰到的。

http://support.microsoft.com/kb/943847/zh-tw 是中文版,內容說該檔案有毒。救命啊!微軟的網頁說自己的檔案有毒?

image

http://support.microsoft.com/kb/943847/en-us 是英文版,看一下原來的字句。
image

原來是微軟的自動翻譯錯了。竟然翻譯完全相反了。

Google 的翻譯可正確多了。

image

還是看原文的比較快。

使用 VSS (Volumn Shadow Copy Service) 備份 VM

在使用 Hyper-V 之前,最好先知道 VM 如何進行備份。如果沒有擬好策略,將來VM 要災難復原時,就求助無門了。

備份的方法分成四種,其中一個方法是使用 VSS (Volumn Shadow Copy Service) 備份 VM。也就是今天要介紹給大家的。

安裝 Windows Backup Features

目的,是在 Windows Backup 時,也能備份 VM。但,VM 在執行中時,VM 的檔案,如 *.vhd 是被 locked 住的,因此 windows 要如何進行備份呢?

首先,在 Hyper-V Host 上以 admin 權限執行 cmd,輸入

vssadmin list writers

結果會列出在該台機器上支援的 writer,也就是使用 vss 備份時可以不受限於lock 的方法。找到 Microsoft Hyper-V VSS Writer 的 Writer Id。如下資訊。
Writer name: 'Microsoft Hyper-V VSS Writer'
Writer Id: {66841cd4-6ded-4f4b-8f17-fd23f8ddc3de}
Writer Instance Id: {84261b8d-c19b-42f7-8463-c540d606495b}
State: [1] Stable
Last error: No error

寫機碼

沒錯,要自訂機碼。原來 Hyper-V VSS Writer 並不是預設啟用的。使用 regedit,在 HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion 下新增一個名為 WindowsServerBackup 的 key。於 WindowsServerBackup 下再新增一個名為 Application Support 的 key。再於其下新增一個上個步驟的 Writer Id 的key,也就是 66841cd4-6ded-4f4b-8f17-fd23f8ddc3de。最後,再於其下新增一個 string value,名為 Application Identifier,值為 Hyper-V VSS Writer。

寫起來很複雜,其實就如下圖所示。

image

進行 Windows Server Backup

在 Administrative tools 下可找到 Windows Server Backup。這就是一般進行備份的工具。在此不多做介紹,坊間的書都有介紹。

附註說明的是,使用 Windows Server 2008 執行 Windows Server Backup,只能進行邏輯磁碟的備份,如 c, d, e 磁碟,並沒有辦法進行單一檔案的備份。因此,需要使用第三方軟體(也就是另外花$$買)。在 Windows Server 2008 R2,也增強了這方面的功能。

2010年1月22日 星期五

Cannot convert lambda expression to type 'string' because it is not a delegate type

開發程式程,當然預設使用 Linq 的語法。發現了一個錯誤,訊息如下
Error 234 Cannot convert lambda expression to type 'string' because it is not a delegate type
錯誤出現在下方的 linq 語法上

 

var ctx = new ProjectServerEntities();
string email = (from i in ctx.Resources
       where i.NAME == displayName
       select i.Email).SingleOrDefault();

怎麼看都沒問題啊!

原來是少了 using System.Linq;

天啊!誰看的懂哪。

2010年1月19日 星期二

VS2010 預計 April 12 上市

如題。雖然已有 delay,但也只是 delay 一個月而已。

另外,也預計二月時出 RC 版。

看來春節也不得閒。

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:建立多專案的範本
範例下載

Share with Facebook