data contract issue  
Author Message
cool dude 2007





PostPosted: Windows Communication Foundation ("Indigo"), data contract issue Top

What is wrong with the following code. When i try to access Retrive() operation the application time outs and at times it get communication error while MyOperation1() and MyOperation2() are working fine. host and clients are on the same machine.

using System;

using System.ServiceModel;

using System.Collections.Generic;

using System.Runtime.Serialization;

// A WCF service consists of a contract (defined below as IMyService, DataContract1),

// a class which implements that interface (see MyService),

// and configuration entries that specify behaviors associated with

// that implementation (see <system.serviceModel> in web.config)

[ServiceContract()]

public interface IMyService

{

[OperationContract]

string MyOperation1(string myValue1);

[OperationContract]

string MyOperation2(DataContract1 dataContractValue);

[OperationContract]

CommunicationWrapper Retrieve();

}

public class MyService : IMyService

{

public string MyOperation1(string myValue1)

{

return "Hello: " + myValue1;

}

public string MyOperation2(DataContract1 dataContractValue)

{

return "Hello: " + dataContractValue.FirstName;

}

public CommunicationWrapper Retrieve()

{

CommunicationWrapper wrapper = new CommunicationWrapper();

List<Object> objList = new List<object>();

string str;

try

{

DataContract1 dataContract = new DataContract1();

dataContract.FirstName = "Saeed";

dataContract.LastName = "Hassan";

objList.Add(dataContract);

wrapper.Bolist = objList;

}catch(Exception e)

{

str = "The method or operation is not implemented.";

}

return wrapper;

}

}

[DataContract]

public abstract class BaseClass

{

private string primaryKey;

[DataMember]

public string PrimaryKey

{

get { return primaryKey; }

set { primaryKey = value; }

}

}

[DataContract]

public class DataContract1 : BaseClass

{

string firstName;

string lastName;

[DataMember]

public string FirstName

{

get { return firstName;}

set { firstName = value;}

}

[DataMember]

public string LastName

{

get { return lastName;}

set { lastName = value;}

}

}

[DataContractAttribute]

public class CommunicationWrapper

{

private List<object> bolist;

[DataMember]

public List<object> Bolist

{

get { return bolist; }

set { bolist = value; }

}

public List<object> GetBOList()

{

return Bolist;

}

public void SetBOList(List<object> value )

{

Bolist = value;

}

}



Visual Studio 200846  
 
 
BenK





PostPosted: Windows Communication Foundation ("Indigo"), data contract issue Top

Reply if it works once , it is not a data contract / serialization issue .

Though I would never put something like private List<object> bolist; in a contract .

When does it give you comms errors When you are making asingle connection or lots of connections Is the client threaded

Regards,

Ben


 
 
Carlos Figueira - MSFT





PostPosted: Windows Communication Foundation ("Indigo"), data contract issue Top

Since you're declaring the list of objects, the client would have no idea that an incoming object is of type DataContract1 (the throw actually happens on the serialization on the server). To fix this problem, you need to inform WCF that DataContract1 is one of the "known types":

[DataContractAttribute]
[
KnownType(typeof(DataContract1))]
public class CommunicationWrapper
{
private List<object> bolist;
...

Since DataContract1 knows about BaseClass, you don't need to specify it.