2011年7月21日 星期四

WCF教學(2) - 變更預設的Binding

延續WCF教學(1) - 建立WCF服務

當你建立WCF Service Library專案時,預設會使用wsHttpBinding,本練習將修改為basicHttpBinding,並透過組態檔設定進階的Binding資訊。
  • 組態工具
image
  • 修改預設的wsHttpBinding為basicHttpBinding
image
  • 新增組態設定
image
  • 選basicHttpBinding
image
  • 設SendTimeOut為10分
image
  • 在服務套用binding
image
  • CTRL+S 儲存
image
  • 目前app.config內容如下
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding0" sendTimeout="00:10:00" />
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WcfServiceLibrary1.Calc">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="NewBinding0"
          contract="WcfServiceLibrary1.IMyService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information,
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes,
          set the value below to true.  Set to false before deployment
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
  • F5, Run,可以正確執行運算
 image

WCF教學(1) - 建立WCF服務

使用Visual Studio 2010 建立一個WCF服務。
  • 建立WCF Service Library專案
image
  • 將服務改名
image
  • 設為 IMyService
image
  • 定義IMyService中包含Add方法。寫服務時,要決定服務要顯露何種類型的資料、哪些operation,要使用哪種 Message Pattern,這個決定稱為 Contract。Service Contract 描述服務的功能,服務合約中至少要有一個方法標示OperationContract,否則載入服務時會有例外。
[ServiceContract]
    public interface IMyService    {
        [OperationContract]
        string Add(int a, int b);
    }
  • Rename Service1類別名稱
image
  • 設定名稱為Calc
image
  • 實作IMyService介面
image
  • 實作程式碼
public class Calc : IMyService
    {
        #region IMyService Members

        public string Add(int a, int b)
        {
            return (a + b).ToString();
        }
        #endregion
    }
  • 按F5執行,可以看到wcf test client ,雙擊Add節點,在右方輸入 a 為2, b為3,按Invoke按鈕,下方會顯示執行結果。
image

2011年7月20日 星期三

Silverlight - 執行時期切換User Control

在Silverlight應用程式中,可能會有許多User Control,若要在執行時期切換不同的User Control來做顯示,可以利用一小段程式碼。

例如在Expression Blend建立一個Silverlight 應用程式,

  • 新增一個User Control

image

  • 在MainPage加一個Button

image

  • 切換到Visual Studio 2010撰寫Button Click的事件處程式碼

image

  • 加上以下程式到Click事件,這樣就行了

image

Expression Blend(4)– PathListBox Control

此範例也適用 WPF

PathListBox Control可以新增項目到Path,例如我想要在Silverlight 左上角顯示出圖片清單,每當點選清單中任一圖片時,就將這張圖的大圖顯示在右下角。
image

2011年7月19日 星期二

Expression Blend(3)–修剪圖型

若想要畫出扇型,可以利用現有圓型的Clip屬性來達成,例如

  • 畫一個矩型並填上顏色

image

總網頁瀏覽量