开发者

SOAP generic type serialization

We have created a class to wrap the payload of web service response with common information as foll开发者_JS百科ows.

public class ItemResponse<T> : Response
    {
    /// <summary>
    /// constructor to set private properties Item and Status
    /// </summary>
    /// <param name="item"></param>
    /// <param name="status"></param>
    public ItemResponse(T item, ResponseStatusEnum status) : base(status)
    {
        _item = item;
    }

    public ItemResponse()
    {
    }


    public ItemResponse(ResponseStatusEnum status, System.Collections.Generic.List<ResponseError> errors) : base(status, errors)
    {
    }
    private T _item;

    public T Item
    {
        get
        {
            return _item;
        }
    }


}

The base class "Response" just holds error information and status of the the response. The wsdl clearly shows the definition of this response to be ItemResponseOfType[TypeName] but the item type information is missing from the definition.

We tried adding

[XmlInclude(typeof(TypeName))]

but to no avail. Any ideas what we can do to let the SOAP serializer know we want the "Item" type serializing?


The SOAP serializer doesn't support generic types. Microsoft recommends using WCF http://msdn.microsoft.com/en-us/library/ms172342(v=VS.100).aspx. Before WCF, I remember them recommending the XML serializer.


OK we have got to the bottom of this.

Its turns out that the XML Serializer can serialize generic types of a sort. As neontapir correctly points out Generic types are not fully supported by the XML Serializer but it can still serialise generic types as it creates types of ItemResponseOfType[TheType] as I mentioned in my question.

The XML Serializer just wont de serialize the type to a generic Type.

Our problem was simply that the property Item was read only and as a result SOAP missed the Item property out. We just needed to make the property publicy settable to fix as below

public class ItemResponse<T> : Response
{
/// <summary>
/// constructor to set private properties Item and Status
/// </summary>
/// <param name="item"></param>
/// <param name="status"></param>
public ItemResponse(T item, ResponseStatusEnum status) : base(status)
{
    Item = item;
}

public ItemResponse()
{
}


public ItemResponse(ResponseStatusEnum status, System.Collections.Generic.List<ResponseError> errors) : base(status, errors)
{
}

public T Item
{
     get; set;
}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