开发者

WCF: Client sends the value but server receives empty string

It looks like serverside cannot receive the passed values, requestVersion.Ping is empty.

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

       publ开发者_运维技巧ic 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);

What am I missing?

UPDATE

My application works very well, both sides communicate by passing custom data classes without any problem. However I tried to modify test client which one only sends-receives current time as string and made its parameter a bit complex; from string to custom data class. Main solution's application can send Version request and receive the answer. So I think my little test client got a problem.

WCF: Client sends the value but server receives empty string

Here is the pastebin code:

2nd UPDATE:

Some moderator doesn't allow me to answer my own question, I don't know why, I found a very similar question and the guy answered his own too. To help others next time I'm explaining the reason; I used namespace instead of creating class...And I fixed:

//namespace Communication
public class Communication


You need to label your request (and response) classes with the [DataContract] attribute, and their properties with the [DataMember] attribute:

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


try using [DataContract] on your Data...classes... and [DataMember] on their fields...


Change the fields in DataForRequestVersion DataForResponseVersion classes to properties. By Default DatacontractSerializer will serialize public properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