tags:

views:

332

answers:

3

Hi there,

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?

+1  A: 

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.

Marc Gravell
A: 

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.

Shiraz Bhaiji
A: 

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.

serialhobbyist