會員權益 我的購物車 如何買書 訂閱電子報 讀者回函
 
 
 
 
 
 
帳號:
密碼:
 

>>加入會員!

 
忘記密碼? 會員專區
 

ASP.NET MVC 開發心得分享:Action 要小心使用 using

今天在 Code Review 的時候抓到一支程式臭蟲,這個功能是某 ASP.NET MVC 專案中的一個匯出下載檔案功能,該功能的 Action ...more

黃保翕(Will保哥) 2010/08/26
 
小編:Vicky
以獨特的視角,帶你發現生活中的無窮樂趣
生活的美妙並不是因為美麗景物,而是與我們身邊的人之間的互動,所產生的笑容跟剎那的記憶有關。要想將生活中珍貴的一面記憶下來,就只能靠日記或是照片了。

在NAVER網站上活躍的
...more
 
       
胡百敬 許薰尹
鄭淑芬 李啟宏
宋志峰 邱世華
呂高旭 陳永昇
麻理鈴 程杰
趙敏翔 羅慧真
更多部落格
 
回首頁 回上一頁
 
類別 >網頁開發
 
WCF版本控制
李漢宗 2008/01/29
先前我們已經實做過一個簡單的WCF服務程式。但是在服務的使用過程中,有可能因為環境或者商業流程的變更而需要對WCF服務作一些調整。在調整的過程中,可能對於service operation或是service contract作一些變更,像是新增、刪除、修改operation。這些變更會需要參考服務的用戶端程式去更新其參考的proxy程式。但是,如果用戶端程式一多,需要針對每個用戶端更新的數量實在過於龐大,而且也不是每個用戶端都需要針對變更的部份作更新(例如,沒有使用到變更的相關內容的用戶端程式)。要注意一些原則,像是盡量避免去變更服務,變更後要注意現有用戶端原有功能仍然能夠運行。

如果只是新增service operation,對於使用舊有的service operation的用戶端程式其實是沒有任何影響的,這稱為「非破壞性的變更」。但是如果想要新的用戶端程式只能使用新的service operation,而不能存取舊有的service operation。這就要針對service contract作版本控制─對於舊有的service contract保留,然後再新增另一個版本的service contract。

 

<ServiceContract()> _
Public Interface IMyService
  <OperationContract()> _
  Function MyOperation1(ByVal myValue1 As String) As String

   <OperationContract()> _
  Function MyOperation2(ByVal dataContractValue1 As DataContract1) As String

   <OperationContract()> _
  Function MyOperation3(ByVal myValue1 As String) As String

End Interface

<ServiceContract()> _
Public Interface IMyServiceV2
  <OperationContract()> _
  Function MyOperation_Fix(ByVal myValue1 As String, ByVal myValue2 As String) As String

End Interface

Public Class MyService
  Implements IMyService, IMyServiceV2
  Public Function MyOperation1(ByVal myValue1 As String) As String Implements IMyService.MyOperation1
    Return "Hello: " + myValue1
  End Function

  Public Function MyOperation_Fix(ByVal myValue1 As String, ByVal myValue2 As String) As String Implements IMyServiceV2.MyOperation_Fix
    Return "Hello: " + myValue1 + “ , I am ” + myValue2
  End Function

  Public Function MyOperation2(ByVal dataContractValue1 As DataContract1) As String Implements IMyService.MyOperation2
    Return "Hello: " + dataContractValue1.FirstName
  End Function

  Public Function MyOperation3(ByVal myValue1 As String) As String Implements IMyService.MyOperation3
    Return "xxx"
  End Function
End Class

<DataContract()> _
Public Class DataContract1
  Private m_firstName As String
  Private m_lastName As String

  Public Property FirstName() As String
    Get
      Return
m_firstName
    End Get
    Set(ByVal value As String)
      m_firstName = value
    End Set
  End Property

  Public Property LastName() As String
    Get
      Return
m_lastName
    End Get
    Set(ByVal value As String)
      m_lastName = value
    End Set
  End Property
End Class

 

我們將上個練習中的程式碼拿來做些許修改,在程式碼中,我們去新增一個Interface,並且命名為IMyServiceV2,並且在舊跟新的Service Contract屬性(attibute)的命名空間(NameSpace)以及名稱(Name)屬性(property)中,給予值來辨別其不同處(這裡以命名空間中的日期來判斷修改日期,以Interface名稱來辨識服務版本)。不過這裡要注意的是,修改Namespace以及Name屬性會造成「破壞性的變更」,所以要去更新服務的proxy程式。這裡為什麼要提到這個破壞性變更?主要是為了能夠針對可以透過命名空間來判斷版本的不同,但是如果Interface名稱取得好(像是把日期或版本號碼放在名稱中),也不用去設定會造成破壞性變更的命名空間以及名稱屬性。
 
在上面修改完後,還要注意在服務端的web.config設定,因為service contract是透過端點(endpoint)來連結,所以我們還要在設定中新增一個端點:
 

<configuration>
 <system.serviceModel>
  <services>
   <!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
   <service name="MyService" behaviorConfiguration="returnFaults">
    <endpoint contract="IMyService" binding="wsHttpBinding"/>
    
   </service>
  </services>
  <behaviors>
   <serviceBehaviors>
    <behavior name="returnFaults">
     <serviceDebug includeExceptionDetailInFaults="true" />
     <serviceMetadata httpGetEnabled="true" />
    </behavior>
   </serviceBehaviors>
  </behaviors>
 </system.serviceModel>

 <system.web>
  <compilation debug="true" />
 </system.web>

</configuration>

 
這個端點的Contract屬性要設定為interface名稱,並且以預設的wshttpbinding binding作為繫結的設定。然後我們再新增一個web application的專案,然後依照先前文章中的方式參考WCF服務。接下來在Default.aspx頁面中,拉入兩個TextBox以及一個Button、一個Label。在Button的Click事件中,我們輸入下列程式:
 
Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim localhostV2 As New localhostV2.MyServiceV2Client
    Label1.Text = localhostV2.MyOperation_Fix(TextBox1.Text, TextBox2.Text)
  End Sub
 
然後輸入TextBox後,我們可以看到下面的顯示:
 
你在new proxy物件程式時,一定會發現也可以呼叫先前的WCF服務版本,但是在operation的使用上就區分成兩種版本。使用舊有WCF版本的用戶端程式,如果對於新版的WCF上的operation如果沒有需要的話,其實只要沿用原有的版本就好。這樣的版本控制讓WCF能夠更容易被使用以及識別,但是前提還是要將識別條件設定好才行。
 

  回首頁關於悅知聯絡我們
電話:02-2719-8811 傳真:02-2719-7980 地址:105台北市松山區復興北路99號12樓
copyright © 精誠資訊股份有限公司 悅知文化 All rights reserved