【问题标题】:Javascript proxy classes for compound objects in WCFWCF 中复合对象的 Javascript 代理类
【发布时间】:2010-11-02 08:04:30
【问题描述】:

我创建了一个 WCF 服务并且正在运行。目前这是一个非常基础的服务,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;


[ServiceContract(Namespace = "TestServiceNameSpace")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyTestService
{

    [OperationContract]
    public MyNamespace.MyCompoundType ReturnMyCompoundType()
    {

        return new MyNamespace.MyCompoundType { DateVal = DateTime.Now, IntegerVal = 256, StringVal = "Pedro's test" };
    }

}

下面是 MyCompoundType 类

using System.Runtime.Serialization;
using System;
using System.ComponentModel;
namespace MyNamespace
{

    [DataContract]    
    public class MyCompoundType : IMyCompoundType
    {
        [DataMember]
        public int IntegerVal { get; set; }

        [DataMember]
        public string StringVal { get; set; }

        [DataMember]
        public DateTime DateVal { get; set; }
    }
}

现在,当我通过转到http://localhost/MyTestService.svc/jsdebug(然后我用它来进行 Ajax 调用)查看此服务的 JS 文件时,我注意到没有为 MyCompoundType 创建代理。所以,当我包含这个 JS 文件时,一切正常,我可以调用服务,但我不能声明 MyCompoundType 类型的 javascript 变量(即代理类型)。这甚至可能吗?这样做的一个主要部分是,我们将在 javascript 中使用智能感知功能,以避免像有人输入这样的错误:

var mycompundTypeReturn = returnValueFromWCFCall;
alert(mycompoundTypeReturn.StrVal); //this will give us an error because mycompoundTypeReturn.StrVal does not exist, only mycompoundTypeReturn.StringVal exists

是否可以使用 svcutil.exe 生成 JS 代理文件并指定更多详细信息?有什么我错过的属性吗?这甚至可能吗?这对使用 WCF 是否有意义?

任何帮助都将不胜感激,甚至“您在浪费时间,这是不可能的,您错过了 WCF 的重点”回复将不胜感激。

谢谢

【问题讨论】:

    标签: javascript ajax wcf proxy


    【解决方案1】:

    在搜索了很多年之后,我仍然没有发现任何关于为 Ajax 调用自动生成这些代理类的信息,所以最后我编写了一个小脚本生成应用程序,它查看特定的 .Net 库,然后为该库中的每个类创建一个该类的 Javascript 表示。因此,对于上面的对象,它会生成以下 javascript 类:

    var MyLibEntities = 新函数 () {

    this.MyLibBase = function () {
        var t = this;
    };
    this.MyLibBase.prototype.entityHashCode = null;
    this.MyLibBase.prototype.validate = function () {
        return ValidateObject(this);
    };
    this.MyLibBase.prototype.getEntityName = function () {
        if (this.__type != null) {
            //split the __type param with : to get the Entity Name - 1st element
            var ary = this.__type.split(":");
            if (ary.length > 0)
                return ary[0]
            else
                return this.__type;
        }
    }
    this.MyLibBase.prototype.clone = function (source) {
        if (source !== undefined && source !== null) {
            //if the source object is not of the same type we should not instantiate the object
            if (this.__type != source.__type) {
                var errormsg = "Object type '" + source.__type + "' does not match object type '" + this.__type + "'";
                if (typeof console != "undefined")
                { console.log(errormsg); }
                throw new Error(errormsg);
            }
            for (var i in source) {
                if (typeof this[i] != "undefined")
                { this[i] = source[i]; }
            }
        }
    };
    
    this.MyCompoundType = function (wo) {
        var t = this;
        t.__type = "MyCompoundType:#MyLib.Entities";
        //assign all the properties from the referencing object
        this.clone(wo);
    };
    
    this.MyCompoundType.prototype = new this.MylibBase();
    this.MyCompoundType.prototype.constructor = this.MyCompoundType;
    this.MyCompoundType.prototype.IntegerVal = null;
    this.MyCompoundType.prototype.StringVal = null;
    this.MyCompoundType.prototype.DateVal = null;
    

    }

    虽然这不是完全相同的类型,但它给了我智能感知,我可以将此对象传递给 MSAjax 方法。它的构造函数可以接收从 WCF 传回的 JSON 对象,然后在克隆函数中使用该对象来分配属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      相关资源
      最近更新 更多