Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将对象列表发送到WCF服务?_C#_Wcf_Asp.net 4.0_Wcf Data Services_Wcf Client - Fatal编程技术网

C# 如何将对象列表发送到WCF服务?

C# 如何将对象列表发送到WCF服务?,c#,wcf,asp.net-4.0,wcf-data-services,wcf-client,C#,Wcf,Asp.net 4.0,Wcf Data Services,Wcf Client,我正在构建WCF服务,我想接受列表作为我的一个方法的参数 这是我的密码: [ServiceContract] public interface IProductService { [OperationContract] int InsertProducts(List<Product> products); } [DataContract] [KnownType(typeof(List<Product>))] public class Product {

我正在构建WCF服务,我想接受列表作为我的一个方法的参数

这是我的密码:

[ServiceContract]
public interface IProductService
{
    [OperationContract]
    int InsertProducts(List<Product> products);
}

[DataContract]
[KnownType(typeof(List<Product>))]
public class Product
{
    [DataMember]
    public int ProductId{ get; set; }

    [DataMember]
    public string ProductName{ get; set; }

    [DataMember]
    public List<Product> Products { get; set; }
}
[服务合同]
公共接口IPProductService
{
[经营合同]
int InsertProducts(列出产品);
}
[数据合同]
[知识类型(类型(列表))]
公共类产品
{
[数据成员]
public int ProductId{get;set;}
[数据成员]
公共字符串ProductName{get;set;}
[数据成员]
公共列表产品{get;set;}
}
当我运行服务时,它会给我一个错误


WCF中不支持此操作,因为它使用
名称空间.Product[]

通过WCF发送通用列表时,我们始终创建一个具有属性的类,该属性为列表

[ServiceContract] 
public interface IProductService 
{ 
    [OperationContract] 
    int InsertProducts(MyListofProducts products); 
 } 


[DataContract] 
public class  MyListofProducts
{

    [DataMember] 
    List<Product> Products { get; set; }

 }

[DataContract] 
public class Product 
{ 
    [DataMember] 
    public int ProductId{ get; set; } 

    [DataMember] 
    public string ProductName{ get; set; } 

    [DataMember] 
    public List<Product> Products { get; set; } 
} 
[服务合同]
公共接口IPProductService
{ 
[经营合同]
int InsertProducts(MyListofProducts产品);
} 
[数据合同]
公共类MyListofProducts
{
[数据成员]
列出产品{get;set;}
}
[数据合同]
公共类产品
{ 
[数据成员]
public int ProductId{get;set;}
[数据成员]
公共字符串ProductName{get;set;}
[数据成员]
公共列表产品{get;set;}
} 

您不需要[KnownType(typeof(List))]我尝试了您建议的方法。我也犯了同样的错误。我正在使用WCF测试客户端,这会是问题吗?也许it接口不支持列表?还有一个可能的问题,那就是您的产品中包含产品。您可以尝试使用子产品类型的产品,如果我从控制台应用程序调用服务,我可以发送列表。只是它不能与WCF测试客户端正常工作,那么区别在于配置文件。确保confif设置位于测试项目的app.config中。