我正在将protobuf-net集成到基于WCF服务的解决方案中,但遇到了一个我无法弄清楚的问题。以下类将序列化正常,除了ObjectId属性。
/// <summary>
/// A service data object that represents a user of the system.
/// </summary>
[DataContract(Namespace = "http://LINKS.Service.Security.Administration", Name = "User")]
public sealed class UserMessagePart : IMessagePart
{
private ObjectId objectId;
private string userName;
private string firstName;
private string lastName;
private string middleName;
private string gender;
private string emailAddress;
private bool isAccountDisabled;
private string disabledMeaning;
private DateTime createDtTm;
private DateTime disabledDtTm;
private VersionMessagePart version;
/// <summary>
/// Main constructor.
/// </summary>
public UserMessagePart(ObjectId objectId, string userName, string firstName, string lastName, string middleName,
string gender, string emailAddress, bool isAccountDisabled, string disabledMeaning, DateTime createDtTm,
DateTime disabledDtTm, VersionMessagePart version)
{
this.objectId = objectId;
this.userName = userName;
this.firstName = firstName;
this.lastName = lastName;
this.middleName = middleName;
this.gender = gender;
this.emailAddress = emailAddress;
this.isAccountDisabled = isAccountDisabled;
this.disabledMeaning = disabledMeaning;
this.createDtTm = createDtTm;
this.disabledDtTm = disabledDtTm;
this.version = version;
}
/// <summary>
/// Parameterless constructor.
/// </summary>
public UserMessagePart(){}
/// <summary>
/// The unique identifier for this user.
/// </summary>
[DataMemberAttribute(IsRequired = true, Name = "ObjectId", Order = 0)]
public ObjectId ObjectId
{
get { return objectId; }
set { objectId = value; }
}
/// <summary>
/// The user's login identity.
/// </summary>
[DataMemberAttribute(IsRequired = true, Name = "UserName", Order = 1)]
public string UserName
{
get { return userName; }
set { userName = value; }
}
// ...abbreviated code for readability...
/// <summary>
/// Version information for this user
/// </summary>
[DataMemberAttribute(IsRequired = true, Name = "Version", Order = 11)]
public VersionMessagePart Version
{
get { return version; }
set { version = value; }
}
}
这是与我一起玩的课程:
/// <summary>
/// Uniquely identifies any <see cref="IMessagePart"/> implementation in the system.
/// </summary>
[DataContract(Namespace = "http://LINKS.Service.Core", Name = "ObjectIdentifier")]
public class ObjectId
{
private string id;
private string domain;
private string modelName;
private long instanceId;
private int versionNumber;
/// <summary>
/// Default constructor. (required for Protobuf-net)
/// </summary>
public ObjectId()
{
}
/// <summary>
/// Main constructor.
/// </summary>
public ObjectId(string domain, string modelName, long instanceId, int versionNumber)
{
id = string.Format("{0}#{1}#{2}#{3}", domain, modelName, instanceId, versionNumber);
this.domain = domain;
this.modelName = modelName;
this.instanceId = instanceId;
this.versionNumber = versionNumber;
}
/// <summary>
/// The unique identifier for the <see cref="ObjectId"/>. The format of this string is not
/// guaranteed and should only be treated as a unique key. No attempts to parse it should be
/// made by client applications.
/// </summary>
[DataMemberAttribute(IsRequired = true, Name = "Id", Order = 0)]
public string Id
{
get { return id; }
set
{
id = value;
string[] parts = id.Split('#');
domain = parts[0];
modelName = parts[1];
instanceId = long.Parse(parts[2]);
versionNumber = int.Parse(parts[3]);
}
}
/// <summary>
/// The system domain that the <see cref="ObjectId"/> originated from.
/// </summary>
public string Domain
{
get { return domain; }
}
/// <summary>
/// The type of object that this <see cref="ObjectId"/> refers to.
/// </summary>
public string ModelName
{
get { return modelName; }
}
/// <summary>
/// The object instance identifier for the object that this <see cref="ObjectId"/>
/// refers to. Typically, this is a database key.
/// </summary>
public long InstanceId
{
get { return instanceId; }
}
/// <summary>
/// The version instance of the object referred to by this <see cref="ObjectId"/>
/// </summary>
public int VersionNumber
{
get { return versionNumber; }
}
/// <summary>
/// Returns a string representation of the object identifier.
/// </summary>
new public string ToString()
{
return id;
}
}
我尝试过多次没有运气的事情。任何想法都将不胜感激!
答案 0 :(得分:2)
(我是protobuf-net的作者)
我即将离开,所以我无法“立即”验证,但在第一次猜测时,我会说Order = 0
看起来很可能是罪魁祸首。尝试Order = 1
或其他一些数字(“协议缓冲区”标识符必须为正数)。
请注意,您需要在ObjectId
和Id
上进行调整。
我已经拿了上面的副本,我会检查火车......
另请注意,如果使用程序集共享,则WCF挂钩最有效 - 即在客户端和服务器上具有相同的类。如果您使用svcutl
(或“添加服务引用”),它有时会重新映射数字;有一个技巧可以解决这个问题 - 让我知道这是一个问题(基本上,仔细检查工具生成的属性Order
)