none
Uploading files to WCF service RRS feed

  • Question

  • Hi,

    It was just plain simple to upload a file. I did it this way, with a stream :

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/56d3e3e6-0aab-4391-bad8-18718c02d88b/problems-with-sending-image-over-wcf?forum=wcf

    The 1 parameter limitation is really a problem (I guess for most users), so this implementation looks a bit crippled to me. I think most developers would like to include at least a filename and location together with the uploaddata. 

    const int bufferLen = 4096; byte[] buffer = new byte[bufferLen]; int count; while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)

    .........

    Data is being sent in chunks. I was wondering if there is a way to include a first chunk, at the beginning of thet stream that holds the filename and location? Or do I see it completely wrong?

    I tried already to create a uploadclass like this :

        [DataContract]
        [KnownType(typeof(UploadRequest))]
        public class UploadRequest {
            [DataMember]
            public string FileName { get; set; }
    
            [DataMember]
            public string Folder { get; set; }
    
            [DataMember]
            public byte[] Content { get; set; }
        }
    

    I use ReadAllBytes inside the app and WriteAllBytes in the WCF service. But I ended up nowhere. I don't like to ask too many questions here and most of the times I try myself to fix it or to google for a solution, but now I'm already busy 1 week and everything I found on forums is not helping me anywhere near a working sample. First there was lots of trouble to configure the endpoint using a certificate. Later on I received "Enitity too large errors". After that seemed fixed, the latest error is this one :

    "The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:request. The InnerException message was 'There was an error deserializing the object of type TransmissionService.DataContracts.UploadRequest. Unexpected end of file. Following elements are not closed: Content, request, Upload, Body, Envelope. Line 1, position 131073.'.  Please see InnerException for more details."

    Every error that is fixed is replaced by a new error and I'm getting desperate. 

    The Upload(Stream sourceStream) was just simple. Is there no way to include a extra chunk of data holding the filename/path?

    Or maybe somebody has a working sample? (by the way, I googled and tried already plenty of samples). Communication with the service is from a Win 10 universal app.

    Many thanks in advance !

    Steve



    Tuesday, September 27, 2016 9:30 AM

Answers

  • Hi SteveDeclerck,

    As our previous thread discussion, I am afraid an upload class is not supported in Win 10 universal app. You could add a SOAP header or HTTP Header to an outgoing request.

    I suggest you create a custom class called FileInfo which contains FileName and FilePath fields, and then pass this class to Header, you could refer the link below for more information.

    # Add custom Message and HTTP headers to WCF method call in Windows Store apps

    https://code.msdn.microsoft.com/windowsapps/How-to-add-custom-5cbbf066

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, September 28, 2016 6:27 AM

All replies

  • Hi SteveDeclerck,

    As our previous thread discussion, I am afraid an upload class is not supported in Win 10 universal app. You could add a SOAP header or HTTP Header to an outgoing request.

    I suggest you create a custom class called FileInfo which contains FileName and FilePath fields, and then pass this class to Header, you could refer the link below for more information.

    # Add custom Message and HTTP headers to WCF method call in Windows Store apps

    https://code.msdn.microsoft.com/windowsapps/How-to-add-custom-5cbbf066

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, September 28, 2016 6:27 AM
  • Hi Edward,

    I'm afraid I was too fast in the previous thread to say that everything was ok...

    Thanks for replying again, very much appreciated. I will give this a try.

    Thanks and best regards, S.

    Wednesday, September 28, 2016 6:46 AM
  • Hi Steve,

    Have your issue been resolved? If you have any updates, please feel free to let us know.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, October 6, 2016 9:37 AM
  • Hi Edward,

    Thanks for your reply. Sorry for the delay, I just managed today to get back to this issue. Last week, I had another project that needed to be finished.

    I found what is causing the problem: The file size. Things go wrong when the file size reaches 100 KB. I guess that's why I receive the error of elements not closed. It gets truncated if I'm not mistaken. But I don't know why this is happening. These days, 100 KB is nothing anymore.

    Just to include my config file and code :

    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <customErrors mode="Off"/>
        <compilation targetFramework="4.5"/>
        <httpRuntime targetFramework="4.5" executionTimeout="4800" maxRequestLength="500000000"/>
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name = "mexBehavior">
              <!-- 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>
        <bindings>
          <basicHttpBinding>
            <binding name="WSHttpBinding_Upload" 
                     closeTimeout="00:10:00" 
                     maxBufferPoolSize="250000000" 
                     maxReceivedMessageSize="250000000" 
                     openTimeout="00:10:00" 
                     receiveTimeout="00:10:00" 
                     sendTimeout="00:10:00" 
                     messageEncoding="Text">
              <readerQuotas maxDepth="4500000" 
                            maxStringContentLength="4500000" 
                            maxBytesPerRead="40960000" 
                            maxNameTableCharCount="250000000" 
                            maxArrayLength="4500000"/>
            </binding>
          </basicHttpBinding>
        </bindings>
        <protocolMapping>
          <add scheme="http" binding="basicHttpBinding"/>
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
        <services>
          <service name="FileTransferService.FileTransferService" behaviorConfiguration = "mexBehavior">
            <endpoint name="Test" 
                      address="" 
                      binding="basicHttpBinding" 
                      bindingConfiguration="WSHttpBinding_Upload" 
                      contract="FileTransferService.IFileTransferService">
            </endpoint>
          </service>
        </services>
      </system.serviceModel>
      <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="500000000"></requestLimits>
          </requestFiltering>
        </security>
        <modules runAllManagedModulesForAllRequests="true"/>
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true"/>
      </system.webServer>
      <appSettings>
        <add key="IncludeQuotationProjects" value="true"/>
      </appSettings>
    </configuration>
    


    Code to read the file in the W10 Universal App

    uploadRequest.Content = File.ReadAllBytes(FilePath);
    

    Code in the webservice to read the file :

    File.WriteAllBytes("c:\\temp\\test.jpg", request.Content);
    

    I omitted the extra datamember like filename, path, etc....  since it has nothing to do with it.

    Any idea what I'm doing wrong ?

    Many thanks and regards,

    Steve

    Thursday, October 13, 2016 2:25 PM
  • Hi Steve,

    I am glad you find the root issue. Based on your code, it seems you use web request, and send request with file bytes in content, am I right? Did you set the “Content-Length”? Why did not you try WCF client? How did you read file bytes in UWP and call wcf Service? What is the real value for FilePath? As my understanding, you need to use StoreageFile Method to operate file in UWP, you could refer the link below:

    # Skip the path: stick to the StorageFile

    https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/

    To check whether it is related with reading file, writing file or wcf transfer, I suggest you get file from UWP and write the file in UWP to check whether the file is ok.

    If your file is small than 100k, will it work?

    For this issue, I would suggest you post a new thread, and share us above information.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, October 14, 2016 3:37 AM
  • Hi Edward,

    After another struggle, things are finally working now :-)

    Yes, I sticked to the storage file, because accessing the file system seemed another challenge. So I took the most easy solution, the sandbox.

    As requested, I will create a new thread and share my findings. I don't know if the title of the thread will be ok for you.

    Thanks for your help , Steve

    Tuesday, October 18, 2016 1:43 PM