2011-08-22 52 views
1

它看起来像serverside不能接收传递的值,requestVersion.Ping为空。WCF:客户端发送值但服务器收到空字符串

namespace Communication 
    { 
     public class DataForRequestVersion 
     { 
      public string Ping = ""; 
     } 

     public class DataForResponseVersion 
     { 
      public string Pong = ""; 
      public string Version = ""; 
     } 
    } 

    [ServiceContract] 
    public interface IService 
    {  
     [OperationContract] 
     Communication.DataForResponseVersion Version(Communication.DataForRequestVersion requestVersion); 

    } 

    //Server 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
    public class ServiceImplementation : WCFSimple.Contract.IService 
    { 
     public Communication.DataForResponseVersion Version(Communication.DataForRequestVersion requestVersion) 
     { 

      //Here requestVersion.Ping is EMPTY 

      Communication.DataForResponseVersion responseVersion = new Communication.DataForResponseVersion(); 
      responseVersion.Pong = requestVersion.Ping; 
      responseVersion.Version = "MyApp v" + Settings.version; 

      return responseVersion; 
     } 

    } 

    //Client makes a request here  
    Communication.DataForRequestVersion requestVersion = new Communication.DataForRequestVersion(); 
    requestVersion.Ping = DateTime.Now.ToString(Settings.DayTimePreciseFormat);  

    //Here requestVersion.Ping has a value 

    Communication.DataForResponseVersion responseVersion = 
     Service.Version(requestVersion); 

我错过了什么?

UPDATE

我的应用程序工作得很好,双方通过传递自定义数据类没有任何问题沟通。然而,我试图修改测试客户端哪一个只发送 - 接收当前时间为字符串,并使其参数有点复杂;从字符串到自定义数据类。主解决方案的应用程序可以发送版本请求并接收答案。所以我认为我的小测试客户端遇到了问题。

My debug scren

这里是pastebin code

月2日更新:

有些主持人不允许我来回答我的问题,我不知道为什么,我发现了一个非常类似的问题,这个人也回答了他自己的问题。下次我帮他人解释原因;我用命名空间,而不是创建 ......我定:

//namespace Communication 
public class Communication 
+2

另外...公共领域不被大量推荐...属性将是更可取; –

回答

1

您需要用[DataContract]属性标记您的请求(和响应)类,以及它们与[DataMember]属性属性:

[DataContract] 
public class DataForRequestVersion 
{ 
    [DataMember] 
    public string Ping = ""; 
} 
+0

奇怪,但我的应用程序。没有[DataContract]属性,其他服务也可以工作。 –

+0

请确保您具有对“System.Runtime.Serialization.dll”的引用以查找该属性。 – dlev

1

尝试使用[DataContract]你的数据... ...类和[DataMember]在各自的领域...

0

将DataForRequestVersion DataForResponseVersion类中的字段更改为属性。默认情况下,DatacontractSerializer将序列化公共属性。

相关问题