/* by josh */

2014年3月26日 星期三

[C#] .NET Compact Framework 3.5之WCF 使用說明

微軟已在 .net compact framework 3.5版中加入了WCF client的功能(但並未支援WCF server);
要在.net compact framework上使用wcf service必須用到Power Toys for .NET Compact Framework 3.5中的ServiceModel Metadata Tool for the .NET Compact Framework (NetCFSvcUtil)程式來產生Proxies藉以呼叫WCF service。
注意:若你是使用Windows 7以上的OS開發,務必下載支援windows 7的NetCFSvcUtil,否則無法正確產生proxies

以下說明如何建立service及client,

Server端:
Step 1: 新增合約IGreetingService.cs,此例之合約名稱為IGreetingService

namespace WcfDemoService
{
 // 注意: 若變更此處的介面名稱 "IGreetingService",也必須更新 App.config 中 "IGreetingService" 的參考。

    [ServiceContract]
    public interface IGreetingService
    {
        [OperationContract]
        string DoWork();
              
    }
}

Step 2:實作合約內容GreetingService.cs


namespace WcfDemoService
{
    // 注意: 若變更此處的類別名稱 "GreetingService",也必須更新 App.config 中 "GreetingService" 的參考。
    public class GreetingService : IGreetingService
    {
        public string DoWork()
        {
            return "This is return string2";
        }
      
    }
}
Step 3: 在主程式中撰寫開啟服務的程式碼
 class Program
 {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(GreetingService));
            try
            {
                host.Open();
                Console.WriteLine("Server Opened !");
                Console.Read();
            }
            finally
            {
                if (host.State == CommunicationState.Faulted)
                    host.Abort();
                else
                    host.Close();
            }
        }
 }
Step 4: 建立App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfDemoService.GreetingServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="WcfDemoService.GreetingServiceBehavior"
                name="WcfDemoService.GreetingService">
                <endpoint address="GreetingService" binding="basicHttpBinding" contract="WcfDemoService.IGreetingService" bindingConfiguration="basicHttpBindingConfiguration">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://192.168.0.103:8731"/>
                    </baseAddresses>
                </host>
            </service>
        </services>
      <bindings>
        <basicHttpBinding>
          <binding name="basicHttpBindingConfiguration" maxReceivedMessageSize="20971510">
            <readerQuotas maxStringContentLength="20971520" maxArrayLength="20971520"/>
         
          </binding>
        </basicHttpBinding>
      </bindings>
    </system.serviceModel>
</configuration>

Client端:
Step 1: 使用NetCFSvcUtil產生Proxy檔案,方法如下:
    a) 先執行host程式
    b)在cmd下cd到NetCFSvcUtil.exe的路徑下,輸入NetCFSvcUtil.exe http://host ip:host port/  按          下enter後會產生兩個檔案GreetingService.csCFClientBase.cs
        

Step 2:建立.net compact framework專案並匯入GreetingService.csCFClientBase.cs

Step 3: 在程式中撰寫呼叫wcf service的程式碼
private void btnGetStringByWCF_Click(object sender, EventArgs e)
{
System.ServiceModel.Channels.Binding binding = 
GreetingServiceClient.CreateDefaultBinding();
   string address = GreetingServiceClient.EndpointAddress.Uri.ToString();
   GreetingServiceClient m_proxy = 
new GreetingServiceClient(binding, new System.ServiceModel.EndpointAddress(address));
   
string GetString=m_proxy.DoWork();
  this.textBox1.Text = GetString;
}