无法访问已关闭的流
本文关键字:访问 | 更新日期: 2023-09-27 18:05:50
我试图在WCF中接收一个流对象,但当我收到它时,我得到一个关闭的流。下面是我的代码
[OperationContract]
string ProcessPackageUsingStream(FileStream stream, string fileName, string docType, string customerKey, int documentId);
在实现中,我使用这个流对象。
string ProcessPackageUsingStream(FileStream stream, string fileName, string docType, string customerKey, int documentId);
{
//Stream is closed here
}
这是我的配置文件
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<bindings>
<basicHttpBinding>
<binding name="TransferService" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" transferMode="Streamed" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="None">
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="TransferServiceBehavior" name="TransferService">
<endpoint address=""
binding="basicHttpBinding" bindingConfiguration="TransferService"
contract ="ITransferService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TransferServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceThrottling
maxConcurrentCalls="500"
maxConcurrentSessions="500"
maxConcurrentInstances="500"
/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
在客户端,我添加了对该服务的引用,并使用了以下代码
SPCServiceClient service = new SPCServiceClient();
service.ProcessPackageUsingStream(File.OpenRead(@"D:'Dev'TestData'002160500041.pdf"), "002160500041.pdf", "Test", "VLR@2016", 1211);
我解决了这个问题。以下解决方案对我有效。我没有直接将流作为参数传递,而是创建了两个类,一个用于传递参数,另一个用于获取响应,如:
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public string DocType;
[MessageHeader(MustUnderstand = true)]
public string CustomerKey;
[MessageHeader(MustUnderstand = true)]
public string DocumentId;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
[MessageContract]
public class RemoteResponse
{
[MessageBodyMember(Order = 1)]
public string Token;
}
然后我更新了WCF服务中的方法:
[OperationContract]
RemoteResponse ProcessPackageUsingStream(RemoteFileInfo remote);
并更新了我的app.config
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IPreProcessor" transferMode="Streamed" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" >
<readerQuotas maxDepth = "2147483647"
maxStringContentLength = "2147483647"
maxArrayLength = "2147483647"
maxBytesPerRead = "2147483647"
maxNameTableCharCount = "2147483647"/>
</binding>
</basicHttpBinding>
</bindings>