WCF参数的2.0可序列化项目问题 [英] 2.0 serializable Item problem with WCF parameters

查看:50
本文介绍了WCF参数的2.0可序列化项目问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为[EmailParameters]的2.0类,该类[可序列化],并且从下面声明的复杂类名AttributesParameter继承,它们都在dll中.

I have a 2.0 class named EmailParameters that is [serializable] and inherits from a complex class names AttributesParameter declared below that are both in a dll.

我想重复使用该dll中的类型作为参数传递给WCF方法,因此我选中了该复选框,然后一切都编译了.

I want to reuse the types from that dll to pass as parameter to a WCF methode so I checked the checkbox and everything compiles.

我正在将对象从客户端传递到服务器端,而不是相反.

I'm passing the objet from Client side to server side, not the converse.

运行它时,出现以下错误消息:

When I run it, I get this error message:

尝试序列化参数时出错 http://tempuri.org/:pEmailParam . InnerException消息是'Type'System.Collections.Generic.List`1 [[System.String,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]',数据协定名称为'ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' 不是预期的.将任何静态未知的类型添加到已知类型的列表中-例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型的列表中.请参阅InnerException了解更多信息 详细信息.

There was an error while trying to serialize parameter http://tempuri.org/:pEmailParam. The InnerException message was 'Type 'System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' with data contract name 'ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

我的服务集合类型"是通用列表",我无法通过属性设置第二个.

My Service Collection Type is Generic List and I cannot set a second one via the the properties.

起作用的是,如果我按照方法签名执行消息中所说的内容,但仅在reference.cs中(我只是无法找到它不知道的类型...),因此不建议这样做,因为您在每次更新服务参考时都会放弃更改. 我尝试在[OperationContract]签名上设置标志,但是它不起作用,可能是因为该对象是从客户端到服务器的.

What works is if I do what the message says over the method signature but only in the reference.cs (I just can't find the type it doesn't know...) which is not recommended because you loose the change everytime you update the service reference. I tried setting the flag over the [OperationContract] signature but it doesn't work, probably because the object is coming from the Client to the Server.

我不能使用[DataContract]/[DataMember],但是为什么我仍要重用该类型,为什么我还是要这么做?

I cannot use [DataContract]/[DataMember] but why should I anyways since I want to reuse the type?

