Returning a byte[] from a WCF service method

I am writing a WCF service that also needs to be able to support legacy clients. I want to send a file from the service to the client. The service method returns an object, lets call it a ServiceRtn object. The ServiceRtn class has a data member named FileBytes declared as a byte[]. The service uses File.ReadAllBytes to populate FileBytes. When stepping through the code I can see that FileBytes is populated correctly. The client assigns the return value of the service method to a ServiceRtn object. All of the other data members of the ServiceRtn object are as expected, but FileBytes is null. Can somebody tell me what I'm doing wrong? Thanks.
Paracom_IncAsked:
Who is Participating?
 
surajgupthaConnect With a Mentor Commented:
>>Have u made sure the client and the server classes are in sync?  can you update the service reference and try again?

>> can you check to see if there are any exceptions/ warnings logged by enabling logging?
0
 
Paracom_IncAuthor Commented:
The client and server share the same class library so there is no service reference to update. For initial testing purposes, I'm using a WCF client application. No exceptions are thrown. I'd be happy to enable logging but I don't know how to do it. What kind of logging did you want me to enable?
0
 
surajgupthaCommented:
Might you be having any code in your services that might be destroying the file object from where you read all the bytes?? Can you try commenting that code?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Paracom_IncAuthor Commented:
I'm not sure I understand your question.
The server side reads the file and returns the object that contains the byte array:
serviceRtn.FileBytes = File.ReadAllBytes(filePath);
return serviceRtn;

The client side calls the service method and assigns the return value to a ServiceRtn object:
serviceRtn = proxy.DownloadFile();

At that point, on the client side, serviceRtn.FileBytes is null. If I check serviceRtn.FileBytes on the service side before the method returns, it contains the expected bytes. I'm not doing anything else with the source file, but once the array is filled is shouldn't make a difference anyway.
0
 
surajgupthaCommented:
Did you add the FileBytes property recently into the class?
0
 
Paracom_IncAuthor Commented:
OK, so it was a bonehead/newbie mistake. You were on the right track when you asked about the contract. I had not decorated the FileBytes property with the DataMember attribute so it was not being marshalled by WCF. Since the client and service are sharing the same class library that defines the contract the client could see the FileBytes property but since it wasn't marked as a DataMember its value remained null. Thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.