Click here to Skip to main content
15,887,425 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
Hi, I have created a WCF services which is used by Win32 application. I have added a service to upload and download the large file via WCF services, to transfer a file I am using byte[] array. because my files are stored in the database. I am passing an object of class which have following data fields.

C#
public long FileID;
public string FileName;
public string FileMIME;
public byte[] FileStream;


and few more field which are type of String but they are not related with Uploading file.
The problem with the service is that i'm not able to upload file size more than 16kb, however i am able to download a file of 100kb+. I have googled a lot about this problem and modified my web.config and app.config file as follow.

web.config file

XML
<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="ITR1_Entity" connection string=""connection string";MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
    <add name="CAIS_Entity" connectionString="connection string";MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpRuntime maxRequestLength="2147483647" />
  </system.web>
  <system.serviceModel>
 
    <bindings>
 
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ICAIS__AdvisiorWebService" closeTimeout="01:10:00"
                 openTimeout="01:10:00" receiveTimeout="01:10:00" sendTimeout="01:10:00"
                 allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                 maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
                 messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                 useDefaultWebProxy="true">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
           maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
             realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint name="BasicHttpBinding_ICAIS__AdvisiorWebService" address="http://localhost:1964/CAIS_AdvisiorWebService.svc"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ICAIS__AdvisiorWebService"
                contract="ICAIS_AdvisiorWebService">        
      </endpoint>
      <endpoint name="BasicHttpBinding_IITR1_WebService" address=""
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ITR1_WebService"
                contract="IITR1_WebService">
        
      </endpoint>
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="customeBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!--To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment-->
          <serviceMetadata httpGetEnabled="true" />
          <!--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="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
</configuration>


here is my app.config file
XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="DataConnection" connectionString="Data Source=.\SQL2K8;Initial Catalog=HnR_1.0;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IITR1_WebService" closeTimeout="10:01:00"
          openTimeout="10:01:00" receiveTimeout="10:01:00" sendTimeout="10:01:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
            maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
        <binding name="BasicHttpBinding_ICAIS__AdvisiorWebService" closeTimeout="10:01:00"
          openTimeout="10:01:00" receiveTimeout="10:01:00" sendTimeout="10:01:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
            maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
        <binding name="BasicHttpBinding_ICAIS_ManagerWebService1" closeTimeout="10:01:00"
          openTimeout="10:01:00" receiveTimeout="10:01:00" sendTimeout="10:01:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
          useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
            maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:1964/ITR1_WebService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IITR1_WebService"
        contract="WebServiceITR1.IITR1_WebService" name="BasicHttpBinding_IITR1_WebService" />
      <endpoint address="http://localhost:1964/CAIS_AdvisiorWebService.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICAIS__AdvisiorWebService"
        contract="WebServiceEmpLogin.ICAIS__AdvisiorWebService" name="BasicHttpBinding_ICAIS__AdvisiorWebService" />
      <endpoint address="http://localhost:1964/CAIS_ManagerWebService1.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICAIS_ManagerWebService1"
        contract="WebServiceManager.ICAIS_ManagerWebService1" name="BasicHttpBinding_ICAIS_ManagerWebService1" />
    </client>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>


the service "WebServiceEmpLogin.ICAIS__AdvisiorWebService" has that function which ll let my client upload and download the file.

please help me to upload and download large file with wcf service.
Thank you
Posted

You increase the timeout period in web.config


XML
<system.web>
  <httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>
 
Share this answer
 
Comments
Kalpesh Bhadra 13-May-13 6:30am    
it throws exception i.e.:- "remote server returned an unexpected responce:(400) bad request
@sixthplanets: thank you for help. I have gone through all this article and after that i have modified my web.config and app.config a bit.
i.e.: i have changed my
HTML
transferMode="Buffered" to transferMode="Streamed" 

and this throws an error for bad request 400


here i am sharing my WCF function with you
C#
public CAIS_FileAttachmentInfoBO downloadfile(long FileID) 
var objAttachment_DAL = objEntity_DAL.FileAttachment1
                                     .Where(us => us.FileID== FileID)
                                     .Select(us => new
                                     {
                                     us.FileID,
                                     us.FileMIME,
                                     us.FileName,                                                                                                                                        
                                     us.FileStream
                                     })
                                     .FirstOrDefault(); 
 CAIS_FileAttachmentInfoBO objFileAttachmentBO =
                                     new CAIS_FileAttachmentInfoBO();
                    if (objAttachment_DAL != null && objAttachment_DAL.FileID > 0)
                    {
                        objFileAttachmentBO.FileName = objAttachment_DAL.FileName;
                        objFileAttachmentBO.FileStream = objAttachment_DAL.FileStream;
                        objFileAttachmentBO.Is_Password = false;
                        objFileAttachmentBO.Password = "";
                        objFileAttachmentBO.FileID = objAttachment_DAL.FileID;
                    }
                        return objFileAttachmentBO;

here "CAIS_FileAttachmentInfoBO" is my class which has the 5 data field as used in function.
I don't think that the problem with my function

thank you. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900