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:
[ServiceContract]
public interface IMyService
{
[OperationContract]
void ReturnArray(byte[] a);
[OperationContract]
byte[] ReturnArrayEx(byte[] a);
}
Here is my concrete service class:
[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:
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:
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?