2009年2月9日 星期一

WCF: 使用 Header 傳遞訊息

在使用 WCF 時,自訂訊息內容自然是免不了的設計。因此使用 DataContract, 已經是 WCF 中經典中的經典了。
如果需要 WCF client 多傳一些訊息回來,就必須在Service上更改 DataContract, 然後Client 再更新程式…..

等一下,DataContract 是一個 Contract,怎麼能說改就改?
話又說回來,面對各式各樣的客戶,常常見到不同的需求需要傳遞不同的訊息。此時該怎麼辦?

原來,WCF 內建有個傳遞訊息的方法,且不需要修改 DataContract。

步驟1:修改 WCF Client 的 config。

        <client>
          <endpoint address="http://localhost:8081/test/service1.svc" binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
              name="BasicHttpBinding_IService1" >
            <!--  use header to pass message  -->
            <headers>
              <MyHeader xmlns="http://sample.org/test">
                This is my test data
              </MyHeader>
            </headers>
          </endpoint>
        </client>

步驟2: 修改 Service, 使用 OperationContext 讀取 header

      int myHeaderIndex = OperationContext.Current.RequestContext.RequestMessage.Headers.FindHeader("MyHeader", "http://sample.org/test");
      string result;
      if (myHeaderIndex != -1)
      {
        result = OperationContext.Current.RequestContext.RequestMessage.Headers.GetHeader(myHeaderIndex);
      }
      else
        result = OperationContext.Current.RequestContext.RequestMessage.Headers.Action;
      return string.Format("You entered: {0}, Header: {1}", value, result);

需要注意的是, Client config 中,可以傳不同的訊息,由 Service 來搜尋header。這樣一來,就不需要修改 DataContract 了。
當然,這樣也是有相當大的壞處,這樣會造成設計人員各自傳遞不同格式的訊息。假以時日,一堆不同的自訂訊息,最終難以維護。

sample download

2009年2月5日 星期四

Enterprist Library: 寫到同一個 log file

也是寫 log 的問題。
當 log application block (LAB) 和 exception handling application block (EHAB) 寫訊息到同一個檔案時,會因 file 已被 open 的關係,產生一個 (GUID)log.log 類似的檔案。
這一點很不好,因為我想將所有的訊息(記錄訊息和錯誤訊息)同到相同的檔案,以方便觀察程式的實際運作行為。

原來這一點,已在 V4.0 版被修正了.

在 EHAB 的 logging handler  上,取得 property

image

property 上,有個新的 UseDefaultLogger,預設值為 false, 將它改為 True。就完成了。

image

 

see http://msdn.microsoft.com/en-us/library/cc511712.aspx

Enterprise Library Logging Application Block: Timespan format

Enterprise Library 大概是我相當堅持要使用的 open source library 吧。
尤其是 logging 與 exception handling 這兩個。

當要記錄訊息時,最常用的就是 rolling flat file trace listener

image

然而,記錄的檔案,時間標記的格式,預設卻是UTC  時間,根台北時間差8小時。
怎麼辦呢?google 大神又發揮了神蹟,原來在 Text Formatter 有東西可以改。

image

改成 [{timestamp(local:yyyy-MM-dd HH:mm:ss)}] {message} , 就符合我的需求了

image

 

see also http://blogs.msdn.com/tomholl/archive/2006/01/22/516055.aspx

Web Services Architecture

我個人十分喜歡 WCF。因為

  1. 可以實作出高重複使用性的元件(re-use)
  2. 依靠設定(configuration)而非程式碼。
  3. 實作出許多 WS-* 。

第三點,較難一次說完,而且WS-* 的標準也愈來愈多。
想要說服同事使用 WCF 的技術,可得花費一番功夫。

下圖是 Web Services Architecture, 是我在 2006 年聽 Web cast 時抓的圖。

Web Service Architecture

裡頭有相當多的標準我還不了解呢。使用 WCF的最大好處,就是 WCF 已經幫我們實作許多這些標準,而且隨著版本昇級,WCF也將實作更多的 WS-* 檔準。
我們需要的就是「看文件」+「設定」而已。

See also An Introduction to the Web Services Architecture and Its Specifications , Web Services Architecture

2009年2月4日 星期三

ThickBox

今日無意間發現了一個好用的 jquery extension library, ThickBox

可用來顯示圖片,model dialog 等。
試試該 demo 吧!(在ThickBox網頁的下方)

2009年2月3日 星期二

Private Accessor for functionXXX is not found. Please rebuild the containing project or run the Publicize.exe manually.

在寫單元測試時,發生了這樣的錯誤。

Private Accessor for functionXXX is not found. Please rebuild the containing project or run the Publicize.exe manually.

原來,當單元測試的程式碼 compile 失敗時,記得不再硬要 Visual Studio 再建立單元測試,就會發生這樣的錯誤。

先讓單元測試的 Class library 先 compile 過了再說。

2009年2月2日 星期一

適用於 Windows Vista 和 Windows Server 2008 的 Windows Internet Explorer 8 版本選擇 1

IE 8 RC1 出來了。

中文英文

還是下載英文的吧。這已經是我的習慣了。

Share with Facebook