2012-03-17 140 views
2

我是WCF的新手,所以我真的很感激,如果你可以回答儘可能詳細:)我有一個WCF服務庫和WPF應用程序(誰是客戶端)。想要的結果是一個應用程序,使文件共享之間的連接clients.I建立真的基本WCF服務庫與一個方法:通過WCF文件傳輸

[ServiceContract] 
public interface IFileService 
{ 
    [OperationContract] 
    byte[] GetFile(string fullPath); 
} 

而且實現了這個方法是這樣的:

public class FileService : IFileService 
{ 
    public byte[] GetFile(string fullPath) 
    { 
     return System.IO.File.ReadAllBytes(fullPath); 
    } 
} 

這是WPF客戶端項目App.config文件:

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="WSHttpBinding_IFileService" closeTimeout="00:01:00" 
       openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
       bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
       maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
       messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" 
       allowCookies="false"> 
       <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
        maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
       <reliableSession ordered="true" inactivityTimeout="00:10:00" 
        enabled="false" /> 
       <security mode="Message"> 
        <transport clientCredentialType="Windows" proxyCredentialType="None" 
         realm="" /> 
        <message clientCredentialType="Windows" negotiateServiceCredential="true" 
         algorithmSuite="Default" /> 
       </security> 
      </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost:9355/TankusFileTransferService/Service/" 
      binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IFileService" 
      contract="TankusFileService.IFileService" name="WSHttpBinding_IFileService"> 
      <identity> 
       <userPrincipalName value="GIL-LAPTOP\Gil" /> 
      </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

這是從主窗口WPF AP代碼折戟:

public partial class MainWindow : Window 
{ 

    ServiceHost sh; 
    TankusFileService.FileServiceClient fsc; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void btn_Connect_Click(object sender, RoutedEventArgs e) 
    { 
     Uri uri = new Uri("http://127.0.0.1:1234/"); 
     sh = new ServiceHost(typeof(TankusFileTransferService.FileService), uri); 
     sh.Open(); 
     lbl_Listener.Content = sh.Description.Endpoints[0].Address.ToString(); 
    } 

    private void btn_Disconnect_Click(object sender, RoutedEventArgs e) 
    { 
     sh.Close(); 
     lbl_Listener.Content = string.Empty; 
    } 

    private void btn_GetFile_Click(object sender, RoutedEventArgs e) 
    { 
     fsc = new TankusFileService.FileServiceClient(); 
     fsc.Endpoint.Address = new EndpointAddress("http://127.0.0.1:1234/"); 
     fsc.Endpoint.Binding = new BasicHttpBinding(); 
     byte[] bytes = fsc.GetFile(@"D:\mika.txt"); 
     System.IO.File.WriteAllBytes(@"D:\mika_new.txt", bytes); 
    } 
} 

當我按下連接按鈕並初始化ServiceHost對象,以便它可以開始監聽我按下getFile按鈕。當調用GetFile()函數時,會引發TimeoutException。爲什麼是這樣?我甚至在正確的方式完成我想要的應用程序?謝謝:)

+0

發佈您的頻道的web.config設置。您可能需要增加尺寸。 – Mutant 2012-03-17 16:49:48

回答

2

您可能會收到TimeoutException,因爲發送文件比發送服務所需的時間要長。

在服務器端和客戶端的配置文件中一定要增加receiveTimeout的SendTimeout

由於WCF配置了最大郵件大小,因此您可能還會遇到大小限制,並且該文件將被視爲郵件的一部分。看看maxBufferPoolSizemaxReceivedMessageSize,以下

<readerQuotas 
    maxDepth="32" maxStringContentLength="8192" 
    maxArrayLength="16384" maxBytesPerRead="4096" 
    maxNameTableCharCount="16384" /> 
0

成員的同步Web服務請求是不轉讓有關文件的最好方法。即使它工作,如果您需要擴展端點以處理併發請求,您將很快遇到麻煩。通過將文件上傳到服務端點,可能會影響端點的可用性。

一個更好的解決方案 - WPF應用程序將文件流寫入磁盤(或數據庫,ftp服務器或隊列),然後向服務器發送快速單向命令消息,然後該服務器會抓取文件。

這具有極大的可擴展性,並且會導致少得多的可用性類型異常。

UPDATE

根據我的經驗,當你上傳大文件到Web服務端點,你可以得到的可用性問題,尤其是如果有任何顯著併發。如果你知道你的上限是什麼(文件大小,併發連接等),你可以規劃這些東西,並且你可以把它定義爲一個服務級別協議,但是你想要做的事情的性質(對等 - 同行)根據定義是一個不穩定的環境,在這樣的環境下,這樣的規劃會很困難

然而,這就是說,你的要求是P2P的事實意味着理想情況下不應該是一個集中的環境來實現我建議的存儲和檢索消息模式的類型。

Windows Azure blob存儲是如何實現這一點的一個例子。

+0

我真的不明白你的答案。最後,我想擁有一個文件共享系統,客戶端可以在其他客戶端的指定目錄中查看所有文件,並可以將選定的文件從其他用戶的計算機傳輸到第一臺客戶端計算機。 – 2012-03-18 17:05:05

+0

那麼,如果我想將2Gb文件從您的機器傳輸到我的機器呢?你的服務應該支持嗎? – 2012-03-19 08:09:29

+0

好吧,這是一個課程的最終項目,所以我猜測2GB文件不會被傳輸。我想了解有關該技術的更多信息,並更好地理解您的建議解決方案。 – 2012-03-20 18:16:45