在运行时忽略DataMember -WCF [IgnoreDataMember]

时间:2015-10-25 00:09:00

标签: c# asp.net web-services wcf

我想知道是否有一种方法可以在运行时添加属性[IgnoreDataMember],以防catch是triggeres。我有返回对象,其中包含自定义错误对象,如果填充了错误DataMember,我想忽略所有其他DataMembers。我不想在响应成功时添加[IgnoreDataMember]。仅在填充Error对象时才执行此操作。

public TransactionResultDto createAccount(AccountActivationDto account)
{

    try
    {
        DatabaseHelper.CheckForDatabaseAndCreate();
        AccountDto newAccount = new PendingAccountDto(account);
        response = DatabaseHelper.CreatePendingAccount(newAccount);
        ActivationEmailer.SendEmail(newAccount);
        PendingAccountDto pendingAccount = (PendingAccountDto)newAccount;
        return response;
    }
    catch(Exception ex)
    {
        ErrorMessageDto error = new ErrorMessageDto();
        response.Error = ExceptionBuilder.LogErrors(ex);
        return response;
    }
}

DataContract

namespace developer1SVC.Core.Dto.Transaction
{
    [DataContract]
    public  class TransactionResultDto
    {
        private bool transactionSuccess;
        private string transactionDescription;
        private string transactionValue;
        private DateTime transactionDate;

        [DataMember]
        public bool TransactionSuccess
        {
            get { return transactionSuccess; }
            set { transactionSuccess = value; }
        }

        [DataMember]
        public string TransactionDescription
        {
            get { return transactionDescription; }
            set { transactionDescription = value; }
        }

        [DataMember]
        public string TransactionValue
        {
          get {return transactionValue;}
          set { transactionValue = value;}
        }

        [DataMember]
        public DateTime TransactionDate
        {
            get {return transactionDate; }
            set { transactionDate = value; }
        }

        [DataMember]
        public ErrorMessageDto Error { get; set; }
}
}

因此,如果错误对象返回某些内容,那就是我想要在客户端中显示的内容。没别了。

帮助将不胜感激。

0 个答案:

没有答案