2

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mike Malter
  • 1,018
  • 1
  • 14
  • 38
  • The WCF DataContractSerializer uses the `[DataContract]` and `[DataMember]` attributes - can you put these on your structs?? Decorate the struct definition with `[DataContract]` and all members you want to have serialized into your messages with `[DataMember]` . The `[Serializable]` has no impact and no effect on WCF serialization whatsoever.... – marc_s Apr 06 '11 at 05:43
  • marc_s, I found out last night after I posted this that it was not the struct that was the issue but the delegate in the struct. If I removed the delegate the struct would serialize. And you are right that the serialize decoration has no bearing on this. Thanks for your answer. – Mike Malter Apr 06 '11 at 13:05

2 Answers2

3

The issue with using structs in WCF was not so much about the structs themselves as what was in them. I had a delegate and that was causing the issue. The broader issue here is that a delegate is not consistent with interoperability. At some point my client is going to want to support iPad's and other tablets and they don't use .NET so figuring out how to support delegates in WCF will be counterproductive.

Mike Malter
  • 1,018
  • 1
  • 14
  • 38
0

Seems Related to :

Using Structs with WCF Services

Community
  • 1
  • 1
sajoshi
  • 2,733
  • 1
  • 18
  • 22
  • sajoshi, thanks for your answer, it seems that the issue is related to the delegates and I found out subsequent to the post that if I remove the delegate, there is no issue. Do you know how to serialize delegates, and is that even a good interop approach? – Mike Malter Apr 06 '11 at 13:07