WCF call with Param List<T> fails when passing a List<MyType>

WCF Service INTERFACE:

[ServiceContract]
public interface ITest
{
    [OperationContract]
    int TestCall(GenericType<MyType> x);

    [OperationContract]
    int TestAnotherCall(GenericType<MyOtherType> x);

}

[DataContract(Name = "GenericType")]
[KnownType(typeof(List<MyType>))]
[KnownType(typeof(List<MyOtherType>))]
public class GenericType<T>
{
    [DataMember]
    public List<T> Data
    {
        get { return data; }
        set { data = value; }
    }
}

WCF Service IMPLEMENTATION:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Test : ITest
{
    public int TestCall(GenericType<MyType> x)
    {
        return x.Data.Count; 
    }

    public int TestAnotherCall(GenericType<MyOtherType> x)
    {
        return x.Data.Count; 
    }
}

CLIENT

List<MyType> list = from a in ctx.Table
                    select new MyType (a.Field1, a.Field2, a.Field3).ToList(); 

GenericType gt = new GenericType();
gt.Data = list;

using(WCFClient client = new WCFClient())
{
   client.TestCall(gt);
   client.Close();
}

ERROR: The remote server returned an unexpected response: (400) Bad Request.

if I pass NULL to "gt.Data" ...it works fine.

NOTE:

When I put the mouse over the gt.Data ...the hint shows as MyType[] Not sure if that's expected.

After some review, I noticed that the Client Service only knows about the 1st [KnownType] stated, in my case the List. No knowledge of List .... Is that expected when you put various [KnownType] on the WCF Interface?

Answers


You need to decorate your generic with the KnownType() attribute

[DataContract(Name = "GenericType")]
[KnownType(typeof(MyType))]
public class GenericType<T>
{
    [DataMember]
    public List<T> Data
    {
        get { return data; }
        set { data = value; }
    }
}

Quick Working Example:

Service

[OperationContract]
GenericType<MyType> GetDataUsingDataContract(GenericType<MyType> composite);

public class Service1 : IService1
{
    public GenericType<MyType> GetDataUsingDataContract(GenericType<MyType> composite)
    {
        composite.Data.First().Stuff = "Test";
        return composite;
    }
}

Model

[DataContract(Name = "GenericType")]
[KnownType(typeof (MyType))]
public class GenericType<T>
{
    [DataMember]
    public List<T> Data { get; set; }
}

public class MyType
{
    public string Stuff { get; set; }
}

Client

var client = new Service1Client();

var genericType = new GenericType
                        {
                            Data = new[]
                                        {
                                            new MyType(),
                                        }
                        };
var result = client.GetDataUsingDataContract(genericType);
client.Close();

Console.WriteLine(result.Data.First().Stuff);

Console.ReadLine();

this example was generated with adding a service reference and not using a shared assembly


Need Your Help

Horizontally align UILabel and UIImageView together inside of a full width UIView

ios uiview autolayout interface-builder

I have a UIView in my layout that is pinned to the sides of my VC. Inside of that I have an icon UIImageView and a UILabel that is next to that. I would like both of those to be center aligned insi...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.