茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1658720 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00139.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2017/7/20 16:04:54

WCF 簡易教學

公司希望有 WCF當中介程式,隔離資料庫,這對習慣 Web API的我們,有點對 WCF生疏,所以寫了些簡單的教學,一方面怕自己有點生疏,另一方面給有需要的朋友們,減少學習的時間。
一、 建立介面的宣告
在介面函式上輸入關鍵字『 OperationContract』,這樣端點的服務
能搜尋到此宣告的函式,另外如果需要宣告物件時,該類別要宣告關鍵字『 DataContract』,類別的屬性宣告『 DataMember

 [ ServiceContract ]
    public interface IService1
    {
        [ OperationContract ]
        IEnumerable < tempCalendar > readGoogleData( int intStartIkey, int intEndIkey);

        [ OperationContract ]
        bool writeGoogleData( tempCalendar updateData);
    }

[ DataContract ]
    public class tempCalendar
    {
        [ DataMember ]
        public int ikey
        {
            get
;
            set ;
        }
        [ DataMember ]
        public string sendParameter
        {
            get ;
            set ;
        }     
    }
二、
 
實作介面的類別

用E ntitySQLlinq語法去讀取資料庫的資料和寫入資料庫的資料,直接
回傳 IEnumerable型別,就因為取回的資料只是用來查詢, 會有較佳的執行速度
public class Service1 : IService1
    {
        GoogleEntities
googleEntityObj = new GoogleEntities ();
        public IEnumerable < tempCalendar > readGoogleData( int intStartIkey, int intEndIkey)
        {
                IEnumerable < tempCalendar > tempCalendarList = from c in googleEntityObj.Calendar
                     Where  c.ikey >= intStartIkey && c.ikey <= intEndIkey
                                       select new tempCalendar
                                          {
                                             ikey=c.ikey,

                                              sendParameter=c. 上傳參數
                                          };
                return tempCalendarList;
        }

        public
bool writeGoogleData( tempCalendar updateData)
        {
                var original = googleEntityObj.Calendar.Find(updateData.ikey);
                original. 上傳完成 = updateData.isSendGoogleOK;
                original. 結果訊息 = updateData.sendMessage;;

                googleEntityObj.SaveChanges();
                return true ;
        }
    }
三、   設定 web.config
   <
system.serviceModel >
    < bindings >
      < basicHttpBinding >
<!-- 預設允許大量傳輸 -->
        < binding name =
cusBinding maxBufferSize = 64000000 maxReceivedMessageSize = 64000000    />
      </ basicHttpBinding >
    </ bindings >
    < client >
<!-- 連線端口設為
basicHttpBinding 具名管線,   -->
      < endpoint binding = basicHttpBinding bindingConfiguration = cusBinding contract = WcfService1.IService1 />
    </ client >
    < services >
      < service name = WcfService1.Service1 >
        < endpoint address = binding = basicHttpBinding bindingConfiguration = cusBinding contract = WcfService1.IService1 />

      </ service >
    </ services >
    < behaviors >
      < serviceBehaviors >
        < behavior name = >
          < serviceMetadata httpGetEnabled = true httpsGetEnabled = true />

          < serviceDebug includeExceptionDetailInFaults = false />
<!-- 預設連線數允許到 100 -->
          < serviceThrottling maxConcurrentCalls = 100 maxConcurrentSessions = 50
maxConcurrentInstances = 50 />
        </ behavior >
      </ serviceBehaviors >
    </ behaviors >
    < protocolMapping >
      < add binding = basicHttpsBinding scheme = https />
    </ protocolMapping >

    < serviceHostingEnvironment aspNetCompatibilityEnabled = true multipleSiteBindingsEnabled = true />
  </ system.serviceModel >
四、   如何呼叫 service

先透過 vs2012tool視窗,執行 svcutil.exe  service.svc的位址,自
動產生檔案到 c:\programe(X86)\ Microsoft Visual Studio 12.0, 2個檔案加入到呼叫 service的程式專案
















以下示範如何呼叫服務
  public IEnumerable < tempCalendar > serviceReadGoogleData( int intStartIkey, int intEndIkey)
        {
            try
            {
               
BasicHttpBinding myBinding = new BasicHttpBinding ();
                EndpointAddress myEndpoint = new EndpointAddress ( );
                ChannelFactory < IService1 > myChannelFactory = new ChannelFactory < IService1 >(myBinding, myEndpoint);
                IService1 wcfClient1 = myChannelFactory.CreateChannel();

               return wcfClient1.readGoogleData(intStartIkey, intEndIkey);
            }
            catch ( Exception ex)
            {
                throw new Exception (ex.Message);
            }
        }
五、
 
設定 web協定

服務的介面要改成 GETPOST等協定,可以依自己的習慣修改網址的傳遞
參數的模式

IService.svc
    [ OperationContract ]
    [ WebInvoke (Method = , UriTemplate = , RequestFormat =
WebMessageFormat .Json, ResponseFormat = WebMessageFormat .Json, BodyStyle = WebMessageBodyStyle .WrappedRequest)]
    bool writeToGoogleCalendar( string orgIkey);

Service.svc.cs
public bool writeToGoogleCalendar( string orgIkey)
        {
            return true ;
        }


修改 Web.config
< system.serviceModel >
    < bindings >
    </ bindings >
    < client >
      < endpoint binding = webHttpBinding
bindingConfiguration = contract = WcfService1.IService1 />
    </ client >
    < services >
      < service name = WcfService1.Service1 >
        < endpoint address = behaviorConfiguration = webBehavior binding = webHttpBinding
          bindingConfiguration = contract = WcfService1.IService1 />
      </ service >
    </ services >
    < behaviors >
      < endpointBehaviors >
        < behavior name = webBehavior >

          < webHttp />
        </ behavior >
      </ endpointBehaviors >
      < serviceBehaviors >
        < behavior name = >
          < serviceMetadata httpGetEnabled = true httpsGetEnabled = true />
          < serviceDebug includeExceptionDetailInFaults = false />
          < serviceThrottling maxConcurrentCalls = 100 maxConcurrentSessions = 50
            maxConcurrentInstances = 50 />
        </ behavior >
      </
serviceBehaviors >
    </ behaviors >


呼叫 WCF網址 :




六、   參考文獻


原文出處: 技術事件薄: WCF 簡易教學
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|