Click here to Skip to main content
15,943,148 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Unable to cast object CPerson to ServiceReference.CPerson

CPerson is defined in its own assembly and is referenced throughout my application.

This is an extract form how my the contract on my Central Message Distribution Service is defined:

Name = "CMDS_QueuedService"

public interface ICMDS_QueuedService
    [OperationContract(IsOneWay = true)]
    void DistributeData(short arg_Installation_ID, CPerson arg_DataRow);

public class cCMDS_Contract : ICMDS_QueuedService
    [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
    public void DistributeData(short arg_Installation_ID, CPerson arg_DataRow)
    //Do Stuff

CPerson in the DistributeData arguments is the class defined in my standard assembly (Db_person.CPerson), but then I add a service reference to my Local Messasage Handler, it's type is now ServiceReference.CPerson and a cast will not work.

Considering this quote from Andrew Troelsen: "The ability to serialize objects is critical when you want to copy an object to a remote machine using various remoting technologies such as Windows Communication Foundation."

Are my options some form of copy constructor, am I looking at serialize / de serialize as above, or something else that I have not even considered?
Ger Hayden 11-Jan-13 5:13am    
I think I have it solved by serializing the class. I need to iron out a few things before adding a solution.

1 solution

I expected the answer to be in using SoapDataFormatter or DataContractSerializer to a memory stream would be the solution and deserialize on the other side.

But no. If the service wants to do anything with the stream, the type e.g. DB_Person.CPerson trips it up - and yes I remembered to reset the stream position.

The solution that I managed to implement involved reading the stream into a byte array, passing that over on the queue to the service, rebuilding the stream and deserializing it.

I will not post code unless asked because while it works, its not a solution I am particularly proud of.

(This post will close the 'question') and hopefully provide some guidance to anyone in a similar bind)

I am also going to do a test where the stream is passed in to the service and rather than deserializing it, the service relays it to other subscribed clients to see if they can act on it...
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900