Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - BinaryFormatter problem

Reply
 
Old 06-25-2007   #1 (permalink)
Andrew


 
 

BinaryFormatter problem

I faced some problem with BinaryFormatter.
Deserialize method doesn't work when I run it from PowerShell Cmdlet, while
it works normally in test application.
I have found only one solution - place dll in GAC, is it correct way?

Here is example. It fails on deserialize method telling that it cannot load
assembly.

[Serializable]
public class Test
{
private int num;
public int Num
{
get { return num; }
set { num = value; }
}
}

ProcessRecord method of cmdlet.
protected override void ProcessRecord()
{
BinaryFormatter sr = new BinaryFormatter();
MemoryStream st = new MemoryStream();
Test t = new Test();
t.Num = 100;
sr.Serialize(st, t);
st.Position = 0;
Test t2 = (Test)sr.Deserialize(st);
st.Close();
this.WriteObject(t2);
}

My System SpecsSystem Spec
Old 06-25-2007   #2 (permalink)
RichS


 
 

RE: BinaryFormatter problem

Presumably the dll you want to use contains the .NET assembly. You could put
it in the GAC then use

$null=
[System.Reflection.Assembly]::LoadWithPartialName(partial_name_of_assembly)
or load it direct
$null = [System.Reflection.Assembly]::LoadFrom(full_path_to_assembly)
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Andrew" wrote:

> I faced some problem with BinaryFormatter.
> Deserialize method doesn't work when I run it from PowerShell Cmdlet, while
> it works normally in test application.
> I have found only one solution - place dll in GAC, is it correct way?
>
> Here is example. It fails on deserialize method telling that it cannot load
> assembly.
>
> [Serializable]
> public class Test
> {
> private int num;
> public int Num
> {
> get { return num; }
> set { num = value; }
> }
> }
>
> ProcessRecord method of cmdlet.
> protected override void ProcessRecord()
> {
> BinaryFormatter sr = new BinaryFormatter();
> MemoryStream st = new MemoryStream();
> Test t = new Test();
> t.Num = 100;
> sr.Serialize(st, t);
> st.Position = 0;
> Test t2 = (Test)sr.Deserialize(st);
> st.Close();
> this.WriteObject(t2);
> }

My System SpecsSystem Spec
Old 06-25-2007   #3 (permalink)
RichS


 
 

RE: BinaryFormatter problem

Sorry - misread your post.

If you are creating a cmdlet - you need to put a reference to the assembly
in your project and a using statement in the code
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk


"Andrew" wrote:

> I faced some problem with BinaryFormatter.
> Deserialize method doesn't work when I run it from PowerShell Cmdlet, while
> it works normally in test application.
> I have found only one solution - place dll in GAC, is it correct way?
>
> Here is example. It fails on deserialize method telling that it cannot load
> assembly.
>
> [Serializable]
> public class Test
> {
> private int num;
> public int Num
> {
> get { return num; }
> set { num = value; }
> }
> }
>
> ProcessRecord method of cmdlet.
> protected override void ProcessRecord()
> {
> BinaryFormatter sr = new BinaryFormatter();
> MemoryStream st = new MemoryStream();
> Test t = new Test();
> t.Num = 100;
> sr.Serialize(st, t);
> st.Position = 0;
> Test t2 = (Test)sr.Deserialize(st);
> st.Close();
> this.WriteObject(t2);
> }

My System SpecsSystem Spec
Old 06-25-2007   #4 (permalink)
Andrew


 
 

RE: BinaryFormatter problem

There is only one assembly (Cmdlet) containing class Test (that must be
serialized and deserialized) and Cmdlet.
Exception is like this:

Unable to find assembly xxx'.
at
System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
at
System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String
objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[]
typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32
objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
at
System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
at
System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at
System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler
handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain,
IMethodCallMessage methodCallMessage)
at
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream
serializationStream, HeaderHandler handler, Boolean fCheck, Boolean
isCrossAppDomain, IMethodCallMessage methodCallMessage)
at myCmdLets.test.ProcessRecord()

"RichS" wrote:

> Sorry - misread your post.
>
> If you are creating a cmdlet - you need to put a reference to the assembly
> in your project and a using statement in the code
> --
> Richard Siddaway
> Please note that all scripts are supplied "as is" and with no warranty
> Blog: http://richardsiddaway.spaces.live.com/
> PowerShell User Group: http://www.get-psuguk.org.uk
>
>
> "Andrew" wrote:
>
> > I faced some problem with BinaryFormatter.
> > Deserialize method doesn't work when I run it from PowerShell Cmdlet, while
> > it works normally in test application.
> > I have found only one solution - place dll in GAC, is it correct way?
> >
> > Here is example. It fails on deserialize method telling that it cannot load
> > assembly.
> >
> > [Serializable]
> > public class Test
> > {
> > private int num;
> > public int Num
> > {
> > get { return num; }
> > set { num = value; }
> > }
> > }
> >
> > ProcessRecord method of cmdlet.
> > protected override void ProcessRecord()
> > {
> > BinaryFormatter sr = new BinaryFormatter();
> > MemoryStream st = new MemoryStream();
> > Test t = new Test();
> > t.Num = 100;
> > sr.Serialize(st, t);
> > st.Position = 0;
> > Test t2 = (Test)sr.Deserialize(st);
> > st.Close();
> > this.WriteObject(t2);
> > }

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
the problem with ndis.sys Blue Screen of Death problem solved? Vista General
Re: Windows Mail Attachement Problem and Adobe Player Problem with IE8 Vista mail
Generic McAfee Problem Message in Vista Problem Reports Vista performance & maintenance
audio problem with my sony vaio laptop and x64 problem Vista hardware & devices
Vista Upgrade Problem - Windows Explorer Loop problem Vista installation & setup


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46