WCF: unable to access closed stream

I am trying to get a Stream object in WCF, but when I get it, I get a private stream. Below is my code

[OperationContract]
string ProcessPackageUsingStream(FileStream stream, string fileName, string docType, string customerKey, int documentId);

in the implementation, I use this stream object.

string ProcessPackageUsingStream(FileStream stream, string fileName, string docType, string customerKey, int documentId);
{
     //Stream is closed here
}

Here is my configuration file

<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>

On the client side, I added a link to this service and used the following code

SPCServiceClient service = new SPCServiceClient();
    service.ProcessPackageUsingStream(File.OpenRead(@"D:\Dev\TestData\002160500041.pdf"), "002160500041.pdf", "Test", "VLR@2016", 1211);
+4
source share
1 answer

I solved this problem. The following solution worked for me. Instead of passing the stream directly as a parameter, I created two classes: one for passing parameters and the other for receiving a response, for example:

[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;
}

Then I updated my method in the WCF service:

[OperationContract]
RemoteResponse ProcessPackageUsingStream(RemoteFileInfo remote);

And updated my 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>
0
source

Source: https://habr.com/ru/post/1653641/


All Articles