WCF:MessageContract、DataContract ......困惑吗? [英] WCF: MessageContract, DataContract ... Confused?

查看:20
本文介绍了WCF:MessageContract、DataContract ......困惑吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写我的第一个 WCF 服务.我决定将服务作为一个 DLL 开始编写,然后再将 WCF 的东西放在上面,这就是我现在的位置.

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
}

任何人都可以评论使用 MessageContract、DataContract、DataMember 和 Serializable 等的正确性吗?有什么提示或明显的错误吗?

Can anyone comment on the correctness in the use of MessageContract, DataContract, DataMember and Serializable etc? Any pointers or glaring mistakes?

还有哪个序列化器最好用?确保我从中获得格式良好的 XML 以便其他客户可以轻松使用我的服务的最佳策略是什么?

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?

推荐答案

重新请求/响应 - [DataContract] 也能正常工作.消息合同的优点之一是您可以针对成员设置隐私,但在许多情况下,这不是必需的.在这种情况下,我更愿意让合同尽可能简单,就像数据合同一样.

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.

重新使用哪个序列化程序 - 这在很大程度上是配置的一个因素.例如,默认情况下通过 http,它将是 DataContractSerializer.

Re which serializer - that is largely a factor of the configuration. By default over http, for example, it will be DataContractSerializer.

但是,我不确定 IMyComplexType 的列表是否会很好地工作.您可以尝试,但通常它需要具体类型.请注意,对于基类,您可以使用 [KnownType] 来指定允许的子类型.

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.

请注意,与 XmlSerializer 不同,集合成员不需要具有 setter - 尽管如果您需要添加一个 OnDeserializing 回调方法来初始化列表这样做(WCF 不调用构造函数).

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).

另外:您还可以将 protobuf-net 与数据合同和 WCF 一起使用(只要他们有明确的订单);这比常规 xml 更密集.不过,它目前不支持消息契约.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