Transfer large file using WCF

Transfer large file using WCF

Hello friends, today I will explain you regarding common problem of WCF configuration for how to transfer large file using WCF. We will create an example from scratch to understand each configurations. Some users also get “There was no endpoint listening at http://localhost:57908/Service1.svc that could accept the message. This is often caused by an incorrect address or SOAP action.“ kind of error. We could also resolve this kind of errors code.

First of all create “WCF Service Application” project from Visual studio. Add below code:
In IService1.cs

[ServiceContract]
public interface IService1
{
    [OperationContract]
    bool GetFileData(string value);
}

In Service1.svc.cs

public class Service1 : IService1
{
    public bool GetFileData(string value)
    {
        try
        {
            File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "MyLargeFile.txt", value);
            return true;
        }
        catch { return false; }
    }
}

In Web.config

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <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>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <bindings>
      <basicHttpBinding>
        <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service  name="WcfLargeFileTransfer.Service1" behaviorConfiguration="" >
        <endpoint address="" binding="basicHttpBinding" contract="WcfLargeFileTransfer.IService1" >
          <identity>
            <dns value="" />
          </identity>
        </endpoint>
      </service>
    </services>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2000000000" />
      </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>

</configuration>

Now its time to create client and call “GetFileData” method from Client application.

class Program
{
    static void Main(string[] args)
    {
        try
        {
            serviceLargeFIle.IService1 ObjServ = new serviceLargeFIle.Service1Client();
            //Try to upload any large file
            bool isUploaded = ObjServ.GetFileData(System.IO.File.ReadAllText(@"E:\LargeFile.txt")); // size of 85.9 MB
        }
        catch (Exception ex){}
    }
}

This is how we could upload large file upto 2 GB using WCF from client.

Download Demo code for Transfer large file using WCF

 

Leave a Reply