2012-03-17 141 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