0

我有以下对象 ValidationType、ValidationRule、ValidationProperty、ValidatorBase(抽象类)、NotNullValidatorRule(派生自 ValidatorBase 抽象类),如下所示。

public class ValidationType
{
    public ValidationType();

    public string AssemblyName { get; set; }
    public string DefaultRuleSet { get; set; }
    public long Id { get; set; }
    public string Name { get; set; }
    public long ProcessId { get; set; }
    public List<ValidationRule> RuleSet { get; set; }
    public long SubProcessId { get; set; }
    public DateTime ValidFrom { get; set; }
    public DateTime ValidTo { get; set; }
}

public class ValidationRule
{
    public ValidationRule();

    public long Id { get; set; }
    public string Name { get; set; }
    public List<ValidationProperty> Properties { get; set; }
    public long TypeId { get; set; }
}

public class ValidationProperty
{
    public ValidationProperty();

    public long Id { get; set; }
    public string Name { get; set; }
    public long RulesetId { get; set; }
    public List<ValidatorBase> Validators { get; set; }
}

public abstract class ValidatorBase
{
    protected ValidatorBase();

    public string MessageTemplate { get; set; }
    public string Name { get; set; }
    public bool Negated { get; set; }
    public string Tag { get; set; }
    public string TemplateResourceName { get; set; }
    public string TemplateResourceType { get; set; }
    public string Type { get; set; }

    public abstract XNode GetValidator();
}

public class NotNullValidatorRule : ValidatorBase
{

    public override XNode GetValidator()
    {
        try
        {
            XElement validation = new XElement("validator",
                    new XAttribute("type", this.Type),
                    new XAttribute("name", this.Name),
                    new XAttribute("messageTemplateResourceName", this.TemplateResourceName),
                    new XAttribute("messageTemplateResourceType", this.TemplateResourceType),
                    new XAttribute("messageTemplate", this.MessageTemplate),
                    new XAttribute("negated", this.Negated),
                    new XAttribute("tag", this.Tag));
            return (XNode)validation;
        }
        catch
        {
            throw;
        }
    }       
}


I have WCF Service to return a List<ValidationType>,

  public List<ValidationType> GetValidationRules(long processId)
    {
        try
        {
            List<ValidationType> ResultValidionTypesList = new List<ValidationType>();

            List<ValidationType> ValidationTypes = new List<ValidationType>();

            ValidatorBase CodeMandatory = new NotNullValidatorRule();
            CodeMandatory.Name = "CodeMandatory";
            CodeMandatory.TemplateResourceName = "";
            CodeMandatory.TemplateResourceType = "";
            CodeMandatory.Type = "Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator,Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
            CodeMandatory.Tag = "display:none";
            CodeMandatory.Negated = true;
            CodeMandatory.MessageTemplate = "";
            CodeMandatory.GetValidator();
            ValidationProperty CodeProperty = new ValidationProperty();
            CodeProperty.Name = "Notes";
            CodeProperty.Validators.Add(CodeMandatory);


            ValidationRule CompanyTypeRules = new ValidationRule();
            CompanyTypeRules.Name = "CustomerGroupRuleset";
            CompanyTypeRules.Properties.Add(CodeProperty);

            ValidationType CompanyValidationsTypes = new ValidationType();
            CompanyValidationsTypes.Name = "iBoxV5.Model.CMS.Masters.CustomerGroup";
            CompanyValidationsTypes.DefaultRuleSet = "CustomerGroupRuleset";
            CompanyValidationsTypes.AssemblyName = "iBoxV5.Model.CMS.Masters, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
            CompanyValidationsTypes.RuleSet.Add(CompanyTypeRules);

            ResultValidionTypesList.Add(CompanyValidationsTypes);

            return ResultValidionTypesList;

        }
        catch (Exception ex)
        {
            RollBackTransaction();
            throw ex;
        }
        finally
        {
            CloseConnection();
        }

    }

如果我没有添加,上面将返回值列表

//CodeProperty.Validators.Add(CodeMandatory);

陈述。如果我尝试添加 NotNullValidatorRule 的实例(它派生自 ValidatorBase(抽象类)),则会发生以下 WCF 服务错误。但是,相同的验证类型正在返回列表,包括控制台应用程序中的 NotNullValidaitonRule。

'调用服务失败。可能原因:服务离线或无法访问;客户端配置与代理不匹配;现有代理无效。有关更多详细信息,请参阅堆栈跟踪。您可以尝试通过启动新代理、恢复到默认配置或刷新服务来恢复。

基础连接已关闭:连接意外关闭。

内部异常:基础连接已关闭:连接意外关闭。在 System.Net.HttpWebRequest.GetResponse() 在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度超时)

任何人都可以澄清出了什么问题..!

提前致谢..

4

1 回答 1

1

WCF 基础结构必须知道所有ValidatorBase被序列化/反序列化的后代以执行这些操作。您应该通过以下方式为您的数据合同提供此信息KnownTypeAttribute

[DataContract]
public class ValidationType {}

[DataContract]
public class ValidationRule {}

[DataContract]
public class ValidationProperty {}

[DataContract]
[KnownType(typeof(NotNullValidatorRule))]
public abstract class ValidatorBase {}

[DataContract]
public class NotNullValidatorRule : ValidatorBase {}
于 2012-09-10T09:09:36.320 回答