简体   繁体   中英

Issue with wcf streaming on IIS

I am trying to stream through WCF. It works on standalone but hosted on IIS I receive an exception at the upload method. I set to True the debug option on Web.cofig to get a better understanding but all I get is:

An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll Additional information: The handle is invalid.

if (client.isItOpen())
{
   client.fileName(fileName);
   FileStream instream1 = File.OpenRead(filePath);                
   bool result1 = client.UploadStream(instream1);
   if (result1)

Since the isItOpen() returns true there is communication with the service but at the UploadStream is the exception. Are there certain options I need to set at the IIS to make it work? I remeber I was changing the upload limit but nothing more. The file I am upload is small, only a couple of KB.

Client:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding maxReceivedMessageSize="2147483647" transferMode="Streamed" />
    </basicHttpBinding>
  </bindings>
  <client>
    <endpoint address="http://IP_HERE/SyncWCF/SyncService.svc/ep1"
      binding="basicHttpBinding" contract="ISyncService" name="BasicHttpBinding_ISyncService" />
  </client>
</system.serviceModel>

if (client.isItOpen())
        {
            client.fileName(fileName);
            FileStream instream1 = File.OpenRead(filePath);                
            bool result1 = client.UploadStream(instream1);
            if (result1)
            {
                StatusTextBox.AppendText("Done!" + Environment.NewLine);
            }
            instream1.Close();
        }
        client.Close();

Service:

 <system.serviceModel>
<services>
  <service name="SyncWCF.SyncService">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost/SyncService"/>
      </baseAddresses>
    </host>
    <!-- this endpoint is exposed at the base address provided by host: http://localhost/ServiceModelSamples/service  -->
    <endpoint address="ep1" binding="basicHttpBinding" contract="SyncWCF.ISyncService"/>
    <!-- the mex endpoint is exposed at http://localhost/SyncService/mex -->
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<bindings>
  <!-- an example basicHttpBinding using streaming -->
  <basicHttpBinding>
    <binding maxReceivedMessageSize="2147483647" transferMode="Streamed"/>
  </basicHttpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>

 public bool UploadStream(System.IO.Stream stream)
    {
        //this implementation places the uploaded file
        //in the current directory and calls it "uploadedfile"
        //with no file extension
        string filePath = Path.Combine(System.Environment.CurrentDirectory, FileName);
        try
        {
            Console.WriteLine("Saving to file {0}", filePath);
            FileStream outstream = File.Open(filePath, FileMode.Create, FileAccess.Write);
            //read from the input stream in 4K chunks
            //and save to output stream
            const int bufferLen = 4096;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = stream.Read(buffer, 0, bufferLen)) > 0)
            {
                Console.SetCursorPosition(0, Console.CursorTop - 1); Console.WriteLine(buffer);
                outstream.Write(buffer, 0, count);
            }
            outstream.Close();
            stream.Close();
            Console.WriteLine();
            Console.WriteLine("File {0} saved", filePath);

            FileName = null;
            return true;
        }

The System.ServiceModel.FaultException - The handle is invalid is caused by this line in your service implementation:

Console.SetCursorPosition(0, Console.CursorTop - 1);

Just remove it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM