473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

InvalidCastException on a Deserialized object inside aTransperantProxy call

Hi,

I'm trying to deserialize an object on the context of a remoting call
(RPC).
The deserialization seem to work fine, however when I'm casting the
result to the relevant class I get an InvalidCastException.

Note:
* the remoting server has TypeFilterLevel to Full to remove security
restrictions.
* the casting has no problem when done in the immediate window, or in
another application without remoting involved.
* The serializing and deserializing DLL's are the same and from the
same build.
void func() // called by Remoting
{
Stream myStream = File.OpenRead(shortFileName +
"_env_stream.bin");
object obj = bf.Deserialize(myStream); // bf is of
type BinaryFormatter
myClass = (MyNameSpace.MyClass)obj; // ERROR - InvalidCastException

Assembly a =
System.Reflection.Assembly.GetAssembly(myClass.Get Type());
Assembly b =
System.Reflection.Assembly.GetAssembly(obj.GetType ());
bool isSameAssembly = a.Equals(b); // this is False
when func is called in Remoting
}
Nov 6 '08 #1
4 3547
Where is the MyNameSpace.MyClass declared?

It should be in a common assembly which is being used by both the ends.
Just having the same code (even the same namespace) in both the client
and the server doesn't make the class same.

When you cast it, apart from the class name, it's assembly name is also
compared.

Thanks & Regards,
Ashutosh
ro*****@gmail.com wrote:
Hi,

I'm trying to deserialize an object on the context of a remoting call
(RPC).
The deserialization seem to work fine, however when I'm casting the
result to the relevant class I get an InvalidCastException.

Note:
* the remoting server has TypeFilterLevel to Full to remove security
restrictions.
* the casting has no problem when done in the immediate window, or in
another application without remoting involved.
* The serializing and deserializing DLL's are the same and from the
same build.
void func() // called by Remoting
{
Stream myStream = File.OpenRead(shortFileName +
"_env_stream.bin");
object obj = bf.Deserialize(myStream); // bf is of
type BinaryFormatter
myClass = (MyNameSpace.MyClass)obj; // ERROR - InvalidCastException

Assembly a =
System.Reflection.Assembly.GetAssembly(myClass.Get Type());
Assembly b =
System.Reflection.Assembly.GetAssembly(obj.GetType ());
bool isSameAssembly = a.Equals(b); // this is False
when func is called in Remoting
}
Nov 6 '08 #2
Check that your exe can only get at one copy of the dll in question.

Something that's happened to me on occasion is that an exe has 2 copies of
the same dll available to it. This can happen if you build your exe to one
directory, and put your dlls in a "libs" sub-directory. If the dll ends up
in the same directory as the exe, and in the libs directory as well (by some
mistake in the build process, or copying by accident) then 2 copies of the
dll end up getting loaded - one from one directory, and another from a
different directory. An object created by, or deserialized by one instance
of the dll cannot be accessed by the other version.

This scenario might not fit your probem at all, of course, but I thought it
was worth mentioning.

Cheers,

Adam.
============
Nov 7 '08 #3
Yes it is loaded from the same assembly.

the serializing program has the exact same complied DLL as the
desrializing.

Thanks,

Roy.

On Nov 6, 7:19*pm, Ashutosh Bhawasinka <discuss...@ashutosh.inwrote:
Where is the MyNameSpace.MyClass declared?

It should be in a common assembly which is being used by both the ends.
Just having the same code (even the same namespace) in both the client
and the server doesn't make the class same.

When you cast it, apart from the class name, it's assembly name is also
compared.

Thanks & Regards,
Ashutosh

royn...@gmail.com wrote:
Hi,
I'm trying to deserialize an object on the context of a remoting call
(RPC).
The deserialization seem to work fine, however when I'm casting the
result to the relevant class I get an InvalidCastException.
Note:
* the remoting server has TypeFilterLevel to Full to remove security
restrictions.
* the casting has no problem when done in the immediate window, or in
another application without remoting involved.
* The serializing and deserializing DLL's are the same and from the
same build.
void func() // called by Remoting
{
* * * * * * * * Stream myStream = File.OpenRead(shortFileName +
"_env_stream.bin");
* * * * * * * * object obj = bf.Deserialize(myStream); // bf is of
type BinaryFormatter
* * * * * *myClass = (MyNameSpace.MyClass)obj; // ERROR -InvalidCastException
* * * * * * * * Assembly a =
System.Reflection.Assembly.GetAssembly(myClass.Get Type());
* * * * * * * * Assembly b =
System.Reflection.Assembly.GetAssembly(obj.GetType ());
* * * * * * * * bool isSameAssembly = a.Equals(b); //this is False
when func is called in Remoting
}
Nov 13 '08 #4
On Nov 7, 1:23*pm, "Adam Benson"
<Adam.Ben...@NOSPAMMYSPAM.omnibus.co.ukwrote:
Check that your exe can only get at one copy of the dll in question.

Something that's happened to me on occasion is that an exe has 2 copies of
the same dll available to it. This can happen if you build your exe to one
directory, and put your dlls in a "libs" sub-directory. If the dll ends up
in the same directory as the exe, and in the libs directory as well (by some
mistake in the build process, or copying by accident) then 2 copies of the
dll end up getting loaded - one from one directory, and another from a
different directory. An object created by, or deserialized by one instance
of the dll cannot be accessed by the other version.

This scenario might not fit your probem at all, of course, but I thought it
was worth mentioning.

Cheers,

Adam.
============
Hi Adam,

this is not the case, the dll's are from the same assembly.
Nov 13 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Johan Appelgren | last post by:
Hi, I'm trying to serialize a class hierarchy using System.Xml.Serialization.XmlSerializer from a class that is loaded as a Com object. But it fails with a InvalidCastException. I don't know...
4
by: Ram | last post by:
I am running a test web site on my own Win2k professional computer. I have a VB6 COM component (running on COM+) on a seperate application runnint as "Server". When running an ASP.NET page calling...
7
by: MounilK | last post by:
Hi all, I have a form which has a TabControl and a Button. At runtime, on the TabControl, I create a TabPage with a TextBoxes. Now, when I click the button after entering some text in the textbox,...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
0
by: Ray Cassick \(Home\) | last post by:
Ok, I am about at the end of my rope here. Been looking at this code for hours and can not see what is wrong with it. I have an object saved to a file using Binary Serialization. The file has...
0
by: JGKjr | last post by:
I have a Web Application written in C#. I am trying to call a web service method and recieve the error: System.InvalidCastException: Cannot assign object of type System.Boolean to an object of...
5
by: Geoff Kell (Lockdesign Ltd) | last post by:
I have just started using VB.Net and I am developing a class to provide data to clients from an IBM-MQ queue. I have developed a class which connects to the queue and issues a read. When a...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
1
by: =?Utf-8?B?QkpT?= | last post by:
I have written a Generic method that takes an object, a type, T, and returns a System.Nullable (Of T) depending on whether or not the object IsDBNull. It was working fine until I tried to pass a...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.