Connecting Tech Pros Worldwide Forums | Help | Site Map

Assembly serialization - how to do it?

Dejan
Guest
 
Posts: n/a
#1: Nov 21 '05
Hello!

I need to make an ASP.NET XML Web Service serving as an assembly
container. Web Service loads the assembly from a given file, and
returns that assembly to the caller.
Since ASP.NET doesn't support serialization of objects of type
System.Reflection.Assembly into XML, I decided to use SoapFormatter or
BinaryFormatter class to serialize assembly object into memory stream,
then construct byte array from memory stream, and finaly return that
byte array to the caller. It seems to work.

Problem occurs on the client side when I'm trying to deserialize
assembly object from memory stream. Client application throws
following exception:

An unhandled exception of type
'System.Runtime.Serialization.SerializationExcepti on' occurred in
mscorlib.dll
Additional information: Insufficient state to deserialize the object.
More information is needed.



Here is sample of code:


Web Service:
[WebMethod]
public byte[] GetAssembly( string assFile )
{
Assembly ass = Assembly.LoadFrom( assFile );

IFormatter formatter = new SoapFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize( stream, ass );

return stream.ToArray();
}




Client application:
[STAThread]
static void Main(string[] args)
{
localhost.AssemblyContainer ac = new localhost.AssemblyContainer();

byte[] assByteArray = ac.GetAssembly("someFile.dll");

MemoryStream stream = new MemoryStream( assByteArray );
IFormatter formater = new SoapFormatter();
Assembly ass = formater.Deserialize( stream );
}



Here is the trace of memory stream, ie. serialized assembly object:

<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:UnitySerializationHolder id="ref-1"
xmlns:a1="http://schemas.microsoft.com/clr/ns/System">
<Data id="ref-2">SemaphoreResource, Version=1.0.1600.33496,
Culture=neutral, PublicKeyToken=null</Data>
<UnityType>6</UnityType>
<AssemblyName href="#ref-2"/>
</a1:UnitySerializationHolder>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>





There is no difference if I use BinaryFormatter instead of
SoapFormatter.

Could anyone answer me why this doesn't work?
Does anyone know how to serialize assembly objects?

Thanks in advance,
Dejan

Ollie
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Assembly serialization - how to do it?


why not open the file in binary mode and read the file stream and returns
this as an array....

witha "Dejan" <dejan.skvorc@ck.htnet.hr> wrote in message
news:9e1fd687.0407080311.1bd42fc7@posting.google.c om...[color=blue]
> Hello!
>
> I need to make an ASP.NET XML Web Service serving as an assembly
> container. Web Service loads the assembly from a given file, and
> returns that assembly to the caller.
> Since ASP.NET doesn't support serialization of objects of type
> System.Reflection.Assembly into XML, I decided to use SoapFormatter or
> BinaryFormatter class to serialize assembly object into memory stream,
> then construct byte array from memory stream, and finaly return that
> byte array to the caller. It seems to work.
>
> Problem occurs on the client side when I'm trying to deserialize
> assembly object from memory stream. Client application throws
> following exception:
>
> An unhandled exception of type
> 'System.Runtime.Serialization.SerializationExcepti on' occurred in
> mscorlib.dll
> Additional information: Insufficient state to deserialize the object.
> More information is needed.
>
>
>
> Here is sample of code:
>
>
> Web Service:
> [WebMethod]
> public byte[] GetAssembly( string assFile )
> {
> Assembly ass = Assembly.LoadFrom( assFile );
>
> IFormatter formatter = new SoapFormatter();
> MemoryStream stream = new MemoryStream();
> formatter.Serialize( stream, ass );
>
> return stream.ToArray();
> }
>
>
>
>
> Client application:
> [STAThread]
> static void Main(string[] args)
> {
> localhost.AssemblyContainer ac = new localhost.AssemblyContainer();
>
> byte[] assByteArray = ac.GetAssembly("someFile.dll");
>
> MemoryStream stream = new MemoryStream( assByteArray );
> IFormatter formater = new SoapFormatter();
> Assembly ass = formater.Deserialize( stream );
> }
>
>
>
> Here is the trace of memory stream, ie. serialized assembly object:
>
> <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:UnitySerializationHolder id="ref-1"
> xmlns:a1="http://schemas.microsoft.com/clr/ns/System">
> <Data id="ref-2">SemaphoreResource, Version=1.0.1600.33496,
> Culture=neutral, PublicKeyToken=null</Data>
> <UnityType>6</UnityType>
> <AssemblyName href="#ref-2"/>
> </a1:UnitySerializationHolder>
> </SOAP-ENV:Body>
> </SOAP-ENV:Envelope>
>
>
>
>
>
> There is no difference if I use BinaryFormatter instead of
> SoapFormatter.
>
> Could anyone answer me why this doesn't work?
> Does anyone know how to serialize assembly objects?
>
> Thanks in advance,
> Dejan[/color]


Closed Thread