1

我有一个 WCF 服务,它包含它的接口、类定义和一些其他对象类。它编译得很好,但是当我运行它时,我得到:

Type 'sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.

我的sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type班级也有一个属性[Serializable]。但无论我做了什么,我都无法成功。

我怎样才能完成将此类设置为可序列化?有没有办法实现这个?

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web;

namespace sarus.division.warehouse.cmms.proxy.services.location.type
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IType" in both code and config file together.
[ServiceContract]
public interface IType
{
    [OperationContract]
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Delete?id={id}")]
    CompositeType Select(Guid id);

    [OperationContract]
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    [ServiceKnownType(typeof(CompositeType))]
    CompositeType List();

    [OperationContract]
    [WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    void Add(sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type type);

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    void Update(sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type type);

    [OperationContract]
    [WebInvoke(Method = "DELETE", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    void Delete(Guid id);
}

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Type" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Type.svc or Type.svc.cs at the Solution Explorer and start debugging.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Type : IType
{
    public CompositeType Select(Guid id)
    {
        return new CompositeType().Select(id);
    }
    public CompositeType List()
    {
        return new CompositeType().List();
    }
    public void Add(sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type type)
    {
    }
    public void Update(sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type type)
    {
    }
    public void Delete(Guid id)
    {
    }
}

[DataContract]
[Serializable]
public class CompositeType
{
    [DataMember]
    public TypeColumns Columns { get; set; }

    [DataMember]
    public TypeData Data { get; set; }

    public CompositeType()
    {
        Columns = new TypeColumns();
        Data = new TypeData();
    }
    public CompositeType Select(Guid id)
    {
        try
        {
            var composite = new CompositeType();

            var columns = ((HttpContext.Current.Session["PRINCIPAL"] as sarus.division.common.localization.data.principal.intelligence.Intelligence).Hubs["warehouse.cmms"].Hive["LOCATION-TYPE"] as sarus.division.common.localization.data.hive.drone.Drone).GetColumns();
            foreach (var column in columns) { composite.Columns.Add(column); }

            var data = new sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type().Select(id);

            composite.Data.Add(data);

            return composite;
        }
        catch (Exception) { throw; }
    }
    public CompositeType List()
    {
        try
        {
            var composite = new CompositeType();

            var columns = ((HttpContext.Current.Session["PRINCIPAL"] as sarus.division.common.localization.data.principal.intelligence.Intelligence).Hubs["warehouse.cmms"].Hive["LOCATION-TYPE"] as sarus.division.common.localization.data.hive.drone.Drone).GetColumns();
            foreach (var column in columns) { composite.Columns.Add(column); }

            var list = sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type.List();
            foreach (var data in list) { composite.Data.Add(data); }

            return composite;
        }
        catch (Exception) { throw; }
    }
}

[CollectionDataContract(Name = "GridViewColumn{0}List", ItemName = "GridViewColumn")]
public class TypeColumns : List<sarus.division.common.infrastructure.data.foundation.definitions.GridViewColumn>
{
    public TypeColumns()
        : base()
    {
    }

    public TypeColumns(sarus.division.common.infrastructure.data.foundation.definitions.GridViewColumn[] items)
        : base()
    {
        foreach (sarus.division.common.infrastructure.data.foundation.definitions.GridViewColumn item in items)
        {
            Add(item);
        }
    }
}

[CollectionDataContract(Name = "Type{0}List", ItemName = "Type")]
public class TypeData : List<infrastructure.data.logic.location.type.Type>
{
    public TypeData()
        : base()
    {
    }

    public TypeData(infrastructure.data.logic.location.type.Type[] items)
        : base()
    {
        foreach (infrastructure.data.logic.location.type.Type item in items)
        {
            Add(item);
        }
    }
  }
}

编辑:这里是“sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type”类;

    using sarus.division.common.infrastructure.data.foundation.definitions;
    using sarus.division.common.infrastructure.data.logic.user;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;

    namespace sarus.division.warehouse.cmms.infrastructure.data.logic.location.type
    {
        public class Type : actual.Actual
        {
            public List<location.Location> Locations { get; set; }

            public Type(bool autoLoadDetails = false)
            {
            }

            public Type Select(Guid id)
            {
            }
            public List<Type> Select()
            {
            }
            public static List<Type> List()
            {
            }
            public void Save()
            {
            }

            public void Delete()
            {
            }
        }
    }
4

2 回答 2

1

从您的代码:

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Type" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Type.svc or Type.svc.cs at the Solution Explorer and start debugging.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Type : IType
{
   ....
}

我在这里看不到[Serializable]属性。并根据您的错误:

无法序列化类型“ sarus.division.warehouse.cmms.infrastructure.data.logic.location.type.Type ”。考虑使用 DataContractAttribute 属性对其进行标记,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员。如果该类型是一个集合,请考虑使用 CollectionDataContractAttribute 对其进行标记。有关其他支持的类型,请参阅 Microsoft .NET Framework 文档。

这是它抱怨的班级。

于 2013-09-17T16:45:02.120 回答
0

答案就在您的错误消息中。[Serializable]根据所使用的序列化代码,可能无法正常工作。尝试按照建议标记您的类和属性。此外,任何被序列化的东西也需要被标记,所以你需要对你的Location班级进行同样的标记。

namespace sarus.division.warehouse.cmms.infrastructure.data.logic.location.type
{
    [DataContract]
    public class Type : actual.Actual
    {
        [DataMember]
        public List<location.Location> Locations { get; set; }

        ...
    }
}
于 2013-09-17T21:22:17.910 回答