C# WCF:MessageContract、DataContract ......困惑吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/673638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:52:18  来源:igfitidea点击:

WCF: MessageContract, DataContract ... Confused?

c#wcfarchitecture

提问by Rob Stevenson-Leggett

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.

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

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?

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

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?

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

采纳答案by Marc Gravell

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.

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

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

Re which serializer - 这在很大程度上是配置的一个因素。例如,默认情况下通过 http,它将是DataContractSerializer.

I'm not sure, however, that the list of IMyComplexTypeis 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.

但是,我不确定该列表IMyComplexType是否会运行良好。您可以尝试,但通常它需要具体类型。请注意,您可以使用基类[KnownType]来指定允许的子类型。

Note that unlike XmlSerializer, it is not a requirement for collection members to have setters - although you might need to add an OnDeserializingcallback method to initialize the list if you do that (WCF doesn't call constructors).

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

Aside: you can also use protobuf-netwith 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.

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

回答by LCJ

Though not a direct answer to your question, it is worth to take a note of the following from MSDN -Using Message Contracts

虽然不是对您问题的直接回答,但值得注意的是MSDN -Using Message Contracts中的以下内容

Each individual message header and message body part is serialized (turned into XML) using the chosen serialization engine for the service contract where the message is used. The default serialization engine, the XmlFormatter, can handle any type that has a data contract, either explicitly(by having the System. Runtime. Serialization. DataContractAttribute) or implicitly(by being a primitive type, having the System.SerializableAttribute, and so on).

每个单独的消息头和消息正文部分都使用为使用消息的服务合同选择的序列化引擎进行序列化(转换为 XML)。默认的序列化引擎XmlFormatter可以处理任何具有数据协定的类型,无论是显式(通过具有System.Runtime.Serialization.DataContractAttribute)还是隐式(通过作为基本类型,具有 System.SerializableAttribute 等等) )。

enter image description here

在此处输入图片说明