【问题标题】:WCF Service collection type to Generic List instead of an arrayWCF 服务集合类型为通用列表而不是数组
【发布时间】:2014-05-21 13:19:04
【问题描述】:

我想知道是否有办法以编程方式将集合类型配置为通用列表而不是客户端上的任何数组。我知道如果我们添加服务参考并转到我们可以在那里指定的高级设置。但是该项目的设计方式是我们为合约提供了单独的程序集,并被服务器和客户端引用,并且模型类型在同一个项目中定义,服务和客户端都使用它们。因此,我们不是添加服务引用,而是通过以编程方式定义绑定和端点来完成代码中的所有事情。我想知道这是否可以在代码中轻松实现,而无需编写代码将数组转换为客户端上的通用列表。

谢谢, 阿杰

【问题讨论】:

  • 你是如何生成代码的?
  • 我们没有进行代理类生成。所有 POCO 对象和服务合同都在一个单独的程序集中,这些程序集被服务和客户端项目引用。
  • 我已经查看了该问题,答案表明 2 种方法之一是更新 reference.svcmap,因为我们没有向客户端应用程序添加服务引用,因此我们将不会拥有它。另一种方法建议使用 IList 而不是 ICollection。我们生成的 POCO 是使用 EF6 的 T4 模板,我们要么必须修改 T4 模板以将 ICollection 更改为 IList。我一直在寻找客户端的解决方案,而不是修改 T4 模板。但是我会通过手动将 ICollection 更改为 IList 来尝试一下,然后先尝试
  • 将 ICollection 更改为 IList 也无济于事。我仍然得到一个数组

标签: c# wcf


【解决方案1】:

这是由于 DataContractSerializer 将 GenericLists 转换为数组。为了处理这个问题,我必须拦截对象的反序列化过程并将它们转换回列表。我提供了我的类定义和部分类定义,以使我的答案更容易理解。

类定义:

public partial class provider
{
    public provider()
    {
        this.provider_contracts = new HashSet<provider_contracts>();
        this.provider_clinics = new HashSet<provider_clinics>();
        this.provider_custom_field_validations = new HashSet<provider_custom_field_validations>();
        this.provider_directory_languages = new HashSet<provider_directory_languages>();
        this.provider_directory_services = new HashSet<provider_directory_services>();
        this.provider_employees = new HashSet<provider_employees>();
        this.provider_field_exceptions = new HashSet<provider_field_exceptions>();
        this.provider_languages = new HashSet<provider_languages>();
        this.provider_required_fields = new HashSet<provider_required_fields>();
        this.provider_users = new HashSet<provider_users>();
        this.user_privileges = new HashSet<user_privileges>();
    }

    public virtual ICollection<provider_contracts> provider_contracts { get; set; }
    public virtual ICollection<provider_clinics> provider_clinics { get; set; }
    public virtual ICollection<provider_custom_field_validations> provider_custom_field_validations { get; set; }
    public virtual ICollection<provider_directory_languages> provider_directory_languages { get; set; }
    public virtual ICollection<provider_directory_services> provider_directory_services { get; set; }
    public virtual ICollection<provider_employees> provider_employees { get; set; }
    public virtual ICollection<provider_field_exceptions> provider_field_exceptions { get; set; }
    public virtual ICollection<provider_languages> provider_languages { get; set; }
    public virtual provider_options provider_options { get; set; }
    public virtual ICollection<provider_required_fields> provider_required_fields { get; set; }
    public virtual ICollection<provider_users> provider_users { get; set; }
    public virtual ICollection<user_privileges> user_privileges { get; set; }
}

我的部分班级来处理这个问题:

public partial class provider
{
    [OnDeserialized()]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        if (provider_contracts != null) provider_contracts = provider_contracts.ToList();
        if (provider_clinics != null) provider_clinics = provider_clinics.ToList();
        if (provider_custom_field_validations != null) provider_custom_field_validations = provider_custom_field_validations.ToList();
        if (provider_directory_languages != null) provider_directory_languages = provider_directory_languages.ToList();
        if (provider_directory_services != null) provider_directory_services = provider_directory_services.ToList();
        if (provider_employees != null) provider_employees = provider_employees.ToList();
        if (provider_field_exceptions != null) provider_field_exceptions = provider_field_exceptions.ToList();
        if (provider_languages != null) provider_languages = provider_languages.ToList();
        if (provider_required_fields != null) provider_required_fields = provider_required_fields.ToList();
        if (provider_users != null) provider_users = provider_users.ToList();
        if (user_privileges != null) user_privileges = user_privileges.ToList();
    }
}

有用的资源:@​​987654321@

我也在 MSDN 论坛上回答了这个问题,并在下面添加了一个链接 "http://social.msdn.microsoft.com/Forums/vstudio/en-US/5ccb534c-e310-48d4-8345-160c06568220/collection-types-when-using-channelfactory-without-servicereference?forum=wcf"

【讨论】:

    猜你喜欢
    • 2012-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多