我目前正在玩WCF服务和C#基本Winform应用程序。两者都在不同的服务器上。我试图让C#应用程序使用WCF从其他服务器获取文件。这是WCF的代码:
public Stream GetData(string fileName)
{
string filePath;
FileStream file = null;
try
{
// Stuff
file = File.OpenRead(Path.Combine(filePath, fileName));
file.Position = 0L;
return (Stream)file;
}
catch (Exception ex)
{
// Trace stuff
return Stream.Null;
}
}
这是操作合同:
[OperationContract]
Stream GetData(string fileName);
这是客户端配置文件:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBindingEndpoint" transferMode="Streamed" closeTimeout ="10:00:00" openTimeout="10:00:00" sendTimeout="10:00:00" receiveTimeout ="10:00:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://SERVER/UpdaterUtilsWCF" binding="netTcpBinding"
bindingConfiguration="NetTcpBindingEndpoint" contract="UpdaterUtilsWCF.IUpdaterUtilsWCF"
name="NetTcpBindingEndpoint">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
这是WCF配置文件:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="TCPBinding_IDataService" openTimeout="10:00:00"
closeTimeout="10:00:00"
sendTimeout="10:00:00"
receiveTimeout="10:00:00"
maxBufferPoolSize="2147483647"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
transferMode="Streamed">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="UpdaterWCF.UpdaterUtilsWCF">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="TCPBinding_IDataService"
name="NetTcpBindingEndpoint" contract="UpdaterWCF.IUpdaterUtilsWCF">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
name="MexTcpBindingEndpoint" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://SERVER/UpdaterUtilsWCF" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceThrottling maxConcurrentCalls="100" maxConcurrentInstances="2147483647" maxConcurrentSessions="200" />
<dataContractSerializer maxItemsInObjectGraph="2147483646"/>
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
在WCF配置文件中将最大并发调用,实例和会话设置为最大值。
这是客户端应用程序的代码:
using (Stream stream = WCFClient.GetData(textBox1.Text))
{
// stuff
}
当我执行应用程序时,它会在客户端(或WCF端)崩溃,但是当Stream传递给客户端应用程序时。我已经跟踪了WCF上的代码并且已经进行了调用,并且在WCF的末尾调用了返回代码。调用后立即出现错误,文件非常小(400kb) 这是错误:
System.ServiceModel.CommunicationException: 套接字连接已中止。这可能是由错误引起的 处理您的消息或远程超过接收超时 主机或底层网络资源问题。本地套接字超时是 &#39; 10:00:00&#39 ;. System.Net.Sockets.SocketException:强制执行现有连接 由远程主机关闭 在System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32 size,SocketFlags socketFlags) 在System.ServiceModel.Channels.SocketConnection.ReadCore(Byte []缓冲区, Int32偏移量,Int32大小,TimeSpan超时,布尔结束)
请帮助我。
答案 0 :(得分:0)
根据我的经验,这通常是配置中的问题。确保你遵循一个已知的好例子开始。微软的例子在这里: https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-enable-streaming 可能是一个好的开始。 设置所有三个缓冲区大小或使用只有几个字符的文件进行测试,以便在发送更大的文件之前验证基本功能。
<basicHttpBinding>
<binding name="HttpStreaming" maxBufferPoolSize="2147483647"
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Streamed"/>
</basicHttpBinding>
<!-- an example customBinding using Http and streaming-->
<customBinding>
<binding name="Soap12">
<textMessageEncoding messageVersion="Soap12WSAddressing10" />
<httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
</binding>
</customBinding>
答案 1 :(得分:0)
经过一系列调试后,我发现了问题所在。在我的WCF服务代码中,我在try-catch:
之后实现了流资源发布
finally
{
if (file != null)
{
file.Close();
file = null;
}
}
当客户端收到Stream时,Stream正在关闭WCF端。
这是我找到答案的地方: IOException on streamed file upload via WCF over HTTP