将派生类型发布到WCF服务

时间:2014-03-11 10:39:38

标签: jquery asp.net wcf c#-4.0

我正在尝试从WCF Web服务获取并发布派生类型。 所以,我有以下复合类型:

[KnownType(typeof(CompositeTypeOne))]
[DataContract]
public class CompositeType
{
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
        get { return boolValue; }
        set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
        get { return stringValue; }
        set { stringValue = value; }
    }
}

[DataContract]    
public class CompositeTypeOne : CompositeType
{
    string stringValue1 = "Helllo One";

    [DataMember]
    public string StringValue1
    {
        get { return stringValue1; }
        set { stringValue1 = value; }
    }

}

以下服务:

[ServiceContract]
[ServiceKnownType(typeof(CompositeTypeOne))]
public interface IService1
{
    [WebGet]
    [OperationContract]
    CompositeType GetDataInheretence(int inheretenceLevel);

    [WebInvoke(Method="POST")]
    [OperationContract]
    CompositeType PostDataInheretence(CompositeType postedType);

}

服务接口的实现是(请注意命名空间):

namespace WcfServiceSandBox
{
public class Service1 : IService1
{

    public CompositeType GetDataInheretence(int inheretenceLevel)
    {
        var compositeType = new CompositeType();
        if (inheretenceLevel > 0)
        {
            compositeType = new CompositeTypeOne();
        }

        return compositeType;
    }


    public CompositeType PostDataInheretence(CompositeType postedType)
    {
        return postedType;
    }
}
}

我正在jQuery中执行复合类型的get和post,如下所示:

function doGetPostCompositeType() {
var inheretenceLevelVal = $("#inheretenceLevel").val();
var getUrl = "http://localhost:65201/Service1.svc/JsonService/GetDataInheretence?inheretenceLevel=" + inheretenceLevelVal;
var compositeModel = null;

$.ajax({
    cache: false,
    type: "GET",
    async: false,
    url: getUrl,
    contentType: "application/json",
    dataType: "json",
    success: function (serverModel) {
        if (serverModel != null) {
            compositeModel = serverModel;
        }            
        alert(serverModel);
    }
});

if (compositeModel != null) {
    var postUrl = "http://localhost:65201/Service1.svc/JsonService/PostDataInheretence";
    compositeModel.__type = "CompositeTypeOne:#WcfServiceSandBox";
    var stringifiedCompositeLevel = JSON.stringify(compositeModel);

    $.ajax({
        cache: false,
        type: "POST",
        async: false,
        url: postUrl,
        data: stringifiedCompositeLevel,
        contentType: "application/json",
        dataType: "json",
        success: function (serverModel) {                
            alert(serverModel);
        }
    });
}

}

所以我首先得到一个类型CompositeType,如果inheretence level为0,CompositeTypeOne如果大于0.得到CompositeTypeOne类型,但比我发布它时序列化为CompositeType。

我找到了这个链接http://coab.wordpress.com/2010/03/01/serializing-and-deserializing-derived-types-or-interfaces-in-wcf/,但它似乎无效。

那么,如何告诉WCF从json请求序列化/反序列化哪种类型?

0 个答案:

没有答案