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

查看:175
本文介绍了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.

下面是我的code的显示例子。

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,数据成员和序列化等的正确性有何评论?任何指针或明显的错误是什么?

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] 将工作一样好。之一的消息的合同的一个优点是,可以设置隐私针对成员,但在许多情况下,这是没有必要的。在这种情况下,我preFER守合同尽可能的简单,就像一个数据的合同。

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 ,它不是集合成员有制定者的要求 - 尽管你可能需要添加一个 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网具有数据合同和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天全站免登陆