【问题标题】:There was an error while trying to serialize parameter http://tempuri.org/:exception. in C#尝试序列化参数 http://tempuri.org/:exception 时出错。在 C# 中
【发布时间】:2017-12-14 20:09:49
【问题描述】:

我正在从门户应用程序调用我的 WCF 方法。

我的 WCF 设置如下:

 [ServiceContract]
    public interface ILogging
        {
            [OperationContract]
            [ServiceKnownType(typeof(ErrorExceptionInformation))]
            void LogException(ErrorExceptionInformation exception);
        }


    public class Logging : ILogging
        {
            public void LogException(ErrorExceptionInformation exception)
            {

                LogProviderManager.Default.WriteLog(exception.Application, exception.Exception, true, exception.Category);
            }
        }

        [DataContract]
        public class ErrorExceptionInformation
        {
            [DataMember]
            public string Application { get; set; }

            [DataMember]
            public Exception Exception { get; set; }

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

        }

这就是我在门户应用程序中调用的方式:

ErrorExceptionInformation information = new ErrorExceptionInformation
                {
                    Exception = errorToLog,
                    Application = Models.Constants.ErrorLog.ErrorLocation,
                    Category = Models.Constants.ErrorLog.AdminPortalEnvironmentName
                };

                new LoggingClient().LogException(information);

但是我不断收到以下错误: 如果您使用 DataContractSerializer 或将任何静态未知的类型添加到已知类型列表中,请考虑使用 DataContractResolver - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给序列化程序的已知类型列表中。有关详细信息,请参阅 InnerException。

【问题讨论】:

    标签: c# visual-studio wcf datacontractserializer


    【解决方案1】:

    错误告诉你可以这样做:

    [ServiceContract]
    public interface ILogging
    {
        [OperationContract]
        [ServiceKnownType("GetKnownTypes", typeof(KnownTypeHelper))] // << -- change this
        void LogException(ErrorExceptionInformation exception);
    }
    
    static class KnownTypeHelper
    {
        public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
        {
            System.Collections.Generic.List<System.Type> knownTypes =
                new System.Collections.Generic.List<System.Type>();
    
            knownTypes.Add(typeof(ErrorExceptionInformation));
           // knownTypes.Add(typeof(... any others....));
    
            return knownTypes;
        }
    }
    

    更新

    或者把属性放在接口上而不是操作契约上:

    [ServiceKnownType(typeof(ErrorExceptionInformation))]
    [ServiceContract]
    public interface ILogging
    {
        [OperationContract]            
        void LogException(ErrorExceptionInformation exception);
    }
    

    【讨论】:

    • 我不太了解它,因为它与使用 [ServiceKnownType(typeof(ErrorExceptionInformation))] 相同
    • 有人会想,但我这样做的原因是因为你的方式给了我同样的错误。
    • @babuji 这促使我进一步研究,如果您使用该版本,则属性在界面而不是操作合同上。
    猜你喜欢
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多