Passing Complex objects to MSMQ

Y

yonirabin

Hello,
I am building a system in C# 2.0 using the master - worker design
pattern
The system is supposed to work as follows:
1. The Master sends Command objects to several workers
2. All Commands are derived from AbstractCommand class (but are
significantly different from each other)

Since the system is distributed and should be presistant, I want to use

MSMQ to pass the Command object to the Workers
The object which I send to the MSMQ is a message containing an object
of type Abstract Command.
The problem is that MSMQ needs the objects passed into the queue to be
serializable.
In order to maintain abstraction and reusability, I want to send the
AbsCommand in the message rather then a specific type of command. when
I try to do this, the MSMQ throws exceptions because of the
serialization of the abstract command contained in the message.


Is there anyway to get around this and to send an object to the msmq
which contains an interface or an abstract object?


Thanks,
Yoni Rabin


//In this illustration, I need to send the message object to the MSMQ
and later read it
// on the other side
[Serializable]
public class message
{
public abstract AbstractCommand;



}


[Serializable]
public abstract class AbstractCommand
{


}


[Serializable]
public class MyCommand:AbstractCommand
{
public string property;
 
A

Alan Pretre

I try to do this, the MSMQ throws exceptions because of the
serialization of the abstract command contained in the message.

What is the error message that you are getting?

-- Alan
 
S

sloan

In 1.1, I am going this. I have an interface, and just before I send it to
the queue as a message ( and the message.Body ), I cast it as the
IInterface.

IAnimal
void Go();

Dog : IAnimal
Cat : IAnimal


I'd check out http://www.eggheadcafe.com/articles/20041204.asp
where Peter does a great job of bringing the Command Pattern and the MSMQ
together.

My send method looks like this

SendAMessage (IAnimal anm )
{

}

and right before I call the method I go a:

Dog d = new Dog();

this.SendAMessage ( (IAnimal) d );


Anyway... maybe that's not the issue in 2.0, but I'd thought I'd share what
I knew.
 
S

sloan

PS

IAnimal has to be serializable.



sloan said:
In 1.1, I am going this. I have an interface, and just before I send it to
the queue as a message ( and the message.Body ), I cast it as the
IInterface.

IAnimal
void Go();

Dog : IAnimal
Cat : IAnimal


I'd check out http://www.eggheadcafe.com/articles/20041204.asp
where Peter does a great job of bringing the Command Pattern and the MSMQ
together.

My send method looks like this

SendAMessage (IAnimal anm )
{

}

and right before I call the method I go a:

Dog d = new Dog();

this.SendAMessage ( (IAnimal) d );


Anyway... maybe that's not the issue in 2.0, but I'd thought I'd share what
I knew.






Hello,
I am building a system in C# 2.0 using the master - worker design
pattern
The system is supposed to work as follows:
1. The Master sends Command objects to several workers
2. All Commands are derived from AbstractCommand class (but are
significantly different from each other)

Since the system is distributed and should be presistant, I want to use

MSMQ to pass the Command object to the Workers
The object which I send to the MSMQ is a message containing an object
of type Abstract Command.
The problem is that MSMQ needs the objects passed into the queue to be
serializable.
In order to maintain abstraction and reusability, I want to send the
AbsCommand in the message rather then a specific type of command. when
I try to do this, the MSMQ throws exceptions because of the
serialization of the abstract command contained in the message.


Is there anyway to get around this and to send an object to the msmq
which contains an interface or an abstract object?


Thanks,
Yoni Rabin


//In this illustration, I need to send the message object to the MSMQ
and later read it
// on the other side
[Serializable]
public class message
{
public abstract AbstractCommand;



}


[Serializable]
public abstract class AbstractCommand
{


}


[Serializable]
public class MyCommand:AbstractCommand
{
public string property;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top