Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing a Delegate in WCF
    text
    copied!<p>I have a class in a separate assembly that is being exposed via a WCF service. It is referenced in my service.</p> <p>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.</p> <p>I've decorated the structs with [Serializable], but that does not work. Here is an example of one of them:</p> <pre><code>[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; } </code></pre> <p>Here is a snippet to demonstrate how I am declaring them:</p> <pre><code>[Serializable] public class PDUser : Person, IValidate { #region Members public pdInt SomeRange; public pdString SomeString; </code></pre> <p>Here is some code that illustrates how I want to use them on the client.</p> <pre><code> 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 ); } } </code></pre> <p>Here is the data contract:</p> <pre><code>[DataContract( Name = "PDUserSelfResultSet", Namespace = "V_1" )] public class PDUserSelfResultSet { List&lt;PDUser&gt; m_PDUsers; [DataMember] public List&lt;PDUser&gt; PDUsers { get { return m_PDUsers; } set { m_PDUsers = value; } } } </code></pre> <p>and in my service, here is how I am using it:</p> <pre><code> public PDUserSelfResultSet PDUserSelf( int PersonID ) { try { PDUserSelfResultSet s = new PDUserSelfResultSet(); s.PDUsers = new List&lt;PDUser&gt;(); 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; } } </code></pre> <p>And on the client, finally here is how I'm calling it:</p> <pre><code> private void Testing() { WCFHostServiceClient proxy = new WCFHostServiceClient(); try { PDUserSelfResultSet r = new PDUserSelfResultSet(); r = proxy.PDUserSelf( 4 ); List&lt;PDUser&gt; p = r.PDUsers.ToList&lt;PDUser&gt;(); proxy.Close(); } catch ( System.Net.WebException we ) { string s = we.Message; } catch ( System.ServiceModel.CommunicationException ce ) { string s = ce.Message; } } </code></pre> <p>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.</p> <p>Does anyone know how to create a data contract so that I can use the structs?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload