我有一个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()
{
}
}
}
答案 0 :(得分: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文档。
这是它抱怨的课程。
答案 1 :(得分: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; }
...
}
}