background preloader

Serialization

Facebook Twitter

Protobuf-net - Fast, portable, binary serialization for .NET. Introduction protocol buffers is the name of the binary serialization format used by Google for much of their data communications. It is designed to be: small in size - efficient data storage (far smaller than xml) cheap to process - both at the client and server platform independent - portable between different programming architectures extensible - to add new data to old messages protobuf-net is a .NET implementation of this, allowing you to serialize your .NET objects efficiently and easily. The short version (see also the old home page) Serialization is a pain. protobuf-net is designed to be easily used on your existing code with minimal changes (of from an optional .proto schema), enabling fast and portable binary serialization on a wide range of .NET platforms. Usage is very simple; at the most basic level, simply read from a stream or write to a stream; see Getting Started: // write to a fileSerializer.Serialize(outputStream, person); "v2" released.

Enumeration Classes and WCF | Outlawtrail - .NET Development. During the last week I was working on a real fun project. It started after I stumbled across an old article by Jimmy Bogard , author of the famous AutoMapper library. In his post he talks about Enumeration Classes . What they are, how they work and what their advantages over simple enums are. He started by stating that a certain question evolved from a discussion on ALT.NET: “What do you do about enumerations crossing service boundaries?”. His post is inspiring but he did not really give an answer to that exact question. Although WCF is by no means the only way to implement “services” this unanswered question got me thinking: “What if I want to use functionality-enhanced enumeration classes in a WCF service and simple enums on the client side?”

I know that WCF was designed to “talk dirty” when it comes to conversion between client- and server-side objects. But can it talk enums on the client and enumeration classes on the server? The SortOrder Enumeration Class Serialization is easy. Enumeration Classes, WCF and Service Metadata | Outlawtrail - .NET Development. IObjectReference Interface (System.Runtime.Serialization) Using XML Schema Import and Export for XmlSerializer - Youssef M's Blog. In a previous post, I outlined how you could import and export the XML schema for a type that you’re serializing with DataContractSerializer. Here’s how to do the same thing if you’re serializing objects with XmlSerializer: static void RoundTripXmlMetadata( Type type) XmlSchemas schemas = new XmlSchemas (); XmlSchemaExporter exporter = new XmlSchemaExporter (schemas); //Import the type as an XML mapping XmlTypeMapping mapping = new XmlReflectionImporter ().ImportTypeMapping(type); //Export the XML mapping into schemas exporter.ExportTypeMapping(mapping); //Print out the schemas foreach ( object schema in schemas) (( XmlSchema )schema).Write( Console .Out); Console .WriteLine( "\n" ); Console .WriteLine( "-------------------------------------------" ); //Compile the schemas into one logical schema schemas.Compile((o, args) => Console .WriteLine(args.Message), true ); XmlSchemaImporter importer = new XmlSchemaImporter (schemas); //Import the schema element back into an XML mapping public class Animal.

Serializing Object Graphs Without and With References. Updated: 11/25/2007 and 12/12/2007 (Added C# class definitions, plus C# and VB object initialization code. See below.) Windows Communication Foundation (WCF) defaults to the DataContractSerializer (DCS) for serializing services' object graph payloads to well-formed XML Infosets. DCS is capable of serializing 1:many associations of an object graph, such as Customers -> Orders -> OrderDetails, into a hierarchical XML Infoset, which has a structure that any WS-* SOAP client should be able to deserialize correctly. An alternative to DCS is the NetDataContractSerializer (NDCS), which can serialize graphs with 1:many and many:1 (bidirectional) associations by preserving association references in ID/IDREF pairs of Id and Ref values. NDCS delivers graph fidelity at the expense of interoperability because it requires clients to support CLR types.

Note: The Entity Framework (EF) maps associations to Navigation Properties. Serializing Partial Object Graphs with DCS and to deserialize it is: Serialize object graph to XML in .Net | Martin Konicek. /class/System.Runtime.Serialization/System.Runtime.Serialization/ Untitled. POCO Proxies Part 1 | chainding. The Entity Framework (EF) in .NET 4.0 introduces the ability to interact with entities with persistence ignorance (POCOs).

While this favors separation of concerns, it also means that the entities will do less because they cannot talk directly to the data access layer. Another new concept in EF 4.0 is the idea of dynamically generated proxies for such entities. Opting into proxies is an easy way to get additional capabilities like lazy loading and change tracking, while still keeping the entity class definition and implementation simple. This is the first in a two part series that describes how proxies work in the Entity Framework. Proxies in a Nutshell Proxies are a form of dependency injection where additional functionality is injected into the entity.

The EF generates proxy types on the fly using the AssemblyBuilder and TypeBuilder in the .NET framework. For the EF to be able to create a dynamically derived type, a couple of things need to be done: Proxy creation must be turned on. Vega.frugalware.org/tmpgit/mono/mcs/class/System.Runtime.Serialization/Test/System.Runtime.Serialization/XsdDataContractExporterTest.cs. .NET Development >>Problem with mixing XmlReflectionImporter and XmlSchemaProviderAttribute. I have three classes: A, B and C. A and B use the XmlSchemaProviderAttribute to define some specific schema. A uses the XmlReflectionImporter to get B's schema. Class B is nothing special, but has a ref to C. The resulting schema is missing the definition for C however (when dumping the schema's to the console). This seems to be a problem with the XmlReflectionImporter. Wout Here's some code to reproduce (used in context of web services where I'm trying to avoid having property setters, just to satisfy the XmlSerializer, it would be awesome if you guys just added a .NET attribute to be able to serialize a property that only has a getter, I've run into this issue a lot by now): using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; [XmlSchemaProvider("ProvideSchema")] public class A { private B b; public static XmlQualifiedName ProvideSchema(XmlSchemaSet schemaSet) { XmlSchemas schemas = new XmlSchemas();

