Salve a tutti sono nuovo ed ho un problema che non riesco a risolvere.
Ho da poco tempo iniziato a studiare il C# e sto cercando di realizzare una piccola applicazione, ora questa applicazione dovrebbe scaricare dei dati da un database MySql on line. Ho quindi creato un Web Service, composto da i seguenti file :
WebService.asmx
Web.configcodice:<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
WebService.cs (nella cartella App_Code)codice:<?xml version="1.0"?> <!-- Per ulteriori informazioni su come configurare l'applicazione ASP.NET, visitare il sito Web all'indirizzo http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="false" targetFramework="4.0"> <assemblies> <add assembly="MySql.Data, Version=6.3.1.0, Culture=neutral, PublicKeyToken=13B67CE9E090FEFA"/></assemblies></compilation> </system.web> </configuration>
Fin qui tutto bene.codice:using System; using System.Collections.Generic; using System.Web; using System.Web.Services; //Per DB MySql.. using MySql.Data.MySqlClient; //Per ArrayList.. /// <summary> /// Descrizione di riepilogo per WebService /// </summary> [WebService(Namespace = "www.---.it")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // Per consentire la chiamata di questo servizio Web dallo script utilizzando ASP.NET AJAX, rimuovere il commento dalla riga seguente. // [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { public WebService () { //Rimuovere il commento dalla riga seguente se si utilizzano componenti progettati //InitializeComponent(); } [WebMethod(Description = "Scarica gli iscritti")] public List<string[]> Download() { List<string[]> datiAtleti = new List<string[]>(); string[] atleta = new string[9]; using (MySqlConnection connessioneDB = new MySqlConnection("server=;User Id=;password=;Persist Security Info=True;database=;Allow User Variables=True")) { try { MySqlCommand caricaIscritti = new MySqlCommand("SELECT * FROM partecipanti WHERE DownloadOK = 'false'", connessioneDB); MySqlDataReader dataReaderAtleta = caricaIscritti.ExecuteReader(); while (dataReaderAtleta.Read()) //finche ci sono righe leggi { atleta[0] = (string)dataReaderAtleta["Cognome"]; atleta[1] = (string)dataReaderAtleta["Nome"]; atleta[2] = (string)dataReaderAtleta["AnnoNascita"]; atleta[3] = (string)dataReaderAtleta["Grado"]; atleta[4] = (string)dataReaderAtleta["Palestra"]; atleta[5] = (string)dataReaderAtleta["Nazione"]; atleta[6] = (string)dataReaderAtleta["CatPesoIN"]; atleta[7] = (string)dataReaderAtleta["Genere"]; atleta[8] = (string)dataReaderAtleta["Email"]; atleta[9] = ((int)dataReaderAtleta["CodIscrizione"]).ToString(); datiAtleti.Add(atleta); } } catch (MySqlException ex) { } } return datiAtleti; } [WebMethod(Description = "Aggiorna l'iscritto come già scaricato")] public void Update(int CodIsc) { using (MySqlConnection connessioneDB = new MySqlConnection("server=;User Id=;password=;Persist Security Info=True;database=;Allow User Variables=True")) { try { connessioneDB.Open(); MySqlCommand aggiornaScaricato = new MySqlCommand("UPDATE partecipanti SET DownloadOK = 'true' WHERE CodIscrizione '" + CodIsc + "'", connessioneDB); aggiornaScaricato.ExecuteNonQuery(); } catch (MySqlException ex) { } } } }
Ora con VS 2010 ho proceduto nel seguente modo:
Aggiungi riferimento al servizio
Ho inserito il percorso web del file .asmx.
E ho impostato come spazio dei nomi “WebServiceSJC”
Ora nel mio progetto è comparsa una cartella Service References con all’interno il riferimento al servizio WebServiceSJC.
Ora nel client che dovrà consumare il web service ho inserito :
È fino a qui non ho nessun errore.codice:private void daSitoWebMenuMaster_Click(object sender, EventArgs e) { ArrayList codici = new ArrayList(); WebServiceSJC.WebServiceSoapClient scaricaIscritti = new WebServiceSJC.WebServiceSoapClient(); List<string[]> Atleti = new List<string[]>(scaricaIscritti.Download()); foreach (string[] atleta in Atleti) //finchè ci sono { using (MySqlConnection connessioneDB = new ..........
Ma se provo ad avviare il debug, mi trova degli errori nel file reference.cs
In particolare:
“Il nome di tipo ‘WebServiceSJC’ non esiste nel tipo ‘Easy.Easy’
Il file reference.cs:
Dove Sbaglio?codice://------------------------------------------------------------------------------ // <auto-generated> // Il codice è stato generato da uno strumento. // Versione runtime:4.0.30319.296 // // Le modifiche apportate a questo file possono provocare un comportamento non corretto e andranno perse se // il codice viene rigenerato. // </auto-generated> //------------------------------------------------------------------------------ namespace Easy.WebServiceSJC { using System.Runtime.Serialization; using System; [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")] [System.Runtime.Serialization.CollectionDataContractAttribute(Name="ArrayOfString", Namespace="www.scuolajudoceracchini.it", ItemName="string")] [System.SerializableAttribute()] public class ArrayOfString : System.Collections.Generic.List<string> { } [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] [System.ServiceModel.ServiceContractAttribute(Namespace="www.---.it", ConfigurationName="WebServiceSJC.WebServiceSoap")] public interface WebServiceSoap { // CODEGEN: Generazione di un contratto di messaggio perché il nome di elemento DownloadResult dallo spazio dei nomi www.---.it non è contrassegnato come nillable [System.ServiceModel.OperationContractAttribute(Action="www.---.it/Download", ReplyAction="*")] Easy.WebServiceSJC.DownloadResponse Download(Easy.WebServiceSJC.DownloadRequest request); [System.ServiceModel.OperationContractAttribute(Action="www.---.it/Update", ReplyAction="*")] void Update(int CodIsc); } [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] [System.ServiceModel.MessageContractAttribute(IsWrapped=false)] public partial class DownloadRequest { [System.ServiceModel.MessageBodyMemberAttribute(Name="Download", Namespace="www.---.it", Order=0)] public Easy.WebServiceSJC.DownloadRequestBody Body; public DownloadRequest() { } public DownloadRequest(Easy.WebServiceSJC.DownloadRequestBody Body) { this.Body = Body; } } [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] [System.Runtime.Serialization.DataContractAttribute()] public partial class DownloadRequestBody { public DownloadRequestBody() { } } [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] [System.ServiceModel.MessageContractAttribute(IsWrapped=false)] public partial class DownloadResponse { [System.ServiceModel.MessageBodyMemberAttribute(Name="DownloadResponse", Namespace="www.---.it", Order=0)] public Easy.WebServiceSJC.DownloadResponseBody Body; public DownloadResponse() { } public DownloadResponse(Easy.WebServiceSJC.DownloadResponseBody Body) { this.Body = Body; } } [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] [System.Runtime.Serialization.DataContractAttribute(Namespace="www.---.it")] public partial class DownloadResponseBody { [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=0)] public Easy.WebServiceSJC.ArrayOfString[] DownloadResult; public DownloadResponseBody() { } public DownloadResponseBody(Easy.WebServiceSJC.ArrayOfString[] DownloadResult) { this.DownloadResult = DownloadResult; } } [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] public interface WebServiceSoapChannel : Easy.WebServiceSJC.WebServiceSoap, System.ServiceModel.IClientChannel { } [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] public partial class WebServiceSoapClient : System.ServiceModel.ClientBase<Easy.WebServiceSJC.WebServiceSoap>, Easy.WebServiceSJC.WebServiceSoap { public WebServiceSoapClient() { } public WebServiceSoapClient(string endpointConfigurationName) : base(endpointConfigurationName) { } public WebServiceSoapClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public WebServiceSoapClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public WebServiceSoapClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress) { } [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] Easy.WebServiceSJC.DownloadResponse Easy.WebServiceSJC.WebServiceSoap.Download(Easy.WebServiceSJC.DownloadRequest request) { return base.Channel.Download(request); } public Easy.WebServiceSJC.ArrayOfString[] Download() { Easy.WebServiceSJC.DownloadRequest inValue = new Easy.WebServiceSJC.DownloadRequest(); inValue.Body = new Easy.WebServiceSJC.DownloadRequestBody(); Easy.WebServiceSJC.DownloadResponse retVal = ((Easy.WebServiceSJC.WebServiceSoap)(this)).Download(inValue); return retVal.Body.DownloadResult; } public void Update(int CodIsc) { base.Channel.Update(CodIsc); } } }

Rispondi quotando