2010-05-07

我要獨立的 MSDN Helper for VS2010

VS2010 有很多很棒的新玩意兒, 但是最讓我不習慣的就是那本 MSDN Library 竟然變成 Browser 版了, 而且沒有索引, 實在太難用了, 上網查了一下, 果然有人有相同的困擾, 參考網址: http://bit.ly/aRs7sY

簡單記錄一下步驟:

  1. 確定 Help 安裝在 local
  2. 下載並安裝 Help Viewer Power Tool extension for VS2010(也可以從 VS2010 內的 Extension Manager 安裝)
  3. 這樣就可以了, 現在的 Help 就會多兩個 tab, search & index, 可是還是 in-browser 的, 如果想要 standalone 的呈現方式, 那就要再多兩個步驟
    1. 先確定工具列中的 Help Library Agent 己經關閉
    2. 以管理者權限執行 c:\program files\microsoft help viewer\v1.0\hlpvwpt.exe standaloneviewer=true

最後的結果如下圖, 而且還可以把圖示 pin 到 taskbar 上!

image

keywords: help, vs2010, extension

2010-04-13

Google Chrome 4 (42898) 最小字型設定

用 Google Chrome 瀏覽 codeplex 網站的討論區都會看到像以下的畫面

image

左側排版有點亂掉, 打開 Chrome 的開發人員控制台試著調了一下 css, 發現字型設到某一個大小之後, 就不能再更小了, 所以才會導致版面跑掉, google 了一下解決方法, 如下:

  1. 先關閉所有的 Google Chrome 程序
  2. 找到 %USERPROFILE%\AppData\Local\Google\Chrome\User Data\Default 底下的 Preferences 這個檔案, 用記事本打開它
  3. 找到 "webkit" 底下的 "webprefs": {} 區段, 加入下面兩行就可以了
    "minimum_font_size": 9,
    "minimum_logical_font_size": 9
    image

minimum_font_size 是設定絕對單位的最小值, 如 px 等, minimum_logical_font_size 是設定相對單位的最小值, 如 em 等.

重開 Chrome 之後再次瀏覽 codeplex, 就會看到正確的版面了.

image

keywords: chrome, mininum font size

2010-03-23

throw 和 throw ex 的差別

看看你是否也寫過以下的 code:

    try
    {
        
// do something
    }
    
catch (Exception ex)
    {
        
// do something
        
throw ex;
    }
注意 throw ex; 這一行, 它的用意只是把原來的 exception 再次的抛出去, 看起來好像沒什麼問題, 但是其實這種寫法已經改變的原來的 StackTrace, 一般的用法如果想要抛出原來的例外應該是只要寫 throw; 就可以了, 這樣才能保留原有的 StackTrace, 以下範例可以比較清楚的看出這個問題:


    void Main() {
        
try {
            OuterCall();
        }
catch (Exception e) {
            Console.WriteLine(e.StackTrace);
        }
    }

    
static void InnerCall() {
        
throw new Exception();
    }

    
static void OuterCall() {
        
try    {
            InnerCall();
        }
catch (Exception ex) {
            
throw; // 試著將這行改成 throw ex;
        }
    }

第一種(throw;)執行結果為
at MyTest.InnerCall() in ...
at MyTest.OuterCall() in ...
at MyTest.Main() in ...


第二種(throw ex;)執行結果為






at MyTest.OuterCall() in ...
at MyTest.Main() in ...

差別應該看得很清楚了吧!





keywords: throw, exception, stacktrace

2010-03-13

26G (260億) 相素巴黎美景

LINQ to SQL changes in .NET 4.0



Change list

Performance

  • Query plans are reused more often by specifically defining text parameter lengths (when connecting to SQL 2005 or later)
  • Identity cache lookups for primary key with single result now includes query.Where(predicate).Single/SingleOrDefault/First/FirstOrDefault
  • Reduced query execution overhead when DataLoadOptions specified (cache lookup considers DataLoadOptions value equivalency)

