[英]Client aborted when WCF return Stream
我目前正在使用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:套接字連接已中止。 這可能是由於處理您的消息時出錯,遠程主機超出了接收超時或潛在的網絡資源問題引起的。 本地套接字超時為“ 10:00:00”。 System.Net.Sockets.SocketException:System.Net.Sockets.Socket.Receive(Byte []緩沖區,Int32偏移量,Int32大小,SocketFlags socketFlags)處的遠程主機強制關閉了現有連接。 SocketConnection.ReadCore(Byte []緩沖區,Int32偏移量,Int32大小,TimeSpan超時,布爾值關閉)
請幫我解決一下這個。
以我的經驗,這通常是配置中的問題。 確保您遵循一個已知的好例子開始。 Microsoft的示例位於: https : //docs.microsoft.com/zh-cn/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>
經過一堆調試,我發現了問題。 在我的WCF服務代碼中,我在try-catch之后實現了流資源發布:
finally
{
if (file != null)
{
file.Close();
file = null;
}
}
客戶端接收Stream時,Stream在WCF端關閉。
這是我找到答案的地方: 通過HTTP通過WCF上傳流文件時出現IOException
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.