知道发生了什么事吗?

 ///< summary>
 ////基于实现IEditableObject的Attribute Hashtable对象的参数
 ///</summary>
  [可序列化]
 公共类AttributesParameter:IEditableObject,IIndexer
  {
   #region公共财产成员...

 /// <summary>
 /// Parameter base on Attribute Hashtable object that implement IEditableObject
 /// </summary>
 [Serializable]
 public class AttributesParameter : IEditableObject, IIndexer
 {
  #region Public Property Member...

 ///< summary>
  ///此类的索引器...
  ///</summary>
  ///< param name ="pKey"></param>
  ///< returns></returns>
  公共对象this [string pKey]
   {
    get {返回this._Attributes [pKey]; }
    set {this._Attributes [pKey] = value; }
  }

  /// <summary>
  /// Indexer of this class...
  /// </summary>
  /// <param name="pKey"></param>
  /// <returns></returns>
  public object this[string pKey]
  {
   get { return this._Attributes[pKey]; }
   set { this._Attributes[pKey] = value; }
  }

 ///< summary>参数集合属性</summary>
   [XmlIgnore]
  公共哈希表属性
   {
    get {返回this._Attributes; }
    set {this._Attributes = value; }
  }

  /// <summary>Parameter Collection Property</summary>
  [XmlIgnore]
  public Hashtable Attributes
  {
   get { return this._Attributes; }
   set { this._Attributes = value; }
  }

  #endregion
   #region私有财产成员...

  #endregion
  #region Private Property Member...

 ///< summary>参数集合</summary>
  私有哈希表_Attributes = new Hashtable(5);

  /// <summary>Parameter Collection</summary>
  private Hashtable _Attributes = new Hashtable(5);

 ///< summary>
  ///备份以进行编辑过程
  ///</summary>
  私有对象oEditBackup = null;

  /// <summary>
  /// Backup for the Edit process
  /// </summary>
  private object oEditBackup = null;


   #endregion


  #endregion

  #region公共方法成员...
   #region构造函数...

  #region Public Method Member...
  #region Constructor...

 ///< summary>构造函数</summary>
   public AttributesParameter():base(){}

  /// <summary>Constructor</summary>
  public AttributesParameter() : base() { }

 ///< summary>
  ///具有AttributesParameter
的构造方法   ///</summary>
  ///< param name ="pParam"></param>
  公共AttributesParameter(AttributesParameter pParam)
   :this()
   {
    this._Attributes = pParam._Attributes;
  }

  /// <summary>
  /// Constructor with AttributesParameter
  /// </summary>
  /// <param name="pParam"></param>
  public AttributesParameter(AttributesParameter pParam)
   : this()
  {
   this._Attributes = pParam._Attributes;
  }

 ///< summary>构造函数</summary>
 ///< param name ="info">保留序列化或反序列化对象所需的所有数据.  ///< param name ="context">描述给定序列化流的源和目标.   公共AttributesParameter(SerializationInfo信息,StreamingContext上下文)
    ;: base()
   {
    this._Attributes =(Hashtable)info.GetValue(" Attributes,typeof(Hashtable));
  }

  /// <summary>Constructor</summary>
  /// <param name="info">Holds all the data needed to serialize or deserialize an object.</param>
  /// <param name="context">Describes the source and destination of a given serialized stream.</param>
  public AttributesParameter(SerializationInfo info, StreamingContext context)
   : base()
  {
   this._Attributes = (Hashtable)info.GetValue("Attributes", typeof(Hashtable));
  }

 ///< summary>
  ///构造函数
  ///</summary>
  ///< param name ="pData"></param>
   public AttributesParameter(object [,] pData)
   {
    if(pData.GetLength(1)> 1)
    {
    int Max = pData.GetLength(0);
    for(int i = 0; i< Max; i ++)
     this [pData [i,0] .ToString()] = pData [i,1];
   }
  }

  /// <summary>
  /// Constructor
  /// </summary>
  /// <param name="pData"></param>
  public AttributesParameter(object[,] pData)
  {
   if (pData.GetLength(1) > 1)
   {
    int Max = pData.GetLength(0);
    for (int i = 0; i < Max ; i++)
     this[pData[i, 0].ToString()] = pData[i, 1];
   }
  }


   #endregion


  #endregion

 ///< summary>获取所有序列化的数据.</summary>
 ///< param name ="info">保留序列化或反序列化对象所需的所有数据.  ///< param name ="context">描述给定序列化流的源和目标.   公共虚拟void GetObjectData(SerializationInfo信息,StreamingContext上下文)
   {
    info.AddValue(" Attributes,this._Attributes);
  }

  /// <summary>Gets all serialized data.</summary>
  /// <param name="info">Holds all the data needed to serialize or deserialize an object.</param>
  /// <param name="context">Describes the source and destination of a given serialized stream.</param>
  public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
  {
   info.AddValue("Attributes", this._Attributes);
  }

 ///< summary>开始版本</summary>
  公共无效BeginEdit()
   {
    if(this.oEditBackup == null)
     this.oEditBackup = this._Attributes.Clone();
  }
  ///< summary>取消版</summary>
  公共无效的CancelEdit()
   {
    this._Attributes =(哈希表)this.oEditBackup;
    this.oEditBackup = null;

  /// <summary>Begin Edition</summary>
  public void BeginEdit()
  {
   if (this.oEditBackup == null)
    this.oEditBackup = this._Attributes.Clone();
  }
  /// <summary>Cancel Edition</summary>
  public void CancelEdit()
  {
   this._Attributes = (Hashtable)this.oEditBackup;
   this.oEditBackup = null;

  }
  ///< summary> End Edition</summary>
   public void EndEdit(){this.oEditBackup = null; }

  }
  /// <summary>End Edition</summary>
  public void EndEdit() { this.oEditBackup = null; }

 ///< summary>
  ///覆盖ToString以使用其"ToString"属性
  ///如果属性ToString可以包含一个重要的属性键...
  ///</summary>
  ///< returns></returns>
  公共替代字符串ToString()
   {
    string Result = base.ToString();

  /// <summary>
  /// Override the ToString to user his "ToString" attribute
  /// if the Attribute ToString can containe a noter attribute key...
  /// </summary>
  /// <returns></returns>
  public override string ToString()
  {
   string Result = base.ToString();

   string att =此["ToString"]作为字符串;
    if(att!= null)
    {
    Result = att;
    att = this [att]作为字符串;
     if(att!= null)
     Result = att;
   }

   string att = this["ToString"] as string;
   if (att != null)
   {
    Result = att;
    att = this[att] as string;
    if (att != null)
     Result = att;
   }

  返回结果;
  }

   return Result;
  }

 ///< summary>返回是否存在特定的键属性...</summary>
  ///< param name ="pKey"></param>
  ///< returns></returns>
  公共布尔包含(字符串pKey)
   {
   返回this.Attributes.ContainsKey(pKey);
  }

  /// <summary>return if a specific key attributes exist...</summary>
  /// <param name="pKey"></param>
  /// <returns></returns>
  public bool Contains(string pKey)
  {
   return this.Attributes.ContainsKey(pKey);
  }

  #endregion
   #region私有方法成员
   #endregion

  #endregion
  #region Private Method Member
  #endregion

  #region公共类成员
   #endregion
   #region私人班级成员
   #endregion
 }

  #region Public Class Member
  #endregion
  #region Private Class Member
  #endregion
 }

推荐答案

从错误中您会看到,我猜您的操作采用了一个类型的参数,但您正在传递一个从该类型派生的类型的实例.在这种情况下,您需要使用KnownTypeAttribute指定要传递的类型. 有关更多信息,请参见WCF文档中的以下主题: http://msdn.microsoft.com/zh-cn/library/ms730167.aspx

From the error you are seeing I would guess that your operation take a parameter of a type but you are passing an instance of a type derived from that type. In that case you need to specify the type you will be passing using the KnownTypeAttribute.  For more information please see the following topic in the WCF documentation: http://msdn.microsoft.com/en-us/library/ms730167.aspx


这篇关于WCF参数的2.0可序列化项目问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