我是 WCF 的新手,所以如果您能尽可能详细地回答,我将非常感激:) 我有一个 WCF 服务库和一个 WPF 应用程序(谁是客户端)。想要的结果是一个能够在连接的客户端之间实现文件共享的应用程序。我使用一种方法构建了一个真正基本的 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 应用程序的代码:
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。为什么是这样?我是否正在以正确的方式完成我想要的应用程序?谢谢:)
最佳答案
您可能会收到TimeoutException,因为发送文件的时间超过了您的服务允许的时间。
在服务器和客户端的配置文件中,请务必增加 receiveTimeout 和 sendTimeout。
您还可能会遇到大小限制,因为 WCF 配置最大消息大小,并且该文件将被视为消息的一部分。查看 maxBufferPoolSize、maxReceivedMessageSize 和下面的成员
<readerQuotas
maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
关于wcf - 通过 WCF 传输文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9751336/