• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

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.
0
Paracom_Inc
Asked:
Paracom_Inc
  • 3
  • 3
1 Solution
 
surajgupthaCommented:
>>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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now