从Silverlight调用WCF服务:某处我的参数正在消失

时间:2011-07-29 15:06:56

标签: .net silverlight wcf

我正在尝试使用生成的代理从Silverlight调用WCF服务,并且出于某种原因我的参数被淘汰了。我这样打电话:

Events.EventServiceClient events = new Events.EventServiceClient(new BasicHttpBinding(),     new EndpointAddress("http://localhost:52878/EventService.svc"));
event.EventTypeName = e.GetType().FullName;
events.AddEventWrapperAsync(new EventWrapper(event));

在调试时,当我调用AddEventWrapperAsync方法时,我看到我有一个有效的对象。但是,进入下一个调用

public System.IAsyncResult BeginAddEventWrapper(BCL.BusinessObjects.EventWrapper Event, System.AsyncCallback callback, object asyncState) {
    object[] _args = new object[1];
    _args[0] = Event;
System.IAsyncResult _result = base.BeginInvoke("AddEventWrapper", _args, callback, asyncState);
    return _result;
}

我发现“Event”参数设置为新的EventWrapper(即,所有字段都设置为默认值)。

任何人都知道从哪里开始寻找?这显然是一个序列化问题,但除此之外,我感到困惑。

更新:这是EventWrapper

using System;
using System.Collections.Generic;
using BCL.FLY;
using BCL.CTB;
using BCL.WCN;
using BCL;
using System.ServiceModel;
using System.Runtime.Serialization;
namespace BCL.BusinessObjects
{
    [Serializable]
    [DataContract]
    public class Point
    {
        [DataMember]
        public Double X { get; set; }
        [DataMember]
        public Double Y { get; set; }
        public Point(System.Windows.Point point){
            this.X = point.X;
            this.Y = point.Y;
        }
    }
    [DataContract]
    public class EventWrapper 
    {
         [DataMember]
        public Guid Id { get; set; }
         [DataMember]
        public   string StudentID{get;set;}
         [DataMember]
        public   DateTime LocalTimestamp{get;set;}
         [DataMember]
         public   GameEventType Type { get; set; }
         [DataMember]
         public   int Level { get; set; }
         [DataMember]
         public   int Score { get; set; }
         [DataMember]
         public   string message { get; set; }
         [DataMember]
         public   EventLoggingLevel LogLevel { get; set; }
         [DataMember]
         public   int SessionID { get; set; }
         [DataMember]
         public   string EventTypeName { get; set; }
         [DataMember]
         public   double Speed { get; set; }
         [DataMember]
         public   GameEventSubtype SubType { get; set; }


        //CTBEvent
         [DataMember]
        public double BallSize{get;set;}
         [DataMember]
        public string CTBRuleType{get;set;}
         [DataMember]
        public  Point ClickLocation{get;set;}
         [DataMember]
        public TimeSpan StimulusDuration{get;set;}
         [DataMember]
        public List<BallInfo> BallStates{get;set;}

        //FLYEvent
         [DataMember]
        public string Category{get;set;}
         [DataMember]
        public List<BflyInfo> ButterFlies{get;set;}

        //WCNEvent
         [DataMember]
        public TimeSpan TimeGiven{get;set;}
         [DataMember]
        public TimeSpan TimeRemaning{get;set;}
         [DataMember]
        public WCNTrialData TrialData{get;set;}


        #region constructors

        public EventWrapper() { }

        public EventWrapper(IGameEvent _event)
        {
            this.StudentID = _event.StudentID;
            this.LocalTimestamp = _event.LocalTimestamp;
            this.Type = _event.Type;
            this.Level = _event.Level;
            this.Score = _event.Score;
            this.message = _event.message;
            this.LogLevel = _event.LogLevel;
            this.SessionID = _event.SessionID;
            this.EventTypeName = _event.EventTypeName;
            this.Speed = _event.Speed;
            this.SubType = _event.SubType;
        }

        public EventWrapper(CTBEvent _event)
        {
            this.StudentID = _event.StudentID;
            this.LocalTimestamp = _event.LocalTimestamp;
            this.Type = _event.Type;
            this.Level = _event.Level;
            this.Score = _event.Score;
            this.message = _event.message;
            this.LogLevel = _event.LogLevel;
            this.SessionID = _event.SessionID;
            this.EventTypeName = _event.EventTypeName;
            this.Speed = _event.Speed;
            this.SubType = _event.SubType;
            this.BallSize = _event.BallSize;
            this.CTBRuleType = _event.CTBRuleType;
            this.ClickLocation = new Point(_event.ClickLocation);
            this.StimulusDuration = _event.StimulusDuration;
            this.BallStates = _event.BallStates;
        }

