【问题标题】:WCF not implementing data annotationWCF 未实现数据注释
【发布时间】:2017-09-22 08:53:24
【问题描述】:

我有一个 N 层解决方案。它有四个项目,如下所示:

  1. 基础设施(模型类)
  2. 存储库
  3. 服务 (WCF)
  4. 网络(演示文稿)

基础设施负责模型类

基础设施

using System.Runtime.Serialization;
namespace Infrastructure
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using DataAnnotationsExtensions;

[Serializable]
[DataContract(IsReference = true)]
public partial class COUNTRIES
{
    public COUNTRIES()
    {
        this.CITIES = new HashSet<CITIES>();
        this.LGA = new HashSet<LGA>();
        this.STATES = new HashSet<STATES>();
    }

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

    // [DataMember(Name = "Country Code")]
    [DataMember]
    [Required(ErrorMessage = "Country Code is required")]
    [Display(Name = "Country Code")]
    [StringLength(2, ErrorMessage = "The {0} must be at least {1} characters long. Plese check again!", MinimumLength = 2)]
    //[Index(IsUnique = true)]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
    public string COUNTRY_CODE { get; set; }

    [DataMember]
    [Required(ErrorMessage = "Country Name is required")]
    [Display(Name = "Country Name")]
    //[Index(IsUnique = true)]
    //[StringLength(50, ErrorMessage = "Too long. Plese check again!")]
    [StringLength(50, ErrorMessage = "The {0} must be at least {1} characters long. Plese check again!", MinimumLength = 2)]
    [RegularExpression(@"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
    public string COUNTRY_NAME { get; set; }

    [DataMember]
    [Display(Name = "Action Status")]
    public int ACTION_STATUS { get; set; }

    [DataMember]
    [Display(Name = "Date Created")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> CREATED_DATE { get; set; }

    [DataMember]
    [Display(Name = "Created By")]
    public Nullable<int> CREATED_BY { get; set; }

    [DataMember]
    [Display(Name = "Last Update Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> LAST_UPDATE_DATE { get; set; }

    [DataMember]
    [Display(Name = "Last Update By")]
    public Nullable<int> LAST_UPDATE_BY { get; set; }

    [DataMember]
    [Display(Name = "Date Deleted")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public Nullable<System.DateTime> DELETED_DATE { get; set; }

    [DataMember]
    [Display(Name = "Deleted By")]
    public Nullable<int> DELETED_BY { get; set; }


    [DataMember]
    public virtual ICollection<CITIES> CITIES { internal get; set; }

    [DataMember]
    public virtual ICollection<LGA> LGA { internal get; set; }

    [DataMember]
    public virtual ICollection<STATES> STATES { internal get; set; }
}
}

服务:WCF

namespace BPP.CCSP.Admin.Services.Services.Concrete
{

[ValidateDataAnnotationsBehavior]

public class CountriesService : ICountriesService
{
    //public void DoWork()
    //{
    //}
    private readonly ICountriesManager _countriesManager;

    public CountriesService(ICountriesManager countriesManager)
    {
        _countriesManager = countriesManager;
    }

    public COUNTRIES GetCountry(Int32 countryID)
    {
        return _countriesManager.Country(countryID);
    }

    public IEnumerable<COUNTRIES> GetCountries()
    {
        return _countriesManager.Countries();
    }

    public void AddCountry(COUNTRIES countries)
    {
        _countriesManager.AddCountry(countries);
    }

    public void RemoveCountry(int countryID)
    {
        _countriesManager.Country(countryID);
    }
}
}

问题是,为什么数据标注和验证没有在表现层(视图)中实现?

【问题讨论】:

    标签: c# asp.net-mvc wcf


    【解决方案1】:

    您遗漏的关键信息是您如何将服务引用导入到您的演示项目中。

    我暂时假设您使用了服务参考向导 - 这就是导致您的问题的原因。当您使用提供的向导时,Visual Studio 会查看您的 WCF 服务的托管 WSDL 定义,并在您正在处理的项目中自动生成新的代理和数据协定。WSDL 不支持您使用的数据注释,因此不会被复制到演示项目中定义的新合同。

    要解决此问题,您有两个选择。

    1) 导航到您的 Presentation 项目中自动生成的类并标记它们。显然这从长远来看会导致代码重复,并不是最理想的。

    2) 通过 DLL 引用您的数据契约和服务契约,并编写您自己的继承自 ClientBase 的代理类。您可以在此处找到更多详细信息:Create WCF Client without auto generated proxy

    【讨论】:

    • 是的,你是对的。我使用了服务参考向导。问题是,我如何应用您所说的选项 2 通过 DLL 引用您的数据合同和服务合同并编写您自己的从 ClientBase 继承的代理类。您可以在此处找到更多详细信息。请问我不知道。
    猜你喜欢
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 2012-04-16
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    相关资源
    最近更新 更多