Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I cannot return an array from a WCF service. Any values assigned in my service are not returned to the client.

Here is my service interface:

C#
[ServiceContract]
public interface IMyService
{
   [OperationContract]
   void ReturnArray(byte[] a);

   [OperationContract]
   byte[] ReturnArrayEx(byte[] a);
}


Here is my concrete service class:

C#
[KnownType(typeof(byte[]))]
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class MyService : IMyService
{
   public void ReturnArray(byte[] a)
   {
      a[0] = 11;
      a[1] = 21;
   }

   public byte[] ReturnArrayEx(byte[] a)
   {
      byte[] temp = a;

      a[0] = 33;
      a[1] = 44;

      return a;
   }
}


I am self-hosting my service with the following code:

XML
string address;
ChannelFactory<ChannelTest.IMyService> channelFactory;

BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.None; // None is the default

address = @"http://localhost:12345/MyService";
channelFactory = new ChannelFactory<ChannelTest.IMyService>(binding, address);
m_service = channelFactory.CreateChannel();


I invoke my service as follows:

C#
byte[] myArray = new byte[5];
m_service.ReturnArray(myArray);


On return, my array still contains its default zeroes rather than 11 and 21 assigned in the service.

An article suggested that the second method (ReturnArrayEx) is the way to return arrays, but there has to be a better way.

Do I need to assign more KnownTypes? Are there other attributes I must set? Is there a setting somewhwere I need to change?
Posted
Comments
kalkwarf 9-Jun-13 15:16pm    
Perhaps a poor choice of names and words. Pretend it is named FillMyArray, if you like. Further reading of the question would have made the problem clear.

The code shown is just an example of my problem, it is not alone intended to make 'sense'. I have tried to boil the issue down to a representative example rather than burden everyone with loads of irrelevant code. The issue is the values assigned in the service are not making it back to the client.

First problem, without reading the rest of your question: the method ReturnArray does not return any arrays. It simply modifies two first elements of some existing array, and only if this array was initialized to have at least two elements, otherwise the assignment operation throws exception. Get rid of this method completely; it makes no sense, not because of its implementation, but due to its very signature.

—SA
 
Share this answer
 
The solution was simple. The method signature should be as follows:

void ReturnArray(ref byte[] a);



Note that 'ref' was added.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900