Professional Documents
Culture Documents
Protocolo SOAP1
Protocolo SOAP1
ASP.NET Web Services soporta tanto SOAP 1.1 como SOAP 1.2. Por defecto, cada protocolo tendrá un vínculo en el wsdl
producido para el servicio. Para funcionar, o dejar de hacerlo, en SOAP 1.1 o SOAP 1.2, tendremos que modificar las
opciones de configuración tanto en el machine.config como en el web.config. Descomentando las líneas del siguiente
ejemplo deshabilitaremos el soporte para SOAP 1.1 o SOAP 1.2 respectivamente.
<system.web>
<webServices>
<protocols>
<!-- <remove name="HttpSoap"/> -->
<!-- <remove name="HttpSoap1.2"/> -->
</protocols>
</webServices>
</system.web>
C# Sample Caption.
View Source
This file is part of the Microsoft .NET Framework SDK Code Samples.
<script runat="server">
</script>
<body>
<form id="form1" runat="server">
<p><font face="Verdana" size="-1">Use SOAP 1.1 or SOAP 1.2 to
make your Web service call. </p>
<p> Modify web.config (in the 'server' subdirectory) to turn on or
off Soap 1.1 and Soap 1.2.</font></p>
<p><input id="SOAP11" type="submit" value="SOAP 1.1"
onserverClick="SOAP11_Click" runat="server" /></p>
<p><input id="SOAP12" type="submit" value="SOAP 1.2"
onserverClick="SOAP12_Click" runat="server" /></p>
<p><b><font face="Verdana" size="-1">Output:</font></b>
<font face="Verdana" size="-1" color='red'><asp:Label id="output"
text="" runat="server"/></font></p>
</form>
</body>
</html>
Devolver Varios Tipos de Datos
ASP.NET Web Services nos da una gran flexibilidad para estructurar los mensajes que se pasan hacía y desde un Servicio
Web. Este ejemplo muestra ejemplos de WebMethods que devuelven los siguientes tipos de datos:
• Primitive (String)
• Array of Primitives (Integers)
• Enumeration (Enum)
• Class/Struct
• Array of Classes/Structs
This file is part of the Microsoft .NET Framework SDK Code Samples.
<html>
<style>
div
{
font: 8pt verdana;
background-color:cccccc;
border-color:black;
border-width:1;
border-style:solid;
padding:10,10,10,10;
}
</style>
</script>
</body>
</html>
Permitir la DesCompresión de Servicios
Web
Cuando la propiedad EnableDecompression de una instancia de un cliente de una clase proxy se establece a verdadero, el
cliente soportará la descompresión.
Para deshabilitar la descompresión, tendremos que establecer EnableDecompression a falso. Un probable escenario dónde
pasa esto es cuando queremos ver las trazas SOAP de las respuestas enviadas desde el servidor, en texto plano.
Estableciendo la propiedad a falso, permitimos que el servidor sepa que la descompresión no se soporta.
View Source
This file is part of the Microsoft .NET Framework SDK Code Samples.
<script runat="server">
</script>
<body>
<form id="form1" runat="server">
<p><font face="Verdana" size="-1">When the EnableDecompression
property
on an instance of a client proxy class is set to true the client will
support decompression.</p>
<p><input id="EnableDecompression" type="submit" value="Query
Service" onserverClick="EnableDecompression_Click" runat="server" /></p>
<p><b><font face="Verdana" size="-1">Output:</font></b>
<font face="Verdana" size="-1" color='red'><asp:Label id="output"
text="" runat="server"/></font></p>
<p><input id="Reset" type="submit" value="Reset"
onserverClick="Reset_Click" runat="server" /></p>
</form>
</body>
</html>
Utilizando Sesiones ASP.NET
Los Servicios Web de ASP.NET nos permiten acceder a la administración de estado subyacente que proporciona ASP.NET.
Para permitir el estado de sesión de ASP.NET tenemos que establecer set WebMethod.EnableSession = True. En el cliente
tendremos que fijar serviceName.CookieContainer a una nueva instancia de System.Net.CookieContainer. Esto nos
permite mantener los datos en la misma sesión. Observar que para mantener los datos para toda la aplicación (para todas
las peticiones entrantes al servicio), no se requiere ningún paso.
//On the client you must add the following code (for session state only):
serviceName.CookieContainer = new System.Net.CookieContainer();
//Note that state can be persisted for the entire Application even if
EnableSession is false
[WebMethod(EnableSession=false)]
public String UpdateApplicationHitCounter() {
//update the application "HitCounter" here
return Application["HitCounter"].ToString();
}
This file is part of the Microsoft .NET Framework SDK Code Samples.
<html>
<style>
div
{
font: 8pt verdana;
background-color:cccccc;
border-color:black;
border-width:1;
border-style:solid;
padding:10,10,10,10;
}
</style>
</body>
</html>
Validación de Mensajes
Para validar los mensajes en el servicio usamos XmlValidatingReader. El mejor lugar para hacerlo es en una extensión
SOAP. Ésto nos da un control completo sobre los contenidos del mensaje SOAP y nos permite descartar mensajes que no
cumplen con el esquema antes del paso de deserialización, relativamente costoso.
Observar que este ejemplo valida los mensajes en el cuerpo del WebMethod. Esto se ha hecho por simplicidad. Los pasos
para validar un mensaje son los mismos independientemente del lugar. Para más información sobre la validación en
extensiones SOAP, acudid a MSDN article.
//add our event callback function to the event handler in case there is a
validation error
vr.ValidationEventHandler += new ValidationEventHandler
(ValidationHandler);
try
{
//if there is an error we will fall out of this while loop and our
callback function will be called
while (vr.Read())
{
//do nothing
}
}
catch (Exception exception)
{
//an exception will be thrown if there are non-validation errors,
e.g. in the case of malformed XML
}
This file is part of the Microsoft .NET Framework SDK Code Samples.
<script runat="server">
try
{
//First, create the xml to send using the Xml Serializer
XmlSerializer xs = new XmlSerializer(typeof(Book),
"Microsoft.Samples.Web.Services");
StringBuilder builder = new StringBuilder();
StringWriter writer = new StringWriter(builder);
xs.Serialize(writer, book);
writer.Close();
string serializedContent = builder.ToString();
EditXML.Text = serializedContent;
}
catch (Exception exception)
{
output.Text = "Received error: " + exception.Message;
}
}
try
{
response = service.SendToValidator(EditXML.Text);
output.Text = response;
}
catch (Exception exception)
{
output.Text = "Received error: " + exception.Message;
}
}
</script>
<html>
<body>
<form id="form1" runat="server">
<font face="Verdana">
<p><font size="-1"><b>Step 1:</b> Enter the title and author of
your favorite book and then click <b>Generate XML</b>.</font></p>
<p><b>Book Title: </b><input type=text id="BookTitle" value=""
runat="server"/><br>
<b>Book Author: </b><input type=text id="BookAuthor" value=""
runat="server"/></p>
<p><input id="GenerateButton" type="submit" value="Generate XML"
onserverClick="Button1_Click" runat="server" /></p>
<p><font size="-1"><b>Step 2:</b> Consider editing the generated
XML to make it malformed or not comply with the <a
href="../../Book.xsd">Book schema</a>. If you do not make any edits
validation will be successful.</font></p>
<b>Edit XML:</b><br /><asp:TextBox Rows=5 TextMode=MultiLine
Height=125 Width=500 id="EditXML" Wrap=true runat=server/></p>
<p><input id="SubmitButton" type="submit" value="Submit"
onserverClick="Button2_Click" runat="server" />
<input id="ResetButton" type="submit" value="Reset"
onserverClick="Button3_Click" runat="server" /></p>
<p><b><font size="-1">Response (from Web service):</font></b><br>
<font size="-1" color='red'><asp:Label id="output" text=""
runat="server"/></font></p>
</font>
</form>
</body>
</html>