Issue
I am writing some code that needs to be backwards compatible with EXISTING remoting code that is using SOAP to serialize some objects.
My difficulty is that I have had to move some objects to new assemblies, so the remoting is broken.
For example, I serialize an object using the .NET SoapFormatter like this:
Person p=new Person();
string path=@"c:\myfile.soap";
using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
f = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
f.Serialize(fs, p);
fs.Close();
}
The resulting xml looks like this:
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Person id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Serialization/dotneat_net.Serialization%2C%20Version%3D0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<FirstName id="ref-3">Joe</FirstName>
<LastName id="ref-4">Doe</LastName>
<_Address id="ref-5">dotneat.net Street, Zaragoza, Spain</_Address>
<_ZIPCode id="ref-6">50007</_ZIPCode>
</a1:Person>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In that XML, I'd like to have some control over the xmlns on the a1:Person object:
<a1:Person id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Serialization/dotneat_net.Serialization%2C%20Version%3D0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
The reason is that my new Person object is not in the same assembly as the original object. So, later when deserialization occurs (in the older projects), it fails because of wrong assembly.
How can I control the text in the xmlns? I have tried a few things such as using the [SoapType Namespace="xxx"] attribute on the class that is being serialized. No luck.
I'd prefer to avoid modifying the XML manually.
Solution
I was able to set the namespace using the SoapType attribute.
[Serializable]
[System.Runtime.Remoting.Metadata.SoapType(XmlNamespace = "MY_NAMESPACE")]
public class Person
{
public string FirstName;
public string LastName;
public string _Address;
public string _ZIPCode;
}
This generated the following serialized XML:
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<i2:Person id="ref-1" xmlns:i2="MY_NAMESPACE">
<FirstName id="ref-3">John</FirstName>
<LastName id="ref-4">Doe</LastName>
<_Address id="ref-5">otneat.net Street, Zaragoza, Spain</_Address>
<_ZIPCode id="ref-6">50007</_ZIPCode>
</i2:Person>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
However, I wasn't able to verify the deserialization on the other side of the remoting channel. Hopefully, that helps you.
Answered By - Randy Levy Answer Checked By - Willingham (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.