IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 .NET Discussion :

Exécuter un web service


Sujet :

.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Inscrit en
    Août 2008
    Messages
    1 596
    Détails du profil
    Informations forums :
    Inscription : Août 2008
    Messages : 1 596
    Par défaut Exécuter un web service
    Bonjour,

    Dans mon web service, j'ai une méthode GetEtudiantsByClasse, je dois donner comme paramètre l'id de la classe, et la méthode doit retourner une liste d'étudiants. pour cela, j'ai implémenter ma base de donnée sous sql server (deux tables étudiants et classes), j'ai mis une une procédure stockée au niveau sgbd que j'ai appelée :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    SELECTEtudiantsByClasse
    ma classe entité c'est :

    Classe :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace ClSCOLARITE.Entity
    {
        public class Classe
        {
            #region Fields
            private string m_libelle;
            private string m_niveau;
            #endregion
     
            #region Constructs
            public Classe()
            {
     
            }
     
            #endregion
     
            #region Properties
     
            [System.ComponentModel.Browsable(true)]
            public string Libelle
            {
              get { return m_libelle; }
              set { m_libelle = value; }
            }
     
            [System.ComponentModel.Browsable(true)]
            public string Niveau
            {
                get { return m_niveau; }
                set { m_niveau = value; }
            }
            #endregion
     
        }
    }
    Etudiant :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace ClSCOLARITE.Table
    {
        class Etudiant : System.Collections.Generic.List<Entity.Etudiant>
        {
            #region Fields
     
            private Data.Etudiant oBData;
     
            #endregion
     
            #region Constructors
     
            public Etudiant()
            {
                oBData = new Data.Etudiant();
            }
     
            #endregion
     
            #region Methods
     
            public void LoadEtudiantsbyClasse(string classe)
            {
                System.Data.SqlClient.SqlDataReader oDBDataReader;
     
                try
                {
                    oDBDataReader = oBData.LoadEtudiantsbyClasse(classe);
                    while (oDBDataReader.Read() == true)
                    {
                        Entity.Etudiant oEtudiant;
                        oEtudiant = new Entity.Etudiant();
     
                        oEtudiant.ClEtudiant = Convert.ToString(oDBDataReader["ClEtudiant"]);
                        oEtudiant.Nom = Convert.ToString(oDBDataReader["Nom"]);
                        oEtudiant.Prenom = Convert.ToString(oDBDataReader["Prenom"]);
                        oEtudiant.Age = Convert.ToInt32(oDBDataReader["Age"]);
     
     
                        this.Add(oEtudiant);
                    }
                    oBData.CloseDataReader();
                    oBData.CloseConnection();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
     
            #endregion
     
     
        }
    }
    Au niveau Data :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace ClSCOLARITE.Data
    {
        public class Classe
        {
            #region Fields
     
            Data.SqlClient m_SqlClient;
     
            #endregion
     
            #region Constructors
     
            public Classe()
                {
                m_SqlClient = new SqlClient();
                }
            #endregion
     
            #region Methodes
     
     
            public System.Data.SqlClient.SqlDataReader LoadClasses()
            {
                System.Data.SqlClient.SqlDataReader oOleDbDataReader;
     
                try
                {
                    m_SqlClient.CreateCommand(System.Data.CommandType.StoredProcedure, "SelectClasses");
                    oOleDbDataReader = m_SqlClient.LoadList();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return oOleDbDataReader;
            }
     
            public void CloseDataReader()
            {
                try
                {
                    m_SqlClient.CloseDataReader();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
     
            public void CloseConnection()
            {
                try
                {
                    m_SqlClient.CloseConnection();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            #endregion
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace ClSCOLARITE.Data
    {
        public class Etudiant
        {
            #region Fields
     
            Data.SqlClient m_SqlClient;
     
            #endregion
     
            #region Constructors
     
            public Etudiant()
                {
                m_SqlClient = new SqlClient();
                }
            #endregion
     
            #region Methodes
     
            public System.Data.SqlClient.SqlDataReader LoadEtudiantsbyClasse(string classe)
            {
                System.Data.SqlClient.SqlDataReader oOleDbDataReader;
     
                try
                {
                    m_SqlClient.CreateCommand(System.Data.CommandType.StoredProcedure, "SELECTEtudiantsByClasse");
                    m_SqlClient.AddCommandParameter("@ClasseLibelle", System.Data.SqlDbType.VarChar, classe);
                    oOleDbDataReader = m_SqlClient.LoadList();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return oOleDbDataReader;
            }
     
            public void CloseDataReader()
            {
                try
                {
                    m_SqlClient.CloseDataReader();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            public void CloseConnection()
            {
                try
                {
                    m_SqlClient.CloseConnection();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            #endregion
        }
    }
    Au niveau table :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace ClSCOLARITE.Table
    {
        class Classe : System.Collections.Generic.List<Entity.Classe>
        {
            #region Fields
     
            private Data.Classe oBData;
     
            #endregion
     
            #region Constructors
     
            public Classe()
            {
                oBData = new Data.Classe();
            }
     
            #endregion
     
            #region Methods
     
            #endregion
     
            public void LoadClasses()
            {
                System.Data.SqlClient.SqlDataReader oDBDataReader;
     
                try
                {
                    oDBDataReader = oBData.LoadClasses();
                    while (oDBDataReader.Read() == true)
                    {
                        Entity.Classe oClasse;
                        oClasse = new Entity.Classe();
                        oClasse.Libelle = Convert.ToString(oDBDataReader["Libelle"]);
     
                        oClasse.Niveau= Convert.ToString(oDBDataReader["Niveau"]);
     
                        this.Add(oClasse);
                    }
                    oBData.CloseDataReader();
                    oBData.CloseConnection();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace ClSCOLARITE.Table
    {
        class Etudiant : System.Collections.Generic.List<Entity.Etudiant>
        {
            #region Fields
     
            private Data.Etudiant oBData;
     
            #endregion
     
            #region Constructors
     
            public Etudiant()
            {
                oBData = new Data.Etudiant();
            }
     
            #endregion
     
            #region Methods
     
            public void LoadEtudiantsbyClasse(string classe)
            {
                System.Data.SqlClient.SqlDataReader oDBDataReader;
     
                try
                {
                    oDBDataReader = oBData.LoadEtudiantsbyClasse(classe);
                    while (oDBDataReader.Read() == true)
                    {
                        Entity.Etudiant oEtudiant;
                        oEtudiant = new Entity.Etudiant();
     
                        oEtudiant.ClEtudiant = Convert.ToString(oDBDataReader["ClEtudiant"]);
                        oEtudiant.Nom = Convert.ToString(oDBDataReader["Nom"]);
                        oEtudiant.Prenom = Convert.ToString(oDBDataReader["Prenom"]);
                        oEtudiant.Age = Convert.ToInt32(oDBDataReader["Age"]);
     
     
                        this.Add(oEtudiant);
                    }
                    oBData.CloseDataReader();
                    oBData.CloseConnection();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
     
            #endregion
     
     
        }
    }
    et au niveau de mon web service :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {
        private ClSCOLARITE.Data.Etudiant oBData;
    
        public Service()
        {
    
            //Supprimez les marques de commentaire dans la ligne suivante si vous utilisez des composants conçus 
            //InitializeComponent(); 
        }
    
        [WebMethod]
        public string HelloWord()
        {
            return "hello Maria";
        }
    
        [WebMethod]
        public void LoadEtudiantsbyClasse(string classe)
        {
            System.Data.SqlClient.SqlDataReader oDBDataReader;
    
            try
            {
                oDBDataReader = oBData.LoadEtudiantsbyClasse(classe);
                while (oDBDataReader.Read() == true)
                {
                    ClSCOLARITE.Entity.Etudiant oEtudiant;
                    oEtudiant = new ClSCOLARITE.Entity.Etudiant();
    
                    oEtudiant.ClEtudiant = Convert.ToString(oDBDataReader["ClEtudiant"]);
                    oEtudiant.Nom = Convert.ToString(oDBDataReader["Nom"]);
                    oEtudiant.Prenom = Convert.ToString(oDBDataReader["Prenom"]);
                    oEtudiant.Age = Convert.ToInt32(oDBDataReader["Age"]);
    
    
                    this.Add(oEtudiant);
                }
                oBData.CloseDataReader();
                oBData.CloseConnection();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
    }
    Je sais pas si je suis sur la bonne voie ou non ? mais j'ai une erreur que j'ai pas pu résoudre au niveau de mon web service :

    il me dit que :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Erreur	1	'Service' ne contient pas de définition pour 'Add'	C:\Inetpub\wwwroot\WsSCOLARITE\App_Code\Service.cs	44	22	http://localhost/WsSCOLARITE/
    ?

    vous avez une idée ?

  2. #2
    Membre extrêmement actif
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 273
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 273
    Par défaut
    Ben oui, la réponse est dans le message ;

    this référence l'instance courante aka "Service" dans ton exemple.

    il faudrait donc :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     
    using System;
    using System.Collections;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
     
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {
        private ClSCOLARITE.Data.Etudiant oBData;
     
        public Service()
        {
     
            //Supprimez les marques de commentaire dans la ligne suivante si vous utilisez des composants conçus 
            //InitializeComponent(); 
        }
     
        [WebMethod]
        public string HelloWord()
        {
            return "hello Maria";
        }
     
        [WebMethod]
        public IList LoadEtudiantsbyClasse(string classe)
        {
            System.Data.SqlClient.SqlDataReader oDBDataReader;
            IList _etudiants=new ArrayList();
     
            try
            {
                oDBDataReader = oBData.LoadEtudiantsbyClasse(classe);
                while (oDBDataReader.Read() == true)
                {
                    ClSCOLARITE.Entity.Etudiant oEtudiant;
                    oEtudiant = new ClSCOLARITE.Entity.Etudiant();
     
                    oEtudiant.ClEtudiant = Convert.ToString(oDBDataReader["ClEtudiant"]);
                    oEtudiant.Nom = Convert.ToString(oDBDataReader["Nom"]);
                    oEtudiant.Prenom = Convert.ToString(oDBDataReader["Prenom"]);
                    oEtudiant.Age = Convert.ToInt32(oDBDataReader["Age"]);
                   _etudiants.Add(oEtudiant);
     
     
                }
                oBData.CloseDataReader();
                oBData.CloseConnection();
                return _etudiants;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
     
    }

    Reste néanmoins que :

    Tu ne devrais pas faire des tranformations et des accès en base directement depuis le service
    Ton service est de type void, ca risque d'être compliqué pour récupérer en sortie...
    Tes classes ne sont pas [Serializable], ce qui en WSE peut bloquer.

Discussions similaires

  1. Réponses: 0
    Dernier message: 30/03/2010, 10h37
  2. Web Service embarqué dans un exécutable
    Par adaneels dans le forum Services Web
    Réponses: 2
    Dernier message: 26/02/2009, 18h30
  3. Web service qui lance un exécutable
    Par maya dans le forum Services Web
    Réponses: 4
    Dernier message: 26/10/2008, 14h19
  4. [Web service] Exécuter un service .NET
    Par davels dans le forum Web & réseau
    Réponses: 3
    Dernier message: 11/07/2008, 23h26
  5. [Système] Problème d'exécution d'un Web Service
    Par kheiro dans le forum Langage
    Réponses: 2
    Dernier message: 11/05/2006, 15h26

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo