Appel asynchrone d'une fonction d'un service web en .net 2.0

froggyinfo Messages postés 16 Date d'inscription vendredi 12 décembre 2003 Statut Membre Dernière intervention 31 juillet 2006 - 27 juil. 2006 à 16:29
froggyinfo Messages postés 16 Date d'inscription vendredi 12 décembre 2003 Statut Membre Dernière intervention 31 juillet 2006 - 27 juil. 2006 à 17:30
Comment faire pour appeler une fonction d'un service web dans une applicaitn console.

Impossible de trouver d'exemple.

Greg

5 réponses

sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
27 juil. 2006 à 16:36
comme dans une application Windows Forms...

tu ajoutes une référence web à ton projet... et tu instancies ton WebService.

Sébastien FERRAND (
blog)
Consultant Indépendant
[Microsoft MVP Visual C#]
0
froggyinfo Messages postés 16 Date d'inscription vendredi 12 décembre 2003 Statut Membre Dernière intervention 31 juillet 2006
27 juil. 2006 à 16:41
Est ce que tu pourrais me donner un exemple je ne vois pas comment ca peut marcher

Merci
0
sebmafate Messages postés 4936 Date d'inscription lundi 17 février 2003 Statut Membre Dernière intervention 14 février 2014 37
27 juil. 2006 à 16:51
Imaging un WebService Toto... avec une WebMethod... Foo.

Tu ajoutes la Référence Web à ton projet...
ensuite dans ton code :

Toto toto = new Toto();
toto.Foo();

Sébastien FERRAND (
blog)
Consultant Indépendant
[Microsoft MVP Visual C#]
0
froggyinfo Messages postés 16 Date d'inscription vendredi 12 décembre 2003 Statut Membre Dernière intervention 31 juillet 2006
27 juil. 2006 à 17:29
Non en fait c'est pas ca le problème,

En fait dans le forum j'ai déja posé une question sur des threads qui se bloquaient. Je croyais que c'était un problème au niveau du codage des threads. En fait non le problème vient du service web.

En fait je fais dans ces thread des appels à la meme fonction du web service. Mais celui ci ne traite pas en parallèle mes appels.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Reflection;
using System.ComponentModel;




namespace RecupEnvoiAlerteVoyage
{
class Program
{
private static Mutex mut = new Mutex();
private static int nbThread = 5;
private volatile bool terminer = false;
private const string sqlConnexionStr = "chaine de connexion";
protected SqlDataReader cible;
private int i = 0;
public int threadFini = 0;
protected DateTime debut = DateTime.Now;
[MTAThread]
static void Main()
{
Program p = new Program();
p.lanceThread();
Console.ReadLine();
}

public void lanceThread()
{

/* Le programme principale lance le nombre de thread de départ
* attention ces thread en lanceront 4 eux meme
*/
SqlCommand recupCible = new SqlCommand();
SqlConnection conn = new SqlConnection(sqlConnexionStr);
recupCible.Connection = conn;
conn.Open();

try
{
recupCible.CommandType = System.Data.CommandType.StoredProcedure;
recupCible.CommandText = "recupCible";
recupCible.CommandTimeout = 3600;
cible = recupCible.ExecuteReader();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
// conn.Close();
}
Thread[] th = new Thread[nbThread];
for (int i = 0; i < nbThread; i++)
{
th[i] = new Thread(new ThreadStart(RecupEnvoiMail));
th[i].Name = i.ToString();
th[i].Start();
}
}


public void RecupEnvoiMail()
{
VarFoncMail v = new VarFoncMail();
bool read = false;
Thread t = new Thread();
while (!terminer)
{
lock (this)
{
try
{
if (!terminer)
{
if (!cible.Read())
{
terminer = true;
}
else
{
i++;
Console.WriteLine(i.ToString());
v = new VarFoncMail(System.Convert.ToInt32(cible["idalerte"]));
read = true;
t = new Thread(new ThreadStart(v.recupVol));
t.IsBackground = false;
t.Name = "thread" + Thread.CurrentThread.Name;
t.Start();
}

}
}
catch (Exception e)
{
Console.WriteLine(e.Message + "\r\n" + e.StackTrace);
terminer = true;
}
}
if (read)
{
try
{
try
{
t.Join();
//tache
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("fin thread" + Thread.CurrentThread.Name);
}
catch (Exception)
{
Console.WriteLine("erreur");
}

}
}
threadFini++;
if (threadFini == nbThread)
{
Console.WriteLine(DateTime.Now - debut);
}
}
}

class VarFoncMail
{
//info pour alerte
public int idalerte;
public MonService.UnObjet unObjet;

public VarFoncMail()
{
}

public VarFoncMail(int pidalerte)
{
this.idalerte = pidalerte;
}


public void recupVol()
{
Console.WriteLine("Debut " + Thread.CurrentThread.Name + "vol");
try
{

DateTime debut = DateTime.Now;
MonService.Service service = new MonService.Service();
service.Timeout = 600000;
object[] ofr = service.getObjet(this.idalerte);
Console.Write(Thread.CurrentThread.Name + " temp exec : " + (DateTime.Now - debut));
if (ofr.Length > 0)
{
unObjet = (ServiceWebXFR.offreVol)ofr[0];
}
}
catch (Exception e)
{
unVol = null;
Console.WriteLine("Vol : " + e.Message);
}
Console.WriteLine("--->Fin " + Thread.CurrentThread.Name + " Vol");
}
}
}

En fait au moment ou j'appel service.getObjet(this.idalerte)

C'est le service web qui ne traite pas les requêtes en parallèle.

Il effectue chaque fonction en série.
Quand il a donnée une réponse il exécute service.getObjet(this.idalerte) du thread suivant et ainsi de suite.

La fonction service.getObjet(this.idalerte) récupère le code source html d'une page web.

Voila pour toutes les explications.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
froggyinfo Messages postés 16 Date d'inscription vendredi 12 décembre 2003 Statut Membre Dernière intervention 31 juillet 2006
27 juil. 2006 à 17:30
En fait mon problème, vient du service web qui ne me permet pas de faire plusieur requete en meme temps. Enfin c'est ce dont j'ai l'impression.

Greg
0
Rejoignez-nous