Shimon
Shimon

Reputation: 13

WCF service is receiving null value from client

I wrote a service that implements functions performing video process.

From the client (console project), I call a function of the service, using the client service reference, and send as parameter to the function a FileStream (I verified that indeed it got correct value on client side).

But when the FileStream parameter gets to the service - I get null exception problem, with no correct value inside the FileStream.

How can I resolve it?

My code:

Service:

public class VideoProcess : IVideoProcess
{
    public void UploadVideo(int VideoPartNumber, FileStream videoFile, Guid ApplicatId, Guid TransactionCode)
    {
    }
}

My client:

 FileStream videoFile = new FileStream(@"C:\VJob\gizmo.mp4", FileMode.Open, FileAccess.Read);     

//vpc id the client service reference
vpc.UploadVideo(2222, videoFile, new Guid("324792c9-d43c-4e38-8f94-7fc0ed2d7492"), Guid.NewGuid());

Upvotes: 1

Views: 1431

Answers (2)

BigBoss
BigBoss

Reputation: 63

Do not use FileStream as parameter in WCF. A FileStream is a stream bound to the local file system, so you would get NullReferenceException in server side Although you really send a corret FileStream object from client. My sugguestion as follow:

Use byte[] as WCF parameter > write File in server side > read file locally

client side:

    // Use byte[] as WCF parameter
    FileInfo fileInfo = new FileInfo(path);
    long length = fileInfo.Length;
    FileStream fileStream = new FileStream(path, FileMode.Open);
    byte[] buffer = new byte[length];
    fileStream.Read(buffer, 0, (int)length);
    fileStream.Close();

    UploadVideo(buffer);

server side:

    // write File in server side
    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddHHmmss"));

    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);
    }

    string filePath = Path.Combine(path, "FileName");

    File.WriteAllBytes(filePath , excelByte);

    // read file locally
    using (FileStream fileStream = new FileStream(filePath , FileMode.Open, FileAccess.Read))
    {
        // TO DO
    }

Upvotes: 0

Mert Gülsoy
Mert Gülsoy

Reputation: 2919

When you receive wcf request in your service, the FileStream object is serialized then deserialized to a new object and this new object will be a Stream, not a FileStream. Stream object does not have a Name property. From another point FileStream is backed up by a local file system. Since it is obvious to send the file contents to remote service, not the file system, it is not logical to send the Name property.

If your service app depends on the Name property, then you can send the name data to service with another parameter like:

public class VideoProcess : IVideoProcess
{
    public void UploadVideo(int VideoPartNumber, Stream videoData, String videoFileName, Guid ApplicatId, Guid TransactionCode)
    {
    }
}

or create a model then use it like:

public class VideoPart {
    public Stream data {get;set;}
    public String Name {get;set;}
    public int VideoPartNumber {get;set;}
}
//then the server method signature would be
//...
public class VideoProcess : IVideoProcess
{
    public void UploadVideo(VideoPart part, Guid ApplicatId, Guid TransactionCode)
    {
        // ... some process ...
    }
}

Upvotes: 1

Related Questions