【问题标题】:How to generate all datacontract class如何生成所有数据合同类
【发布时间】:2012-01-07 16:17:42
【问题描述】:

我已经用 WCF 构建了聊天服务。我有标记为 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;
        }
    }
}

我的服务合同如下

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

当我尝试从 VisualStudio 2010 生成客户端代码时,没有生成类 Message。但是当我将服务合同上的方法“Whisper”中的参数“消息”类型更改为消息而不是字符串时,它会生成。

我将参数消息的类型改为“消息”而不是“字符串”:

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

我的回调类需要 Message 类才能正常工作。

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

问题是为什么标记为datacontract属性的类不包含在服务合同的方法参数或返回值中时不会生成。

【问题讨论】:

    标签: wcf datacontract servicecontract


    【解决方案1】:

    服务引用只生成使用服务所需的类。它确实生成标记为DataContract的每个类。

    但是当我将服务合同的方法“Whisper”中的参数“消息”类型更改为消息而不是字符串时,它会生成。

    这正是它应该如何工作的。如果服务需要该类,则将生成该类。如果它不需要该类,则不会生成它。

    【讨论】:

    • 我有回调类“IChatCallback”,需要类“Message”才能正常工作
    • 你还没有发布那个界面,所以我看不到里面有什么......!
    【解决方案2】:

    好的,我找到了解决方案。

    我忘记在我的回调类中添加 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 2015-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多