Having the following Server Side WCF service (basichttpbinding) :
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.ServiceModel;
using
System.Runtime.Serialization;
using
System.ServiceModel.Description;
namespace
TestHttpService
{
[
ServiceContract(Namespace = "http://ProgrammingHelp.Service/")]
[
ServiceKnownType(typeof(string[]))]
public interface ITest
{
[
OperationContract]
void Test(Sample s);
}
[
DataContract]
[
KnownType(typeof(string[]))]
public class Sample
{
[
DataMember]
object dummy;
[
DataMember]
public Dictionary<string, object> Dic {get; set;}
}
class Test1 : ITest
{
public void Test(Sample s)
{
foreach (KeyValuePair<string, object> i in s.Dic)
{
Console.WriteLine("Key: {0}, Type : {1}", i.Key, i.Value.GetType());
}
}
}
class Program
{
static void Main(string[] args)
{
Uri baseAddr = new Uri("http://localhost:8002/TestService1");
ServiceHost localHost = new ServiceHost(typeof(Test1), baseAddr);
try
{
localHost.AddServiceEndpoint(
typeof(ITest), new BasicHttpBinding(), "TestService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled =
true;
localHost.Description.Behaviors.Add(smb);
localHost.Open();
Console.WriteLine("Service initialized.");
Console.WriteLine("Press the ENTER key to terminate service.");
Console.ReadLine();
localHost.Close();
}
catch (CommunicationException ex)
{
Console.WriteLine("Oops! Exception: {0}", ex.Message);
localHost.Abort();
}
}
}
}
And the following client :
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
TestHttpServiceClient
{
class Program
{
static void Main(string[] args)
{
TestHttpSvc.
Test1 t = new TestHttpServiceClient.TestHttpSvc.Test1();
TestHttpSvc.
Sample s = new TestHttpServiceClient.TestHttpSvc.Sample();
s.Dic =
new TestHttpServiceClient.TestHttpSvc.ArrayOfKeyValueOfstringanyTypeKeyValueOfstringanyType[2];
s.Dic[0] =
new TestHttpServiceClient.TestHttpSvc.ArrayOfKeyValueOfstringanyTypeKeyValueOfstringanyType();
s.Dic[1] =
new TestHttpServiceClient.TestHttpSvc.ArrayOfKeyValueOfstringanyTypeKeyValueOfstringanyType();
s.Dic[0].Key =
"test0";
s.Dic[1].Key =
"test1";
s.Dic[0].Value =
"bye";
s.Dic[1].Value =
"bye1";
t.Test(s);
s.Dic[0].Value =
new string[2] { "chair", "desk" };
s.Dic[1].Value =
new string[2] { "pen", "pencil" };
t.Test(s);
}
}
}
The client fails with exception in deserialization at the 2nd try to call the service, due to the fact that the dictionary is now populated with string[] rather than simple time
I fail to understand what is wrong here.
Note that I use the WellKnown attribute, and if I use a client with AddServiceReference rather than AddWebReference things are working quite fine.
The problem is that I need to be backward compatiable to .NET 2.0 clients.