Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Serialization is the process of converting between an object <strong>data</strong> and bytes which can be transferred over the wire. Interfaces define <strong>behavior</strong>, so by default WCF can't serialize such data. If you have the exact same assemblies on the client and the server, however, you can use the <code>NetDataContractSerializer</code>, which will essentially serialize (and be able to serialize) all the type information for the objects being serialized, so it can be recreated at the other side.</p> <p>The code below shows how to use the NetDataContractSerializer in a service for that (based on the main example for this, the post from Aaron Skonnard at <a href="http://www.pluralsight-training.net/community/blogs/aaron/archive/2006/04/21/22284.aspx" rel="nofollow">http://www.pluralsight-training.net/community/blogs/aaron/archive/2006/04/21/22284.aspx</a>)</p> <pre><code>public class StackOverflow_6932356 { [ServiceContract] public interface IWorker { [OperationContract] void Process(XmlElement data); [OperationContract] void Update(Rule rule); } [DataContract] public class Rule { [DataMember] public string Expression { get; set; } [DataMember] public List&lt;IAction&gt; Actions { get; set; } } public interface IAction { void Execute(XmlElement data); } public class Service : IWorker { static List&lt;IAction&gt; AllActions = new List&lt;IAction&gt;(); public void Process(XmlElement data) { foreach (var action in AllActions) { action.Execute(data); } } public void Update(Rule rule) { AllActions = rule.Actions; } } public class Action1 : IAction { public void Execute(XmlElement data) { Console.WriteLine("Executing {0} for data: {1}", this.GetType().Name, data.OuterXml); } } public class Action2 : IAction { public void Execute(XmlElement data) { Console.WriteLine("Executing {0} for data: {1}", this.GetType().Name, data.OuterXml); } } class NetDataContractSerializerOperationBehavior : DataContractSerializerOperationBehavior { public NetDataContractSerializerOperationBehavior(OperationDescription operationDescription) : base(operationDescription) { } public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList&lt;Type&gt; knownTypes) { return new NetDataContractSerializer(name, ns); } public override XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList&lt;Type&gt; knownTypes) { return new NetDataContractSerializer(name, ns); } } static void ReplaceDCSOB(ServiceEndpoint endpoint) { foreach (var operation in endpoint.Contract.Operations) { for (int i = 0; i &lt; operation.Behaviors.Count; i++) { if (operation.Behaviors[i] is DataContractSerializerOperationBehavior) { operation.Behaviors[i] = new NetDataContractSerializerOperationBehavior(operation); break; } } } } public static void Test() { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(IWorker), new BasicHttpBinding(), ""); ReplaceDCSOB(endpoint); host.Open(); Console.WriteLine("Host opened"); var factory = new ChannelFactory&lt;IWorker&gt;(new BasicHttpBinding(), new EndpointAddress(baseAddress)); ReplaceDCSOB(factory.Endpoint); var proxy = factory.CreateChannel(); proxy.Update(new Rule { Expression = "Expr", Actions = new List&lt;IAction&gt; { new Action1(), new Action2() } }); XmlDocument doc = new XmlDocument(); doc.LoadXml("&lt;root&gt;&lt;foo&gt;bar&lt;/foo&gt;&lt;/root&gt;"); proxy.Process(doc.DocumentElement); ((IClientChannel)proxy).Close(); factory.Close(); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); } } </code></pre>
 

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