Search code examples
wcfdatacontractservicecontract

How to generate all datacontract class


I have build chat service with WCF. I have class that marked as datacontract attribute

[DataContract]
public class Message
{
    string   _sender;
    string   _content;
    DateTime _time;

    [DataMember(IsRequired=true)]
    public string Sender
    {
        get { return _sender;  }
        set { 
            _sender = value;
        }
    }

    [DataMember(IsRequired = true)]
    public string Content
    {
        get { return _content;  }
        set { 
            _content = value;
        }
    }

    [DataMember(IsRequired = true)]
    public DateTime Time
    {
        get { return _time;  }
        set { 
            _time = value;
        }
    }
}

And my service contract is like below

[ServiceContract(Namespace="", SessionMode = SessionMode.Required, CallbackContract = typeof(IChatCallback))]
public interface IChat
{
    [OperationContract]
    bool Connect(Client client);

    [OperationContract(IsOneWay=true, IsInitiating=false, IsTerminating=true)]
    void Disconnect();

    [OperationContract(IsOneWay = true, IsInitiating = false)]
    void Whisper(string target, string message);
}

When i try to generate client code from VisualStudio 2010, class Message is not generated. But it generated when i change the type of parameter "message" in method "Whisper" on my service contract to Message not string.

I change the type of parameter message to "Message" not "string":

[OperationContract(IsOneWay = true, IsInitiating = false)]
void Whisper(string target, Message message);

I've callback class that require Message class to work correctly.

public interface IChatCallback
{
    void RefreshClient(List<Client> clients);
    void ReceiveWhisper(Message message);
    void ReceiveNotifyClientConnect(Client joinedClient);
    void ReceiveNotifyClientDisconnect(Client leaver);
}

And the question is why class that marked as datacontract attribute is not generated when they are not included in service contract's method parameter or return value.


Solution

  • Okay i found the solution.

    I forgot to add operationcontract attribute in my callback class.

    public interface IChatCallback
    {
        [OperationContract(IsOneWay = true)]
        void RefreshClient(List<Client> clients);
    
        [OperationContract(IsOneWay = true)]
        void ReceiveWhisper(Message message);
    
        [OperationContract(IsOneWay = true)]
        void ReceiveNotifyClientConnect(Client joinedClient);
    
        [OperationContract(IsOneWay = true)]
        void ReceiveNotifyClientDisconnect(Client leaver);
    }