Usability

  • ITable interface for additional mocking possibilities
  • Contains with enums automatically casts to int or string depending on column type
  • Associations can now specify non-primary-key columns on the other end of the association for updates
  • Support list initialization syntax for queries
  • LinqDataSource now supports inherited entities
  • LinqDataSource support for ASP.NET query extenders added

Query stability

  • Contains now detects self-referencing IQueryable and doesn’t cause a stack overflow
  • Skip(0) no longer prevents eager loading
  • GetCommand operates within SQL Compact transactions
  • Exposing Link on a property/field is detected and reported correctly
  • Compiled queries now correctly detect a change in mapping source and throw
  • String.StartsWith, EndsWith and Contains now correctly handles ~ in the search string (regular & compiled queries)
  • Now detects multiple active result sets (MARS) better
  • Associations are properly created between entities when using eager loading with Table-Valued Functions (TVFs)
  • Queries that contain sub-queries with scalar projections now work better

Update stability

  • SubmitChanges no longer silently consumes transaction rollback exceptions
  • SubmitChanges deals with timestamps in a change conflict scenario properly
  • IsDbGenerated now honors renamed properties that don’t match underlying column name
  • Server-generated columns and SQL replication/triggers now work instead of throwing SQL exception
  • Improved binding support with the MVC model binder

General stability

  • Binary types equate correctly after deserialization
  • EntitySet.ListChanged fired when adding items to an unloaded entity set
  • Dispose our connections upon context disposal (ones passed in are untouched)

Database  control

  • DeleteDatabase no longer fails with case-sensitive database servers

SQL Metal

  • Foreign key property setter now checks all affected associations not just the first
  • Improved error handling when primary key type not supported
  • Now skips stored procedures containing table-valued parameters instead of aborting process
  • Can now be used against connections that use AttachDbFilename syntax
  • No longer crashes when unexpected data types are encountered

LINQ to SQL class designer

  • Now handles a single anonymously named column in SQL result set
  • Improved error message for associations to nullable unique columns
  • No longer fails when using clauses are added to the partial user class
  • VarChar(1) now correctly maps to string and not char
  • Decimal precision and scale are now emitted correctly in the DbType attributes for stored procedures & computed columns
  • Foreign key changes will be picked up when bringing tables back into the designer without a restart
  • Can edit the return value type of unidentified stored procedure types
  • Stored procedure generated classes do not localize the word “Result” in the class name
  • Opening a DBML file no longer causes it to be checked out of source control
  • Changing a FK for a table and re-dragging it to the designer surface will show new FK’s

Code generation (SQL Metal + LINQ to SQL class designer)

  • Stored procedures using original values now compiles when the entity and context namespaces differ
  • Virtual internal now generates correct syntax
  • Mapping attributes are now fully qualified to prevent conflicts with user types
  • KnownTypeAttributes are now emitted for DataContractSerializer with inheritance
  • Delay-loaded foreign keys now have the correct, compilable, code generated
  • Using stored procedures with concurrency no longer gets confused if entities in different namespace to context
  • ForeignKeyReferenceAlreadyHasValueException is now thrown if any association is loaded not just the first

Potentially breaking changes

We worked very hard to avoid breaking changes but of course any potential bug fix is a breaking change if your application was depending on the wrong behavior. The ones I specifically want to call out are:

Skip(0) is no longer a no-op

The special-casing of 0 for Skip to be a no-op was causing some subtle issues such as eager loading to fail and we took the decision to stop special casing this. This means if you had syntax that was invalid for a Skip greater than 0 it will now also be invalid for skip with a 0. This makes more sense and means your app would break on the first page now instead of subtlety breaking on the second page. Fail fast :)

ForeignKeyReferenceAlreadyHasValue exception

If you are getting this exception where you weren’t previously it means you have an underlying foreign key with multiple associations based on it and you are trying to change the underlying foreign key even though we have associations loaded.Best thing to do here is to set the associations themselves and if you can’t do that make sure they aren’t loaded when you want to set the foreign key to avoid inconsistencies.

