开发者

Javascript proxy classes for compound objects in WCF

开发者 https://www.devze.com 2023-01-22 20:25 出处:网络
I have a WCF service that I have created and is working. It is a very basic service for now and has the following code:

I have a WCF service that I have created and is working. It is a very basic service for now and has the following code:

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" };
    }

}

Below is the MyCompoundType class

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; }
    }
}

Now, when I look at the JS file for this service by going to http://localhost/MyTestService.svc/jsdebug (which I then use for making my Ajax calls) I noticed that there is no proxy created for the MyCompoundType. so, when I include this JS file, everything works fine and I can make a call to the service, but I cannot declare a javascript variable of type MyCompoundType (a proxy type that is). Is this even possible? A major part of this is so that we would use the intellisense functionality in the javascript so as to avoid bugs like somebody typing in:

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

Is it possible to generate the JS proxy file using svcutil.exe and specifying more detail? Are there any attributes I have missed? Is this even possible? Does this even make sense using WCF?

Any help will be greatly appreciated, or even a "You're wasting your time with this, it's impossible and you've missed the point of WCF" repl开发者_JS百科y will be appreciated.

Thanks


After searching for ages I still found nothing on auto generating these proxy classes for Ajax calls, so in the end I wrote a little script generating application that looks at a particular .Net library and then for each class in that library creates a Javascript representation of that class. So, for the object above, it generates the following javascript class:

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;

}

Whilst this is not exactly the same type, it gives me intellisense and I can pass this object to the MSAjax methods. It's constructor can receive an JSON object passed back from the WCF, which is then used in the clone function to assign the properties.

0

精彩评论

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