如何生成所有数据合约类
我用 WCF 构建了聊天服务。我有标记为数据契约属性的类
[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 生成客户端代码时,未生成消息类。但是当我将服务合同上的方法“Whisper”中的参数“消息”类型更改为消息而不是字符串时,它会生成。
我将参数消息的类型更改为“消息”而不是“字符串”:
[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);
}
问题是,为什么当标记为数据契约属性的类不包含在服务契约的方法参数或返回值中时,不会生成这些类。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
服务引用仅生成使用该服务所需的类。它不会生成标记为
DataContract
的每个类。这正是它应该如何工作的。如果服务需要该类,则会生成该类。如果不需要该类,则不会生成它。
The service reference only generates classes that are required to use the service. It does not generate every single class that is marked as
DataContract
.That's exactly how it should work. If the service requires that class, then it will be generated. If it doesn't require that class then it won't be generated.
好吧,我找到了解决方案。
我忘记在回调类中添加 operationcontract 属性。
Okay i found the solution.
I forgot to add operationcontract attribute in my callback class.