我有一个我创建并正在运行的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中使用intellisense功能,以避免像某人输入的错误:
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的观点”将不胜感激。
由于
答案 0 :(得分:0)
搜索了几年之后,我仍然没有发现自动生成这些Ajax调用的代理类,所以最后我写了一个小脚本生成应用程序,查看特定的.Net库,然后为该库中的每个类创建一个该类的Javascript表示。因此,对于上面的对象,它会生成以下javascript类:
var MyLibEntities = new function(){
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;
}
虽然这不是完全相同的类型,但它给了我intellisense,我可以将此对象传递给MSAjax方法。它的构造函数可以接收从WCF传回的JSON对象,然后在克隆函数中使用它来分配属性。