Appear in Online Interview to test your skills and be ready for job!
Twitter Twitter LinkedIn YouTube Google
Be Interview ready ! | Search
Make friends on DotNetFunda.com and expand your network!
Winners

Win Prizes

Announcements
Like us on Facebook
Sponsor
Top Articles Author
Thu, 24-Apr-2014 Authors
All Time Authors
Sourav.Kayal
39750
Sheonarayan
34800
Niladri.Biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads

File Tansfer in WCF Service

Sudhakarj21
Posted by under WCF category on for Intermediate level | Views : 16895 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
Rating: 5 out of 5
1 vote(s)


We have many options to transfer file from services. Previously in webservices have MTOM or use byte array with buffer to transfer files. But with WCF it is very easy to transfer file content in a stream format. This article will help you to create such service.

Introduction

Data transfer is one of the common scenario while using services. But in most of the cases we use xml to transfer data in a string type. The same way if you want to transfer binary data you need to convert byte array to Base64 string and in the client side convert back this data to byte array. Below is the simple code to do that

byte[] myData = ...;
string cData = Convert.ToBase64String(myData);
But if we are using WCF Service there is much better way to do that which takes advantage of the channel that is used to communicate. Like NetTCPIP will allow binary data transfer directly and very quickly without encoding. Will discuss this in details about MTOM encoding using http binding in the below sections to transfer binary stream
WCF Service

To create this type of service we must be familiar with Message Contract. Let us create a simple Message Contract which will be used to carry our Binary Stream.

[MessageContract()]
public class FileData
{
[MessageBodyMember(Order = 1)]
public Stream File;
}

Create a simple operation contract using this Message Contract

[ServiceContract()]
public interface IFileTranfer
{
[OperationContract()]
void FileUpload(FileData value);
}

Actual implementation of service class is below

public class ServiceFileTranfer : IFileTranfer
{
public void FileUpload(FileData value)
{
//Access the stream here
StreamReader sr = new StreamReader(value.File);
//sr.ReadToEnd()
}
}
Configuration

Below is the configuration of the WCF Service. The most important is to define binding information. You must configure it to use MTOM to tranfer binary stream so set messageEncofing to Mtom and tansferMode to Streamed. Se the message and buffersize with the maximum stream size that is expected.

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="HttpBinding_MTOM" messageEncoding="Mtom" transferMode="Streamed" maxBufferSize="65536" maxReceivedMessageSize="67108864">
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</basicHttpBinding>
</bindings>

<services>
<service behaviorConfiguration="Service1Behavior"
name="ServiceFileTranfer">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="HttpBinding_MTOM"
contract="IFileTranfer"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

Conclusion
There are many ways to implement such scenarios. But with WCF it can be done in an easy way and the performance will be dependent on the mode that is used.  You can apply this scenario to basicHttpBinding, ws-* bindings.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:




About the Author

Sudhakarj21

Full Name: Sudhakar Kottapalli
Member Level: Bronze
Member Status: Member
Member Since: 10/5/2009 7:05:50 AM
Country:

Login to vote for this post.

Comments or Responses

Posted by: Raja on: 10/7/2009

Awesome post Sudharkar Kottapalli.

I thought its not possible to do file transfer using services and now learnt that its possibile.

Quite cool. Thank you. Keep sharing these types of posts.

Thank you.
Regards


Login to post response.

Comments / Responses
Select text & click toolbar to format. Formatting appears in viewmode only. HTML Tags are not allowed.
Bold Italic Underline Paragraph Title Code  Link 
 Wait ... Processing ..... please wait.

Comment using Facebook