Xsd annotations in WCF. This post has the goal to explain how it is possible to make Xsd annotations in the Wsdl generated by Windows Communication Foundation (the version we are using in our project is WinFx 3.0 Beta 2 Go-Live). Unfortunately, Indigo does not allow to specify an annotation in the DataMember attribute. So, we can't do this, [DataContract] public class MyDataContract { [DataMember(XsdAnnotation = " MyAnnotation " )] public string MyDataMember; } What we want is something like this, [DataContract] public class MyDataContract { [DataMember] [DataMemberAnnotation( " MyAnnotation " )] public string MyDataMember; } and in the generated Wsdl we expect to have in our schema an Xsd annotation associated with the element MyDataMember.

First of all we must create our attribute, [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true , AllowMultiple = false )] public class DataMemberAnnotationAttribute : Attribute { private string m_Annotation; { ... If (dataMemberAnnotations ! Serializing .NET Objects Into XML – How to Retrieve XML Schema From an Interface.

If you need to serialize your business objects into XML you may also need to generate automatically XML Schema so you can check the XML against that schema before try to deserialize it. This is one utility that will do that for you. Now, let’s first see one utility that will do basic XML serialization and deserialization objects. public static class SerializationUtil { private static readonly Dictionary<Type, XmlSerializer> _serializersCache = new Dictionary<Type, XmlSerializer>(); private static XmlSerializer GetSerializer(Type type) { XmlSerializer xs; if (! You can use it as: // serialize business object into xml string var xmlString = SerializationUtil.ToXml<MyObj>(obj); // deserialize xml string into business object var obj = SerializationUtil.FromXml<MyObj>(xmlString); Now, to generate XML schema from the interface your object implements you can use this utility: The way you use it is as follow: var xsdString = InterfaceToXsdUtil.ToXsd<IMyBusinessObject>();

Dan Rigsby » XmlSerializer vs DataContractSerializer: Serialization in Wcf. The XmlSerializer has been in .Net since version 1.0 and has served us well for everything from Remoting, Web Services, serializing to a file, etc. However in .Net 3.0 the DataContractSerializer came along. And all of a sudden a lot of guidance suggests that we should use it over the old tried and true XmlSerializer. Wcf even uses this as the default mechanism for serialization. The question is, “Is it really better?”. The verdict is yes, and no. Like most things it depends on your implementation and what you need. For Wcf, you should prefer to use the DataContractSerializer. Lets look at the both of these in detail and leave it up to you to decide which is best for your implementation. What is Serialization? Let’s start with the basics. Deserialization is basically the reverse of serialization. What is the XmlSerialzer?

For those that may not be familiar with System.Xml.Serialization.XmlSerializer let’s go over it briefly. However not just any object can be serialized. Schema generation with xsd.exe and IXmlSerializable.GetSchema() Excellent!! I've tested it in my implementation and it looks like it works perfect. Thanks a lot for your time, Elena... Edit : Err...

In fact, the only problem I see is that the "xxx" element in your implementation is available from the root, even if I specify to xsd.exe to serialize only "Test". Edit 2 : And an additional note, if you have the following structure : ______________ public class Root { public Test Test; } public class Test { public TestList List; } public class TestList : SampledList<ListItem> { } public class ListItem { public int id; } [XmlSchemaProvider("ProvideSchema")] public class SampledList<T> : List<T>, IXmlSerializable { ... } ______________ ..., the xsd.exe app does not find the [XmlSchemaProvider] attribute and will not serialize the schema using the method. And everything works. :) Dan Rigsby » XmlSerializer vs DataContractSerializer: Serialization in Wcf.

Having fun with the XmlSchemaProvider: Serializing object trees. Download source - 17.7 Kb Introduction About a year ago, I became very enthusiastic. I found a new attribute in the framework which allows me to determine the WSDL contract which describes my objects. Used in conjunction with the IXmlSerializable interface, I can also make sure the serializer pumps out the correct XML, corresponding to the custom XML-Schema inside the WSDL. Why is this so important? The default XmlSerializer requires all fields to have public get / set properties. Now, all of this sounds good of course, and there are various samples on the internet showing how you can use these two classes to serialize your own ‘ Product ’ objects. Not finding the answer on the web, I dug into this problem myself, like I did about a year ago. Serializing an Order object The following example will show how you can utilize XmlSchemaProvider for serializing an object-tree.

The Order class I’ll describe a bit further, since that’s where the magic happens. Now for some deserialization. WCF Overview | Philip Hendry's Blog. Links Tools Install the Windows SDK to use the Service Configuration Editor(SvcConfigEditor.exe) which helps writing config files. The Service Trace Viewer (SvcTraceViewer.exe) can be used to view log files (for example us the config editor to configure WCF logging then use the viewer to look at the results.) Introduction Windows Communication Foundation is part of the Microsoft .Net Framework V3.0 and is part of Vista and Windows Server 2008. It’s Microsoft’s platform for SOA and unifies ASMX, .NET Remoting and Enterprise Services stacks.

Because there’s a single API it makes it easier to build for a multitude of different circumstances. SOA simply defines a contract for accessing a particular service and makes it available via some means of communication protocol. WCF is interoperable since it supports core Web services standards and supports integration since it integrates with earlier technologies such as COM, Enterprise Services and MSMQ. Connectivity Basics Example Hosting WCF Client.