将数组传递给WCF函数
本文关键字:WCF 函数 数组 | 更新日期: 2023-09-27 17:59:47
因此,我有一个C#项目,在该项目中,我使用Linq到XML加载一个XML文档(包含学生的姓名和Id),并且我必须从WCF服务获取相关数据(截止日期、金额等)。我只需右键单击并添加服务引用就添加了服务,现在需要将数组传递给GetData函数,我初始化了该函数,但它显然为null。我无法将数组转换为服务类型,函数也返回数组。如何将数组分配给studentArray?
ServiceReference1.ServiceClient client = new ServiceReference1.RycorServiceClient();
Application.ServiceReference1.Student[] studentArray = new ServiceReference1.Student[9];
Student[] array = studentList.ToArray();
//for (int i = 0; i <= array.Count(); i++)
// studentArray[i] = (RycorApplication.ServiceReference1.Student)array[i];
//this gives me an error says Cannot convert type 'Application.Student' to 'Application.ServiceReference1.Student'.
var data = client.GetData(studentArray);
获取这些数据后,如何将这些数据保存到XML文件中?
您收到此错误是因为Application.Student是不同的类型,您可以尝试使用Application.ServiceReference.students来保存学生列表,而不是"studentList"类型。
我假设"studentList"是一个"Application.Student"列表,您必须使用相同的模型,或者在它们之间使用类似的东西进行复制(在第一个答案中):将值从一个对象复制到另一个
你几乎必须这样做:
List<ServiceReference1.Student> wcfStudentList = new System.Collections.Generic.List<ServiceReference1.Student>();
foreach (var student in studentList)
{
wcfStudentList.Add(new ServiceReference1.Student()
{
ID = student.ID,
Name = student.Name,
..etc..
});
}
var data = client.GetStudentData(wcfStudentList.ToArray());
我不得不问,如果可以的话,为什么不直接更改WCF调用,以获取学生ID的列表,而不是传递整个对象?