        public EventWrapper(FLYEvent _event)
        {
            this.StudentID = _event.StudentID;
            this.LocalTimestamp = _event.LocalTimestamp;
            this.Type = _event.Type;
            this.Level = _event.Level;
            this.Score = _event.Score;
            this.message = _event.message;
            this.LogLevel = _event.LogLevel;
            this.SessionID = _event.SessionID;
            this.EventTypeName = _event.EventTypeName;
            this.Speed = _event.Speed;
            this.SubType = _event.SubType;
            this.Category = _event.Category;
            this.ButterFlies = _event.ButterFlies;
        }

        public EventWrapper(WCNEvent _event)
        {
            this.StudentID = _event.StudentID;
            this.LocalTimestamp = _event.LocalTimestamp;
            this.Type = _event.Type;
            this.Level = _event.Level;
            this.Score = _event.Score;
            this.message = _event.message;
            this.LogLevel = _event.LogLevel;
            this.SessionID = _event.SessionID;
            this.EventTypeName = _event.EventTypeName;
            this.Speed = _event.Speed;
            this.SubType = _event.SubType;
            this.TimeGiven = _event.TimeGiven;
            this.TimeRemaning = _event.TimeRemaning;
            this.TrialData = _event.TrialData;
        }

        #endregion
    }

}

这是我选择“添加服务参考”

时生成的事件
public partial class CTBEvent : BCL.WCFEvents.IGameEvent {

        private double BallSizeField;

        private System.Collections.ObjectModel.ObservableCollection<BCL.WCFEvents.BallInfo> BallStatesField;

        private string CTBRuleTypeField;

        private System.Windows.Point ClickLocationField;

        private System.TimeSpan StimulusDurationField;

        private System.Collections.ObjectModel.ObservableCollection<string> _tField;

        [System.Runtime.Serialization.DataMemberAttribute()]
        public double BallSize {
            get {
                return this.BallSizeField;
            }
            set {
                if ((this.BallSizeField.Equals(value) != true)) {
                    this.BallSizeField = value;
                    this.RaisePropertyChanged("BallSize");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public System.Collections.ObjectModel.ObservableCollection<BCL.WCFEvents.BallInfo> BallStates {
            get {
                return this.BallStatesField;
            }
            set {
                if ((object.ReferenceEquals(this.BallStatesField, value) != true)) {
                    this.BallStatesField = value;
                    this.RaisePropertyChanged("BallStates");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string CTBRuleType {
            get {
                return this.CTBRuleTypeField;
            }
            set {
                if ((object.ReferenceEquals(this.CTBRuleTypeField, value) != true)) {
                    this.CTBRuleTypeField = value;
                    this.RaisePropertyChanged("CTBRuleType");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public System.Windows.Point ClickLocation {
            get {
                return this.ClickLocationField;
            }
            set {
                if ((this.ClickLocationField.Equals(value) != true)) {
                    this.ClickLocationField = value;
                    this.RaisePropertyChanged("ClickLocation");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public System.TimeSpan StimulusDuration {
            get {
                return this.StimulusDurationField;
            }
            set {
                if ((this.StimulusDurationField.Equals(value) != true)) {
                    this.StimulusDurationField = value;
                    this.RaisePropertyChanged("StimulusDuration");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public System.Collections.ObjectModel.ObservableCollection<string> _t {
            get {
                return this._tField;
            }
            set {
                if ((object.ReferenceEquals(this._tField, value) != true)) {
                    this._tField = value;
                    this.RaisePropertyChanged("_t");
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:3)

几个问题:

  1. 可能与问题无关但你为什么使用像“event”这样的保留关键字作为变量名呢?
  2. 您已调试服务器端并验证参数重整确实发生并且不仅仅是调试视图的问题,对吗?
  3. 是什么让您认为这是序列化问题?据我所知,尚未发生序列化。
  4. 您确定生成的代理/客户端代码与http://localhost:52878/EventService.svc
  5. 托管的服务的当前版本相匹配吗?
  6. 更新:只是在黑暗中拍摄。使用Begin / End方法调用方法有什么不同吗? (我知道不应该)
  7. 更新1:您可以与我们分享BCL.BusinessObjects.EventWrapper的来源吗?

    更新2: EventWrapper类的那些自定义构造函数对我来说很可疑,因为在客户端上,EventWrapper应该由Service Reference生成,因此不应包含这些构造函数。所以你在客户端上修改了Reference.cs - 你不应该做的事情,或者你以某种方式从服务器端代码中导入了代码 - 这是你不应该做的事情。哪一个?

答案 1 :(得分:0)

您能否先使用WCFTestClient或SoapUI尝试使用Web服务?它可能是服务器端正在起作用。

另外,我更喜欢合同分享。这样就可以解决更少的问题,因为您不需要生成这些服务引用。