Tom_123
Tom_123

Reputation:

WCF serialization error

I have method of a WCF service that accepts an interface as a parameter called IClient

[OperationContract]
void RegisterClientToCallBackTo(IClientCallBack client);

public interface IClient
{
    void SendMessage(string message);
}

I have a windows form that implements iclient and When I pass the form into the method I get the error:
Type 'FormMain' cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute. Consider marking the base type 'System.Windows.Forms.Form' with DataContractAttribute or SerializableAttribute, or removing them from the derived type.

i tried marking the form with [DataContractAttribute] and [SerializableAttribute] but i still get that same error

any ideas?

Upvotes: 2

Views: 1794

Answers (3)

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65391

Could you post the definition of SendMessage.

Also you need to make sure that you update the client reference, whenever you change the contract or service.

EDIT

As others have noted you must send a DTO you cannot send a form.

Upvotes: 0

serialhobbyist
serialhobbyist

Reputation: 4815

The message is telling you that you can't just mark the subclass (i.e. your Form) with [DataContractAttribute] and [SerializableAttribute], you have to mark all parent classes with [DataContractAttribute] and [SerializableAttribute] as well. You'll need to find another way.

It seems odd to be passing a form, anyway - it doesn't sound like good encapsulation to have a form managing contact with a service. Maybe it's time to look at a presentation pattern like MVC or MVP? Your callback would then be in a class under your control.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062780

You simply can't pass a form via WCF; WCF is intended to pass state, not implementation. IMO, you would be best served by creating a DTO to represent the data you want to pass, and pass that via WCF.

Upvotes: 2

Related Questions