Поделиться через


Line Breaks in Managed Web Service Proxy Classes

Matt, Rick, and I were working on an issue recently where when an application using EWS would set a contact’s Street address to a value containing a carriage return and line feed, like this:

physicalAddress.Street = "1234 56 Ave NE\r\nc/oPatrick Creehan";

the address card control in Outlook would render it like this:

image

Ugly, right? The problem was that the XMLSerializer would strip out the line feed and leave the carriage return, which the address card didn’t like.

We could prove by sending raw XML requests in a separate application that sending 
 for the carriage return line feed would make everything right, however, if we set the street address like this:

physicalAddress.Street = "1234 56 Ave NE
c/oPatrick Creehan";

then the contact’s address card would look like this:

image

Even uglier! It seems that the .net framework, in an attempt to help us out is encoding our string for XML but it wasn’t letting us specify the value we knew was right.

So – the solution is to implement your own class which can handle the XmlSerialization yourself, and replace the auto-generated proxy class’s decision for the type to yours.

Here’s my simple class:

  1.     [SoapTypeAttribute(Namespace = "https://schemas.microsoft.com/exchange/services/2006/types",TypeName="text")]

  2.     public class mstring:IXmlSerializable  

  3.     {

  4.         private string m_string = string.Empty;

  5.         public override string ToString()

  6.         {

  7.             return m_string;

  8.         }

  9.         

  10.         public static mstring CreateMString(string str){

  11.             mstring newmstring = new mstring();

  12.             newmstring.m_string = str;

  13.             return newmstring;

  14.         }

  15.         public static implicit operator mstring(string str)

  16.         {

  17.             return CreateMString(str);

  18.         }

  19.         #region IXmlSerializable Members

  20.         public System.Xml.Schema.XmlSchema GetSchema()

  21.         {

  22.             return new System.Xml.Schema.XmlSchema();

  23.         }

  24.         public void ReadXml(XmlReader reader)

  25.         {

  26.             m_string = reader.ReadContentAsString();

  27.         }

  28.         public void WriteXml(XmlWriter writer)

  29.         {

  30.             string outString = m_string;

  31.             outString = HttpUtility.HtmlEncode(outString);

  32.             outString = outString.Replace("\r", "
");

  33.             outString = outString.Replace("\n", "
");

  34.             writer.WriteRaw(outString.ToString());

  35.         \

A few things to point out is that I decorated this class with the XML namespace for the Exchange Web Services so that it doesn’t fail schema validation. Also, I didn’t really test whether this works when binding to an existing contact – there may be more work needed in the ReadXML section. In order to support still setting the Street property to a string, I had to override the implicit operator. That allows me to set Street to a string even though technically, now Street is an “mstring.” You’ll notice that the work of actually writing the correct value occurs in WriteXml which we got by implementing IXmlSerializable. Now when the SOAP infrastructure goes to build the request, it will call into our interface to serialize this class.

That reminds me, the last thing you need to do to hook all this up is to go into the web service proxy class Reference.cs and modify the PhysicalAddressDictionaryEntryType so the street properties use your new mstring class instead of string:

  1.     /// <remarks/>

  2.     [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4918")]

  3.     [System.SerializableAttribute()]

  4.     [System.Diagnostics.DebuggerStepThroughAttribute()]

  5.     [System.ComponentModel.DesignerCategoryAttribute("code")]

  6.     [System.Xml.Serialization.XmlTypeAttribute(Namespace="https://schemas.microsoft.com/exchange/services/2006/types")]

  7.     public partial class PhysicalAddressDictionaryEntryType {

  8.         

  9.         private mstring streetField;

  10.         

  11.         private string cityField;

  12.         

  13.         private string stateField;

  14.         

  15.         private string countryOrRegionField;

  16.         

  17.         private string postalCodeField;

  18.         

  19.         private PhysicalAddressKeyType keyField;

  20.         

  21.         /// <remarks/>

  22.         public mstring Street {

  23.             get {

  24.                 return this.streetField;

  25.             }

  26.             set {

  27.                 this.streetField = value;

  28.             }

  29.         }

  30.         /// <remarks/>

  31.         public string City {

  32.             get {

  33.                 return this.cityField;

  34.             }

  35.             set {

  36.                 this.cityField = value;

  37.             }

  38.         }

  39.         

  40.         /// <remarks/>

  41.         public string State {

  42.             get {

  43.                 return this.stateField;

  44.             }

  45.             set {

  46.                 this.stateField = value;

  47.             }

  48.         }

  49.         

  50.         /// <remarks/>

  51.         public string CountryOrRegion {

  52.             get {

  53.                 return this.countryOrRegionField;

  54.             }

  55.             set {

  56.                 this.countryOrRegionField = value;

  57.             }

  58.         }

  59.         

  60.         /// <remarks/>

  61.         public string PostalCode {

  62.             get {

  63.                 return this.postalCodeField;

  64.             }

  65.             set {

  66.                 this.postalCodeField = value;

  67.             }

  68.         }

  69.         

  70.         /// <remarks/>

  71.         [System.Xml.Serialization.XmlAttributeAttribute()]

  72.         public PhysicalAddressKeyType Key {

  73.             get {

  74.                 return this.keyField;

  75.             }

  76.             set {

  77.                 this.keyField = value;

  78.             }

  79.         }

  80.     \

and (after removing the email address so the address will fit on the card) it looks like this:

image

 

Cake.