dimanche 30 novembre 2014

Consume WCF Webservice with angular/javascript


I'm implementing a server with WCF and trying to reach its rest services using a client developed with Angular.


Here is the service :



public interface IConnexion

{

[OperationContract]
[WebGet(UriTemplate = "Connexion/{login}/{mdp}", ResponseFormat = WebMessageFormat.Json)]
Utilisateur Connexion(string login, string mdp);

[OperationContract]
[WebInvoke(UriTemplate = "Deconnexion/{id_user}", RequestFormat = WebMessageFormat.Json)]
void Deconnexion(string id_user);


}


And my function trying to reach the service :



app.service('serverConnexionServices', function(userService, serverConfigService) {

this.Connexion = function(login, pass)
{
var uri = this.getServerUri() + "Connexion/"+login+"/"+CryptoJS.MD5(pass);

$http.get(uri)
.success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
return data;
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
return "";
});
};
});


my form's controller:



app.directive('loginContent', function(userService, serverConnexionServices){
return{
restrict : 'E',
templateUrl : "includes/home/login-content.html",
controllerAs: "loginController",
controller: function($scope, userService, serverConnexionServices) {

var IsSubmitEnabled = true;
this.errors = "";

this.validateLogin = function(user) { // Calling the service
this.IsSubmitEnabled = false; // To avoid more than one click while checking on the server
var retour = serverConnexionServices.TryConnection(user);
alert('retour :'+retour);
if(retour.length > 0)
{ // There is an error so we show a message to the user
this.errors = retour;
}
this.IsSubmitEnabled = true;
user = {};
};

}
}
});


But when I try to open my client the alert box appears before the breakpoint is raised in Visual Studio (I put the breakpoint on the first line of my "Connexion" function. So it means the server has not answered yet to the request but the $http.get() has already sent its answer. So the result object doesn't contain what the server is sending. According to Angular's documentation, the response will be sent asynchronously. But How can I force my request to wait the answer ?


It's my form's controller which calls "Validate Login" which calls the service "Connexion". I'd like the service to answer back so that Validate Login can show "Error while log in" or "You're logged". Now, due to the async mode my Valide Login gets an "undefined" value. How can I manage to either keep async mode but have an answer in my controller or avoid the async mode ("which may not be the better choice ?)


What's the thing I am missing ?


EDIT: I can now reach my breakpoint in the implementation of Connexion in Visual Studio but still the answer from the server is not what is expected


EDIT2 : I've added my controller which needs the refresh.





Aucun commentaire:

Enregistrer un commentaire