Ciao a tutti,
da buon principiante sia di C# che di Web Service sto pesantemente sbattendo la testa contro queste 2 tecnologie.
Ho scopiazzato in giro per la rete un webservice (Service.asmx) così composto:
codice:
<%@ WebService Language="C#" class="WebService" %>
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
[WebService(Description = "Non serve a niente comunque!", Name = "Web Service a cacchio",
Namespace = "http://localhost/MyWebServices/")]
/// <summary>
/// Service1 contains demo methods!
/// </summary>
public class WebService : System.Web.Services.WebService
{
[WebMethod]
public String SayHello()
{
return "Hello World";
}
[WebMethod]
public int Add(int a, int b)
{
return a + b;
}
[WebMethod]
public string Subtract(int a, int b)
{
if (a >= b)
return Convert.ToString(a - b);
else
return "Ma chi ti ha insegnato la matematica?";
}
}
e, richiamandolo dal browser, funziona.
Ho letto che per poter dare la possibilità ad una pagina aspx di usare il WS devo utilizzare un paio di righe dal prompt dei comandi di Visual Studio, per cui ho dato
e
codice:
csc /t:library /out:bin\ProvaWebService.dll ProvaWebService/reference:System.dll,System.Data.dll,System.Web.dll,System.Web.Services.dll,System.XML.dll /optimize
Ho provato poi a creare una pagina .aspx (prova.aspx) e a importare il namespace "WService", che dovrebbe essere stato assegnato al Web Service dalla prima istruzione (giusto?)
Il codice di prova.aspx è
codice:
<%@ Page Language="C#" Explicit="true" Strict="true" Buffer="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="WService" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<script language="C#" runat="server">
protected void runSrvice_Click(object sender, EventArgs e)
{
{
WService mySvc = new WService();
Label1.Text = mySvc.SayHello();
Label2.Text = mySvc.Add(Int32.Parse(txtNum1.Text), Int32.Parse(txtNum2.Text)).ToString();
}
}
</script>
<head>
</head>
<body>
<form id="Form1" runat="server">
First Number to Add :
<asp:TextBox id="txtNum1" runat="server" Width="43px">4</asp:TextBox>
</p>
Second Number To Add :
<asp:TextBox id="txtNum2" runat="server" Width="44px">5</asp:TextBox>
</p>
<u>Web Service Result -</u>
</p>
Hello world Service :
<asp:Label id="Label1" runat="server" Font-Underline="True">Label</asp:Label>
</p>
Add Service :
<asp:Label id="Label2" runat="server" Font-Underline="True">Label</asp:Label>
</p>
<p align="left">
<asp:Button id="runSrvice" onclick="runSrvice_Click" runat="server" Text="Execute"></asp:Button>
</p>
</form>
</body>
</html>
ma quando vado a lanciare la pagina il compilatore mi restituisce
codice:
Errore 1 'WService' è 'spazio dei nomi' ma è utilizzato come 'tipo'. C:\Inetpub\wwwroot\SII2000\WebService\Prova.aspx 30 13 C:\...\WebService\
Errore 2 'WService' è 'spazio dei nomi' ma è utilizzato come 'tipo'. C:\Inetpub\wwwroot\SII2000\WebService\Prova.aspx 30 34 C:\...\WebService\
Cosa cacchio sbaglio? Come li richiamo quei maledetti metodi? Vi prego, datemi una dritta...