Search

Locations of visitors to this page

Categories

On this page

DataContract Serializer and IsReference property
ConfigurationErrorsException: This element is not currently associated with any context
ReceiveActivity authorization/security

Archive

Blogroll

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

RSS 2.0 | Atom 1.0 | CDF

Send mail to the author(s) E-mail

Total Posts: 50
This Year: 6
This Month: 1
This Week: 1
Comments: 69

Sign In

 Tuesday, May 20, 2008
Tuesday, May 20, 2008 1:19:16 PM (GMT Standard Time, UTC+00:00) ( )

In .net Framework 3.5 SP1, DataContractSerializer supports by-ref object graph serialization by using the standard xsd:ID/xsd:IDREF attributes.

You can set the IsReference=true on your DataContract definition and serializer will generate XML elements with IDs/IDREFs attributes and will link them together rather embedding them inside each other(default behavior).

Also if you examine the XSD generated by WCF as part of the metadata export, it will also contain the standard ID/IDREF xml schema attributes. Because of this, xml can be correctly parsed and understood by any framework in a standard way.

This change will enable serialization of object graphs having circular references (which wasn’t possible previously – at least not without writing custom code) and will also reduce the size of the serialized xml.

Let’s examine this change using the following DataContract definition:

    [DataContract]

    public class Employee

    {

        [DataMember]

        public string Name { get; set; }

        [DataMember]

        public Employee Manager { get; set; }

    }

    [DataContract]

    public class Department

    {

        [DataMember]

        public List<Employee> Staff { get; set; }

        [DataMember]

        public string DeptName { get; set; }

    }

Now if we serialize following Department object using DataContractSerializer

        var kenny = new Employee() { Name = "Kenny" };

        var bob = new Employee() { Name = "Bob", Manager = kenny };

        var alice = new Employee() { Name = "Alice", Manager = kenny };

        var ahmed = new Employee() { Name = "Ahmed", Manager = kenny };

 

        var dept = new Department() { DeptName = "RandD", Staff = new List<Employee>() { kenny, bob, alice, ahmed } };       

        DataContractSerializer dcs = new DataContractSerializer(typeof(Department));       

        var ms = new MemoryStream();

        dcs.WriteObject(ms, dept);

        ms.Seek(0, SeekOrigin.Begin);

       

        var sr = new StreamReader(ms);

        var xml = sr.ReadToEnd();

We will get this xml.

<Department xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication2" xmlns:i="http://www.w3.org/2001/XMLSchemainstance">

      <DeptName>RandD</DeptName>

      <Staff>

            <Employee>

                  <Manager i:nil="true" />

                  <Name>Kenny</Name>

            </Employee>

            <Employee>

                  <Manager>

                        <Manager i:nil="true" />

                        <Name>Kenny</Name>

                  </Manager>

                  <Name>Bob</Name>

            </Employee>

            <Employee>

                  <Manager>

                        <Manager i:nil="true" />

                        <Name>Kenny</Name>

                  </Manager>

                  <Name>Alice</Name>

            </Employee>

            <Employee>

                  <Manager>

                        <Manager i:nil="true" />

                        <Name>Kenny</Name>

                  </Manager>

                  <Name>Ahmed</Name>

            </Employee>

      </Staff>

</Department>

You can see manager Kenny is included in all Employee objects, essentially a by-value inclusion.  Now if we change the declaration of Employee class to following:

    [DataContract(IsReference = true)]

    public class Employee

    {

        [DataMember]

        public string Name { get; set; }

        [DataMember]

        public Employee Manager { get; set; }

    }

With above change, you will get following different xml.

<Department xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication2" xmlns:i="http://www.w3.org/2001/XMLSchemainstance">

      <DeptName>R&D</DeptName>

      <Staff>

            <Employee z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">

                  <Manager i:nil="true" />

                  <Name>Kenny</Name>

            </Employee>

            <Employee z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">

                  <Manager z:Ref="i1" />

                  <Name>Bob</Name>

            </Employee>

            <Employee z:Id="i3" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">

                  <Manager z:Ref="i1" />

                  <Name>Alice</Name>

            </Employee>

            <Employee z:Id="i4" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">

                  <Manager z:Ref="i1" />

                  <Name>Ahmed</Name>

            </Employee>

      </Staff>

</Department>

In attribute-free (POCO) world:

you can use a different ctor, taking a boolean flag, to toggle by-val/by-ref serialization.

      DataContractSerializer(Type type, IEnumerable<Type> knownTypes, int maxItemsInObjectGraph, bool ignoreExtensionDataObject, bool preserveObjectReferences, IDataContractSurrogate dataContractSurrogate)

To enable circular references for operation or service scope, you can use custom behaviors etc. Essentially you need the ability to hook into serializer instantiation process and create the instance using above overload:

   1. Subclass DataContractSerializerOperationBehavior

   2. Ovverride CreateSerializer method

   3. Create a new DCS instance passing true to preserveObjectReferences param.

class DataContractSerializerOperationBehaviorEx : DataContractSerializerOperationBehavior

{

public DataContractSerializerOperationBehaviorEx(OperationDescription operation):base(operation)

{

}

public override XmlObjectSerializer CreateSerializer(Type type, string name, string ns, IList<Type> knownTypes)

{

return new DataContractSerializer(type, name, ns, knownTypes, this.MaxItemsInObjectGraph, this.IgnoreExtensionDataObject, true, this.DataContractSurrogate);

}

public override XmlObjectSerializer CreateSerializer(Type type, System.Xml.XmlDictionaryString name, System.Xml.XmlDictionaryString ns, IList<Type> knownTypes)

{

return new DataContractSerializer(type, name, ns, knownTypes, this.MaxItemsInObjectGraph, this.IgnoreExtensionDataObject, true, this.DataContractSurrogate);

}

}

Comments [0] | | # 
 Sunday, April 06, 2008
Sunday, April 06, 2008 10:49:28 AM (GMT Standard Time, UTC+00:00) ( )

Sometimes when debugging WCF code you might get this strange exception. In normal situations this exception is handled by WCF api and never reaches your code. If however if you have configured VS to report exception as they are thrown, WCF api never gets the chance to deal this exception.

A simple fix is to uncheck the setting for "break when an exception is thrown".

On Debug menu, select Exceptions... & uncheck the circled option.

 

Comments [1] | | # 
 Tuesday, March 25, 2008
Tuesday, March 25, 2008 1:19:12 PM (GMT Standard Time, UTC+00:00) ( WF )

ReceieveActivity shipped as part of .net Framework 3.5 enables you to receive data in your workflows by using web services. RecieveActivity also provides a way to enable Role based authorization so you can specify who can send data into your workflows.

By default, RBAC is done against windows principal and you can change this using the serviceAuthorizationBehavior

<serviceAuthorization principalPermissionMode="Custom"/>

There is a small bug in the current UI. If you enter the value in Role field to a “Windows Group” i.e Administrators and leave the Name field blank then everything works fine.

However if you enter a value in the name field say “zuahmed” and click Ok and now come back to this dialog and clear the Name field. Now if you run the application, you will always get an “Access Denied” exception.

 

So what’s happening in the background is:

When you clear the value in the Name field and hit Ok this will set the value of PrincipalPermissionName property to an empty string(“”) rather than a null (the default). Now if you only want to authorize access on group membership, an identity value of null must be used otherwise you will access denied. Currently there is no way to fix this using the UI. You have go into the designer generated code and reset the value of typedoperationinfo1.PrincipalPermissionName = null.

This is now a known issue and will be hopefully fixed in next SP.

Comments [0] | | #