2010-11-02 3 views
0

J'ai un service WCF que j'ai créé et qui fonctionne. Il est un service très basique pour l'instant et a le code suivant:Classes de proxy JavaScript pour les objets composés dans 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" }; 
    } 

} 

est inférieure à la classe 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; } 
    } 
} 

Maintenant, quand je regarde le fichier JS pour ce service en allant http://localhost/MyTestService.svc/jsdebug (que j'utilise ensuite pour effectuer mes appels Ajax) J'ai remarqué qu'il n'y a pas de proxy créé pour MyCompoundType. donc, quand j'inclus ce fichier JS, tout fonctionne bien et je peux faire un appel au service, mais je ne peux pas déclarer une variable javascript de type MyCompoundType (un type de proxy qui est). Est-ce seulement possible? Une grande partie de cela est que nous utiliserions la fonctionnalité IntelliSense dans le javascript afin d'éviter des bugs comme quelqu'un tapant:

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

Est-il possible de générer le fichier proxy JS en utilisant svcutil.exe et en spécifiant plus détail? Y a-t-il des attributs que j'ai manqués? Est-ce seulement possible? Est-ce que cela a même du sens en utilisant WCF?

Toute aide sera grandement appréciée, ou même un "Vous perdez votre temps avec ceci, c'est impossible et vous avez manqué le point de WCF" réponse sera appréciée.

Merci

Répondre

0

Après avoir cherché pendant des siècles, je encore rien trouvé sur l'auto générer ces classes proxy pour appels Ajax, donc à la fin je l'ai écrit une petite application générant script qui ressemble à une bibliothèque .Net particulier et pour chaque class dans cette bibliothèque crée une représentation Javascript de cette classe. Ainsi, pour l'objet ci-dessus, il génère la classe javascript suivante:

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; 

}

Bien que ce n'est pas exactement le même type, il me donne IntelliSense et je peux passer cet objet aux méthodes MSAjax. Son constructeur peut recevoir un objet JSON retransmis depuis le WCF, qui est ensuite utilisé dans la fonction clone pour assigner les propriétés.