2014年12月9日 星期二

WCF 的設定檔的簡易入門

 WCF的功能非常強大,有設定不同協定的端口,如果有同時大量的連線攻擊,也會阻檔,WCF的設定門檻過於複雜,每次都會忘了如何設定,所以決定寫下相關設定方法。

輸入服務名稱


輸入合約名稱



















輸入契約合約



如果傳輸方式web方式請選擇『http』模式,



















端點位址可以依實際情況為主,不過預設為空白即可



服務設定完成



二、進階-->設定端點行為

輸入名稱:WebHttpBehavior (可自訂)
新增 : webHttp 元素

 
















三、進階-->設定服務行為

輸入名稱:serviceBehavior (可自訂)
新增元素: ServiceDebugServiceMetadata


















四、服務-->端點
設定BehaviorConfiguration: WebHttpBehavior (上步驟的端點行為)

















五、服務--> Service
設定BehaviorConfiguration: serviceBehavior (上個步驟的服務行為)

















如果沒意外的話~~恭喜各位~應該就設定完成啦!

六、WCF預設webHTTP有傳輸大小限制,如果檔案量過大,需要另外設定

服務à點選 Binding configuration 



  
修改 MaxBufferPollSize = 90000 (可以實際大小調整)
     MaxBufferSize = 90000     (可以實際大小調整)
     maxReceiveMessageSize=90000     (可以實際大小調整)

















是不是比直接設定web.config 還要簡單許多呢!  

補充說明: 如果有需across-domain 的需求

把以下的XML放置在   <system.webServer></system.webServer> 節點中


<httpProtocol>
      <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*"/>
                           <add name="Access-Control-Allow-Headers" value="Content-Type, Accept" />
                           <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS" />
                           <add name="Access-Control-Max-Age" value="1728000" />
      </customHeaders>
    </httpProtocol>



以上如有錯誤觀念煩請告知本人, 我會立刻糾正錯誤的觀念! 謝謝

2014年12月2日 星期二

如何使用FileWatching 監控某資料夾的檔案是否改變

   window form 的程式好手們,一定對它非常熟悉,因為某案子的需求需要用它來監控檔案是否有變化,為了讓更多朋友瞭解在哪種時機使用它,以下是程式範例和說明

 步驟一: 宣告變數
bool bolChanged; //稍後在說明,此變數的用途
 WrittingEventLog writeObj = new WriteEvent.WrittingEventLog();
 FileSystemWatcher _watchFolder = new FileSystemWatcher();
string strReadParamCooperCategory = System.IO.Directory.GetCurrentDirectory()+"\\"; //資料夾目錄路徑
 string strReadParamCooperFile = "*.*";  //可以指定檔名,若不指定檔案,就監控整個目錄

  步驟二: 註冊監控事件
     _watchFolder.NotifyFilter: 針對檔案的哪種變化來觸發事

  相關更多NotifyFilter 屬性請參考 

   public Form1()
        {
            InitializeComponent();           
            _watchFolder.Path = strReadParamCooperCategory; 指定監控的目錄
            _watchFolder.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size | NotifyFilters.LastWrite; //可以指定哪種檔案的變化
            _watchFolder.Filter = strReadParamCooperFile;
            _watchFolder.Changed += new FileSystemEventHandler(onChanged); //檔案修改時觸發事件
            _watchFolder.Created += new FileSystemEventHandler(onChanged); //檔案新增時觸發事件
            _watchFolder.Deleted += new FileSystemEventHandler(onChanged); //檔案刪除時觸發事件
            _watchFolder.EnableRaisingEvents = true; //啟動監控事件,少了這行…就無法監控檔案
            this.radDesktopAlert1.Popup.AlertElement.ContentElement.MouseDown += ContentElement_MouseDown;
            controlCallerObj.init();
        }

 步驟三: 觸發事件
   private void onChanged(object sender, System.IO.FileSystemEventArgs e)
        {
            _watchFolder.EnableRaisingEvents = false; //記得先停止動監控,以免觸發多次
            switch (e.ChangeType)
            {
                case WatcherChangeTypes.Changed: //測試資料夾的檔案(新增/修改/刪除)有變化
                    Thread myRun = new Thread(threadReadParametere); //啟動多執行緒
                    myRun.Start();
                    _watchFolder.EnableRaisingEvents = true; //執行完畢後,請再重新啟動監控
                    break;
                default:
                    break;
            }
        }

步驟四: 執行動作
        private void threadReadParametere()
        {
            try
            {
 strFileNames = Directory.GetFiles(System.IO.Directory.GetCurrentDirectory() + "\\telphone", "*.txt");  //有偵測資料夾.內的檔案有變化時,需要取得該目錄的所有檔案
                bolChanged = true; //告訴UI,目前要更新畫面
            }
            catch (Exception ex)
            {
                writeObj.writeToFile("threadReadParametere 錯誤:" + ex.Message);
            }
        }

步驟四: 呈在UI的畫面
private void timer1_Tick(object sender, EventArgs e)
        {
            if(bolChanged) //如有有變化,執行以下程式
            {
                string[] strFileData;
                radDesktopAlert1.ContentText = "<html>";
                foreach (string strFileName in strFileNames)
                {
                    strFileData = strFileName.Split('\\');
                    radDesktopAlert1.ContentText += "<size=20>" + changeText(strFileName) +"<br>";                   
                }
                radDesktopAlert1.ContentText += "</html>";
                radDesktopAlert1.Show();
                bolChanged = false; //執行完後,改變其狀態,才不會重複執行
            }
        }

步驟五: 結果
 監控的資料夾,如有檔案新增/刪除/修改















就會自動跳出提示訊息,並顯示相關訊息




















補充說明: 目前確定 FileSystemWatch 會連續觸發兩次的事件,但已經有高手針對這bug 另外做了一隻 FileSystemSafeWatcher 的類別,會自動過濾重複觸發的事件

https://github.com/melenaos/FileSystemSafeWatcher/blob/master/FileSystemSafeWatcher.cs