C# 使用 WCF 服务返回 List<T>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10646978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:38:49  来源:igfitidea点击:

Returning List<T> with WCF service

c#wcf

提问by iJay

I got an Employeeclass and each employee has a list of applied leaves. Is it possible to have the list AppliedLeaveas a [DataMember]in WCF?

我上了一堂Employee课,每个员工都有一份申请休假的清单。是否可以将列表AppliedLeave作为[DataMember]WCF 中的一个?

[DataContract]
public class Employee
{
    [DataMember]
    public string UserID { get; set; }

    [DataMember]
    public int EmployeeNumber { get; set; }

    [ForeignKey("EmployeeUserID")]
    [DataMember]
    public List<Leave> AppliedLeave
    {
        get { return _appliedLeaves; }
        set { _appliedLeaves = value; }
    }

    private List<Leave> _appliedLeaves = new List<Leave>();
    ...
 }

Is there any other way to do this?

有没有其他方法可以做到这一点?

thank you for your consideration of this matter

感谢您对此事的考虑

I extend my Question

我扩展我的问题

This is my Leave Class:

这是我的休假班:

[DataContract]
public class Leave
{

    [Key()]
    [DataMember]
    public Guid LeaveId { get; set; }

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

    [DataMember]
    public DateTime StartDate { get; set; }

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

}

this shows ServiceContract ---->

这显示了 ServiceContract ---->

[ServiceContract]
public interface IEmployeeService
{
    [OperationContract]
    Employee GetEmployeeByUserId(string userId);

    [OperationContract]
    void AssignSupervisor(string userId, string supervisorUserId);

    [OperationContract]
    void DeleteEmployeeByUserId(string userId);

....
}

In Client application,

在客户端应用程序中,

EmployeeServiceClient employeeService = new EmployeeServiceClient();

Employee employee = employeeService.GetEmployeeByUserId(id);

EmployeeServiceClient employeeService = new EmployeeServiceClient();

员工员工 = employeeService.GetEmployeeByUserId(id);

But when Employee gathered from the service its shows Null for leaves,

但是当 Employee 从服务中收集时,它的叶子显示为 Null,

enter image description here

在此处输入图片说明

Can somebody help me? what have I done wrong here?

有人可以帮助我吗?我在这里做错了什么?

采纳答案by Tilak

Yes, it is possible to return generics from WCF service operations.

是的,可以从 WCF 服务操作返回泛型。

But by default they are casted to Array on client side. This can be customized while proxy generation.

但默认情况下,它们在客户端被强制转换为 Array。这可以在代理生成时自定义。

WCF: Serialization and Generics

WCF:序列化和泛型

Also you have to decorate the service with all the types to which generics can be resolved, using KnownTypeAttribute.

此外,您必须使用KnownTypeAttribute 使用泛型可以解析为的所有类型来装饰服务。

Known Types and the Generic Resolver

已知类型和通用解析器

回答by 6opuc

You could use IList<T>instead of List<T>.

您可以使用IList<T>代替List<T>.

回答by Goodies

Changes in my Solution

我的解决方案的变化

I also found my server side list would always arrive at the client as a null pointer. After browsing around a lot for this problem it strikes me it is nearly always denied at first ("your code should work")

我还发现我的服务器端列表总是作为空指针到达客户端。在为这个问题浏览了很多之后,我觉得一开始几乎总是被拒绝(“你的代码应该可以工作”)

Found the issue.. I had configured my solution using one "WCF Service" project and one "Winforms app" project with a generated service reference. Both interface and implementation of Service1 were in the WCF service project, as expected. But any list member returned null.

发现问题.. 我已经使用一个“WCF 服务”项目和一个“Winforms 应用程序”项目配置了我的解决方案,并生成了服务引用。正如预期的那样,Service1 的接口和实现都在 WCF 服务项目中。但是任何列表成员都返回 null。

When I put my IService1.cs = the interface only= in a separate class library instead, reference the class library on both sides (using) and generate the service reference again, my list does work ! The generated code on the client side looks much simpler.

当我将我的 IService1.cs = the interface only= 放在一个单独的类库中时,引用双方的类库(使用)并再次生成服务引用,我的列表确实有效!客户端生成的代码看起来简单多了。

I did not need any special attributes, change service reference configuration, or interface references for this.

为此,我不需要任何特殊属性、更改服务引用配置或接口引用。