2011-11-28 4 views
1

WCF를 사용하여 채팅 서비스를 빌드했습니다. 나는 datacontract 속성모든 datacontract 클래스 생성 방법

[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; 
     } 
    } 
} 

로 표시된 클래스가 그리고 내가으로 VisualStudio 2010에서 클라이언트 코드를 생성 할 때

[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); 
} 

아래 수준의 메시지가 생성되지 않습니다처럼 내 서비스 계약이다. 그러나 그것은 "메시지"문자열에 대한 서비스 계약에서 "속삭임"메서드의 매개 변수 "메시지"유형을 변경할 때 생성됩니다.

나는 "메시지"없습니다 "문자열"을 매개 변수 메시지의 유형을 변경 :

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

나는 메시지 클래스를 필요로 콜백 클래스는 제대로 작동했습니다.

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

그리고 질문은 왜이 서비스 계약의 방법 매개 변수 또는 반환 값에 포함되지 않은 경우 datacontract 속성이 발생하지 않는 것으로 표시 클래스입니다.

답변

1

그래, 해결책을 찾았습니다.

콜백 클래스에 operationcontract 속성을 추가하는 것을 잊었습니다.

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); 
} 
1

서비스 참조는 서비스를 사용하는 데 필요한 클래스 만 생성합니다. 이 아닌DataContract으로 표시된 모든 단일 클래스를 생성합니다.

그러나 서비스 계약에서 메시지가 아닌 문자열에 대한 메서드 "속삭임"에서 "메시지"매개 변수의 유형을 변경할 때 생성됩니다.

정확히 작동해야합니다. 서비스가 해당 클래스를 필요로하면 서비스가 생성됩니다. 해당 클래스가 필요하지 않으면 생성되지 않습니다.

+0

아 내가 수동으로 – brian

+0

그것을 생성해야합니다 참조 나는 콜백 클래스는 클래스 "메시지"를 필요로 "IChatCallback는"일 당신은 인터페이스 내가 그 안에 무엇을 볼 수 있다는 게시하지 않은 제대로 – brian

+0

해야 ...! –