2010-03-02

在 Ubuntu Apache 下架設 Mercurial Server 筆記

  1. 確定安裝好 apache2
    dpkg –l  | grep apache2, 若沒裝則用 sudo apt-get install apache2 安裝
  2. 變身成 root
    $ sudo su
  3. 建立 hg repositories 和 cgi-bin 的存放路徑
    # mkdir /var/hg
    # mkdir /var/hg/cgi-bin
    # mkdir /var/hg/repos
  4. 複製 hgwebdir.cgi 到指定位置, 並更改權限為可執行
    # cp /usr/share/doc/mercurial/examples/hgwebdir.cgi /var/hg/cgi-bin/
    # chmod a+x /var/hg/cgi-bin/hgwebdir.cgi
  5. 建立 /var/hg/cgi-bin/hgweb.config, 寫入以下內容
    [collections]
    /var/hg/repos/ = /var/hg/repos/
  6. 設定 apache
    1. 建立 /etc/apache2/sites-available/hg, 寫入以下資訊, 一行而已
      ScriptAlias /hg "/var/hg/cgi-bin/hgwebdir.cgi"
    2. 啟用該設定
      # a2ensite hg
      # /etc/init.d/apache2 reload
  7. 開啟瀏覽器, 輸入 http://localhost/hg 應該就可以看到 mercurial 的畫面

若要 allow push, 有兩個設定要調整

  1. 確定 apache 有 /var/hg/repos/ 的寫入權限
    # chown –R www-data.www-data /var/hg/repos/
  2. 在 allow push 的 repository 的 .hg 目錄下編輯 hgrc 檔
    [web]
    push_ssl = false
    allow_push = *

想要更進一步控制權限, 要自行設定 .htaccess!

keywords: mercurial, hg, apache, ubuntu, scm

參考資料: Serving Mercurial using Apache on Ubuntu

2009-04-22

ui.datepicker.js 會破壞 Visual Studio 2008 的 intellisense

在引入 jQuery ui 套件中的 ui.datepicker.js 之後, 出現了 "'jQuery.support.htmlSerialize' 是 null 或不是一個物件" 的錯誤, 造成 VS2008 的 intellisense 失效, 雖然並不是那麼依賴 intellisense, 但是看到錯誤訊息就是不舒服, 所以就花了點時間找了一下到底是什麼地方造成的錯誤, 當然先從最可疑的部分(非 funciton 定義的 code)開始找, 在最底部發現以下的 code:

$.datepicker = new Datepicker(); // singleton instance
$.datepicker.initialized = false;
$.datepicker.uuid = new Date().getTime();
$.datepicker.version = "1.7";

把它們註解掉之後, 錯誤就消失了, 找到錯誤來源之後就該想個辦法來解決了, 於是稍稍修改一下第一行那個 singleton instance 的 code 如下:

$.datepicker = ($.support) ? new Datepicker() : {};

問題就解決了!!

 

註: 此修改僅針對 jquery 1.3jquery-ui 1.7, 其他版本沒有試過!

 

keywords: jquery, datepicker, ui, vs2008, intellisense, htmlSerializer

2009-02-18

取消正在執行的 WebService (ASP.NET AJAX)

假設我們有一個 Web service 是 Demo.Service1.SayHello, 如果我們想要在發出要求之後能夠取消的話, 可以用以下的方式

1. 宣告一個 executor 變數來存放等會兒發出的 web service request

2. 新增一個 invokingRequest 的事件處理, 像這樣

var executor = null;
Sys.Net.WebRequestManager.add_invokingRequest(function(sender, args) {
    executor = args.get_webRequest().get_executor();
})

3. 最後再寫一個函式在你想要取消的時候呼叫, 以取消 web service request

function abortRequest() {
    if (executor.get_started() && !executor.get_aborted() && !executor.get_responseAvailable()) {
        executor.abort();
    }
}

這樣就 OK 了!

 

keywords: webservice, asp.net ajax, cancel, abort, executor, invokingRequest