WCF:MessageContract,DataContract ...困惑? [英] WCF: MessageContract, DataContract ... Confused?
问题描述
建筑师告诉我我应该坚持使用我已经完成的消息对象的特定格式。但是我已经在我的消息对象中使用了接口,复杂类型和列表。我来添加属性,我有点困惑。
以下是我的代码示例。
[ServiceContract]
public interface MyServiceContract
{
[OperationContract]
MyMethodResponseMessage MyMethod(MyMethodRequestMessage request);
}
public class MyService:MyServiceContract
{
public MyMethodResponseMessage MyMethod(MyMethodRequestMessage request)
{
//做事情
}
}
//消息
[MessageContract]
public class MyMethodResponseMessage
{
[MessageBodyMember]
public MyMethodResponse身体{get;组;
[DataContract]
public class MyMethodResponse
{
[DataMember]
public IMyComplexTypeItem {get;组; }
[DataMember]
public List< IMyComplexType>物品{get;组; }
[DataMember]
public bool Success {get;组; }
}
// DTO
public interface IMyComplexType
{
[DataMember]
string Identity {get;组;
[DataContract]
public class MyComplexType1:IMyComplexType
{
[DataMember]
public virtual string Identity
任何人都可以评论使用MessageContract,DataContract,DataMember和Serializable等方面的正确性吗?
任何指针或明显的错误?
还有哪个serializer是最好的使用?那么最好的策略是确保我从中获得良好的XML格式,以便其他客户端可以轻松地使用我的服务。
请求/响应 - 一个 [DataContract]
也可以正常工作。消息合约的优点之一是您可以对成员设置隐私,但在许多情况下,这不是必需的。在这种情况下,我希望保持合同尽可能的简单,就像数据合同一样。
哪个序列化程序,主要是配置的一个因素。默认情况下,http,例如,它将是 DataContractSerializer
。
然而,我不知道 IMyComplexType
的列表将工作得很好。你可以尝试,但一般来说它需要具体的类型。请注意,使用基类,您可以使用 [KnownType]
指定允许的子类型。
请注意与 XmlSerializer
不同,收集成员不需要设置器 - 尽管您可能需要添加一个 OnDeserializing
回调如果你这样做(WCF不调用构造函数)初始化列表的方法。
Aside:你也可以使用 protobuf-net 与数据合同和WCF(只要他们有明确的订单);这比常规xml更密集。尽管如此,它不支持消息合约。
I'm writing my first WCF service. I decided to write the service just as a DLL to begin with and then aspect the WCF stuff on afterwards which is where I am now.
I was advised by the architect that I should stick to a specific format for message objects which I have done. However I've used Interfaces, complex types and lists thereof in my message objects. I'm coming to adding the attributes on and I'm getting a bit confused.
Here's a show example of my code.
[ServiceContract]
public interface MyServiceContract
{
[OperationContract]
MyMethodResponseMessage MyMethod(MyMethodRequestMessage request);
}
public class MyService : MyServiceContract
{
public MyMethodResponseMessage MyMethod(MyMethodRequestMessage request)
{
//Do things
}
}
//Messages
[MessageContract]
public class MyMethodResponseMessage
{
[MessageBodyMember]
public MyMethodResponse Body { get; set; }
}
[DataContract]
public class MyMethodResponse
{
[DataMember]
public IMyComplexTypeItem { get; set; }
[DataMember]
public List<IMyComplexType> Items { get; set; }
[DataMember]
public bool Success { get; set; }
}
//DTO
public interface IMyComplexType
{
[DataMember]
string Identity { get; set; }
}
[DataContract]
public class MyComplexType1 : IMyComplexType
{
[DataMember]
public virtual string Identity
}
Can anyone comment on the correctness in the use of MessageContract, DataContract, DataMember and Serializable etc? Any pointers or glaring mistakes?
Also which serializer is the best one to use? and what is the best strategy to ensure I get well formed XML from this so that other clients can consume my service easily?
Re the request/response - a [DataContract]
would work just as well. One of the advantages of message-contracts is that you can set privacy against members, but in many cases this isn't necessary. In such cases, I prefer to keep the contract as simple as possible, just as a data-contract.
Re which serializer - that is largely a factor of the configuration. By default over http, for example, it will be DataContractSerializer
.
I'm not sure, however, that the list of IMyComplexType
is going to work very well. You could try, but generally it wants concrete types. Note that with base-classes you can use [KnownType]
to specify the allowed sub-types.
Note that unlike XmlSerializer
, it is not a requirement for collection members to have setters - although you might need to add an OnDeserializing
callback method to initialize the list if you do that (WCF doesn't call constructors).
Aside: you can also use protobuf-net with data-contracts and WCF (as long as they have an explicit Order); this is more densely packed than the regular xml. It has no support for message-contracts at the moment, though.
这篇关于WCF:MessageContract,DataContract ...困惑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!