【发布时间】:2011-12-02 06:30:27
【问题描述】:
WCF 服务接口:
[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 服务实现:
[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;
}
}
客户
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();
}
错误:
远程服务器返回了意外响应:(400) Bad Request。
如果我将 NULL 传递给“gt.Data”......它工作正常。
注意:
当我将鼠标放在 gt.Data ...提示显示为 MyType[]
不确定这是否符合预期。经过一番审查,我注意到客户服务只知道
第一个 [KnownType] 声明,在我的情况下是列表。 不知道 List ....
当您在 WCF 接口上放置各种 [KnownType] 时,这是预期的吗?
【问题讨论】: