Step 1. Create a New WCF Service Project (WCF Service Library)

  1. On the File menu, click New Project.
  2. In the New Project dialog box under Project types, expand Visual C#, click WCF, and select WCF Service Library. In the Name box, type "DemoMultipartWCF". In the Solution name, type "WCFDemo" and then click OK.
  3. Now, delete the default created Interface, Class perspective "IService1.cs" and "Service1.cs".
  4. Add a New Interface by right-clicking on DemoMultipartWCF(project) > Add > New Item and selecting "WCF Service". Name it as "Users. svc", and click ADD.

Step 2. Create an Interface and implement its body.

  1. Create a New Interface in IUsers.cs File (Paste the code, given below)
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.WrappedResponse,
        UriTemplate = "UpdateUserDetail"
    )]
    bool UpdateUserDetail(Stream stream);
    
  2. Implement the body of the UpdateUserDetail Interface in Users.svc file (Paste the code, given below)
    public bool UpdateUserDetail(Stream stream)  
    {  
    return false;  
    }  

Step 3. Create a MultipartParser to read the requested form data

Create a New folder named "DataContract" in your project, and add the "CommonDC" class to the folder. Paste the code, given below, in the "CommonDC.cs File" class.

Uses Of All Below Method

Step 4. Implement Pre-Required Method like SaveImageFile & GetEncoder.

Create a Method to save the image file.

public static bool SaveImageFile(byte[] ImageFileContent, string ImagePathWithImageName)  
{  
    try {  
        //Require Namespace: using System.Drawing  
        Image image;  
        //Read Image File  
        using(MemoryStream ms = new MemoryStream(ImageFileContent)) {  
            image = Image.FromStream(ms);  
        }  
        Bitmap bmp = new Bitmap(image);  
        //Require Namespace: System.Drawing.Imaging  
        ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);  
        //We need to write Encoder with Root otherwise it is an ambiguous between "System.Drawing.Imaging.Encoder" and "System.Text.Encoder"  
        System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;  
        EncoderParameters myEncoderParameters = new EncoderParameters(1);  
        EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 40 L);  
        myEncoderParameters.Param[0] = myEncoderParameter;  
        bmp.Save(ImagePathWithImageName, jgpEncoder, myEncoderParameters);  
        return true;  
    } catch {  
        //Do Code For Log or Handle Exception   
        return false;  
    }  
}  

Note. We need to write Encoder with Root, elsetheret is an ambiguity between "System.Drawing.Imaging.Encoder" and "System.Text.Encoder".

Encoder

Create a method to retrieve the image codecs.

public static ImageCodecInfo GetEncoder(ImageFormat format)  
{  
    ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();  
    foreach(ImageCodecInfo codec in codecs) {  
        if (codec.FormatID == format.Guid) {  
            return codec;  
        }  
    }  
    return null;  
}  

What is the use of ImageCodecInf?

It provides the necessary storage members and methods to retrieve all the pertinent information about the installed image codecs.

Step 5. Implement our interface - UpdateUserDetail

Now, it's time to implement our interface - UpdateUserDetail. (Paste the code, given below)

public bool UpdateUserDetail(Stream stream) {  
    try {  
        //Create an Object of byte[]  
        byte[] buf = new byte[10000000];  
        //Initialise an Object of MultipartParser Class With Requested Stream  
        MultipartParser parser = new MultipartParser(stream);  
        //Check that we have not null value in requested stream  
        if (parser != null && parser.Success) {  
            //Fetch Requested Formdata (content)   
            //(for this example our requested formdata are UserName[String])  
            foreach(var item in parser.MyContents) {  
                    //Check our requested fordata  
                    if (item.PropertyName == "UserName") {  
                        string RequestedName = item.StringData;  
                    }  
                }  
                //Create a GUID for Image Name  
            string ImageName = Guid.NewGuid().ToString();  
            //Image Path  
            string SaveImagePath = "D:/DemoProject/DemoMultipartWCF/DemoMultipartWCF/MyFile/";  
            //Ensure That WE have the right path and Directory  
            if (!Directory.Exists(SaveImagePath)) {  
                //If Directory Not Exists Then Create a Directory  
                Directory.CreateDirectory(SaveImagePath);  
            }  
            //Fetch File Content & Save that Image HERE (for this example our requested FileContent is ProfilePicture[File])  
            string ImagePathWithImageName = SaveImagePath + ImageName + ".bmp";  
            SaveImageFile(parser.FileContents, ImagePathWithImageName);  
            return true;  
        }  
        return false;  
    } catch {  
        //Do Code For Log or Handle Exception   
        return false;  
    }  
}  

Step 6. Ensure the following setting

Now, all is done. Ensure you have the following setting in your Web .config.

<system.serviceModel>  
    <bindings>  
        <basicHttpBinding>  
            <binding maxReceivedMessageSize="999999999" maxBufferSize="999999999" maxBufferPoolSize="9999999999" name="ITransactionProcessor">  
                <readerQuotas maxDepth="32" maxArrayLength="999999999" maxStringContentLength="999999999" />  
                <security mode="TransportWithMessageCredential" /> </binding>  
        </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="true" /> </behavior>  
        </serviceBehaviors>  
        <endpointBehaviors>  
            <behavior name="web">  
                <webHttp helpEnabled="true" /> </behavior>  
        </endpointBehaviors>  
    </behaviors>  
    <protocolMapping>  
        <add binding="basicHttpsBinding" scheme="https" /> </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  
    <services>  
        <service name="DemoMultipartWCF.Users">  
            <endpoint name="UsersXML" address="" binding="basicHttpBinding" contract="DemoMultipartWCF.IUsers"></endpoint>  
            <endpoint name="UsersJSON" address="json" binding="webHttpBinding" contract="DemoMultipartWCF.IUsers" behaviorConfiguration="web"></endpoint>  
        </service>  
    </services>  
    <client>  
        <endpoint address="https://ics2wstest.ic3.com/commerce/1.x/transactionProcessor" binding="basicHttpBinding" bindingConfiguration="ITransactionProcessor" contract="com.cybersource.api.ITransactionProcessor" name="portXML" /> </client>  
</system.serviceModel>  

Now, it's complementary time.

How to test this WCF Service?

WCF Service

Select POST Method.

POST

Select Body and Fill Form Data. (Requested Data: Username & Requested File: ProfilePicture [browse and select the image]).

Body

Now, just hit Send and here we go.