I have a class in a separate assembly that is being exposed via a WCF service. It is referenced in my service.
This class has several structs and a single delegate, and I can't figure out how to make sure they get serialized. When I comment out the structs in the class, the object is serialized, otherwise I get errors.
I've decorated the structs with [Serializable], but that does not work. Here is an example of one of them:
[Serializable]
public delegate bool ValidationDelegateInt( pdInt structure );
[Serializable]
public struct pdInt
{
public pdInt(int i, bool b, int rh, int rl, bool mv, bool va, ValidationDelegateInt v)
{
Value = i;
IsRequired = b;
RangeHigh = rh;
RangeLow = rl;
MustValidate = mv;
IsValidated = va;
Validate = v;
}
public int Value;
public bool IsRequired;
public int RangeHigh;
public int RangeLow;
public bool MustValidate;
public bool IsValidated;
public ValidationDelegateInt Validate;
}
Here is a snippet to demonstrate how I am declaring them:
[Serializable]
public class PDUser : Person, IValidate
{
#region Members
public pdInt SomeRange;
public pdString SomeString;
Here is some code that illustrates how I want to use them on the client.
private void bPDUserSelfDL_Click( object sender, EventArgs e )
{
Model.PDUser p = new Model.PDUser();
p.Self( 4 );
p.SomeRange.Value = 1;
if ( p.SomeRange.MustValidate )
{
bool IsValid = p.SomeRange.Validate( p.SomeRange );
}
}
Here is the data contract:
[DataContract( Name = "PDUserSelfResultSet", Namespace = "V_1" )]
public class PDUserSelfResultSet
{
List<PDUser> m_PDUsers;
[DataMember]
public List<PDUser> PDUsers
{
get
{
return m_PDUsers;
}
set
{
m_PDUsers = value;
}
}
}
and in my service, here is how I am using it:
public PDUserSelfResultSet PDUserSelf( int PersonID )
{
try
{
PDUserSelfResultSet s = new PDUserSelfResultSet();
s.PDUsers = new List<PDUser>();
PDUser p = new PDUser();
s.PDUsers.Add( p.Self( PersonID ));
return s;
}
catch ( System.Net.WebException we )
{
string s = we.Message;
throw we;
}
catch ( FaultException fe )
{
string s = fe.Message;
throw fe;
}
catch ( Exception e )
{
string s = e.Message;
throw e;
}
}
And on the client, finally here is how I'm calling it:
private void Testing()
{
WCFHostServiceClient proxy = new WCFHostServiceClient();
try
{
PDUserSelfResultSet r = new PDUserSelfResultSet();
r = proxy.PDUserSelf( 4 );
List<PDUser> p = r.PDUsers.ToList<PDUser>();
proxy.Close();
}
catch ( System.Net.WebException we )
{
string s = we.Message;
}
catch ( System.ServiceModel.CommunicationException ce )
{
string s = ce.Message;
}
}
If I comment out the structs in my PDUser class, then in my Testing() method the call succeeds. If I leave the structs in, I'll get a CommunicationException, which in this case means that WCF doesn't know how to deal with the struct.
Does anyone know how to create a data contract so that I can use the structs?