Mark
Mark

Reputation: 2587

Using Asp.net mvc 2 and WCF - Passing generic object to the Service call?

[DataContract]
public class UserCertification
{
…
}

[DataContract]
public class UserPhone
{
…
}

[DataContract]
public class UserAddress
{
…
}

[DataContract]
public abstract class Request
{
    [DataMember]
    public int UserMakingRequest { get; set; }

    [DataMember]
    public Guid RequestId { get; set; }

    [DataMember]
    public Object RequestObjectDTO { get; set; }
}

var request = new Request
                    {
                        RequestId = new Guid(),
                        UserMakingRequest = loggedInUserId,
                        RequestObjectDTO = userCertification,
                    };

I have DataContracts: UserCertification, UserAddress and UserPhone

I also have a DataContact Request. This is what I would like to pass to each WCF service method.

So notice in the Request DataContract is DataMember called RequestObjectDTO. I made this of type object, hoping I would then be able to attach my other DataContracts to it.

This did not work - it throws the error "Cannot create an abstract class."

What type should it be of? Can I do this?

Upvotes: 1

Views: 614

Answers (2)

John Saunders
John Saunders

Reputation: 161773

Keep in mind that everything you send to a service must be serialized and deserialized, in most cases, as XML.

Exactly what XML would you have sent to the service, and what XML Schema would describe it? If you can't answer those questions, then neither can WCF.

Upvotes: 0

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

That is the point of abstract class - you can't create its instance. You must create instace of derived non abstract class but in such case you must mark your Request class with KnownTypeAttribute describing child classes which can be transported by WCF messages. Moreover WCF doesn't like object type as DataMember - it will not work because WCF must know what type should be deserialized on a client.

Upvotes: 2

Related Questions