Procedura: creare metodi asincroni del servizio Web

Questo argomento è specifico di una tecnologia legacy. Servizi Web XML e client di servizi Web XML devono essere creati attualmente tramite Windows Communication Foundation.

La procedura seguente spiega come convertire un metodo del servizio Web in una coppia di metodi progettata per l’accesso asincrono. Si tratta di una procedura conforme al modello di struttura asincrono di .NET Framework. Nell’argomento Metodi di servizio Web asincroni vengono fornite informazioni sul funzionamento della procedura nonché sulle modalità in cui lo strumento Wsdl.exe genera classi proxy client in grado di accedere in modo asincrono ai metodi del servizio Web nonostante questi siano progettati per l’accesso sincrono.

Per implementare un metodo di servizio Web asincrono

  1. Suddividere un metodo di aervizio Web sincrono in due metodi, ognuno con lo stesso nome di base a cui aggiungere il prefisso Begin nel primo caso e il prefisso End nel secondo.

  2. L'elenco di parametri per il metodo Begin contiene tutti i parametri in e per riferimento per la funzionalità del metodo, oltre a due parametri aggiuntivi.

    • I parametri By reference sono elencati come parametri in.

    • Il penultimo parametro deve essere un AsyncCallback. Il parametro AsyncCallback consente a un client di fornire un delegato che viene richiamato quando il metodo è stato completato. Durante la chiamata di un metodo del servizio Web asincrono a un altro metodo asincrono, il parametro può essere passato nel penultimo parametro di quel metodo.

    • L'ultimo parametro è un Object. Il parametro Object consente a un chiamante di fornire informazioni sullo stato al metodo. Durante la chiamata di un metodo del servizio Web asincrono a un altro metodo asincrono, il parametro può essere passato nell’ultimo parametro di quel metodo.

    • Il valore restituito deve essere di tipo IAsyncResult.

  3. L'elenco di parametri per il metodo End è costituito da un IAsyncResult seguito da parametri out e by reference specifici della funzionalità del metodo.

    • Il valore restituito è dello stesso tipo del valore restituito di un metodo di servizio Web sincrono.

    • I parametri By reference sono elencati come parametri out.

Esempio

using System;
using System.Web.Services;

[WebService(Namespace="https://www.contoso.com/")]
public class MyService : WebService 
{
    public RemoteService remoteService;
    public MyService() 
    {
        // Create a new instance of proxy class for 
        // the Web service to be called.
        remoteService = new RemoteService();
    }
    // Define the Begin method.
    [WebMethod]
    public IAsyncResult BeginGetAuthorRoyalties(String Author,
        AsyncCallback callback, object asyncState) 
    {
        // Begin asynchronous communictation with a different XML Web
        // service.
        return remoteService.BeginReturnedStronglyTypedDS(Author,
            callback,asyncState);
    }
    // Define the End method.
    [WebMethod]
    public AuthorRoyalties EndGetAuthorRoyalties(IAsyncResult
        asyncResult) 
    {
        // Return the asynchronous result from the other Web service.
        return remoteService.EndReturnedStronglyTypedDS(asyncResult);
    }
}
Imports System.Web.Services
<WebService(Namespace:="https://www.contoso.com/")> _
Public Class MyService
    Inherits WebService
    Public remoteService As RemoteService

    Public Sub New()
        MyBase.New()
        ' Create a new instance of proxy class for 
        ' the Web service to be called.
        remoteService = New RemoteService()
    End Sub

    ' Define the Begin method.
    <WebMethod()> _
    Public Function BeginGetAuthorRoyalties(ByVal Author As String, _
    ByVal callback As AsyncCallback, ByVal asyncState As Object) _
        As IAsyncResult
        ' Begin asynchronous communictation with a different XML Web
        ' service.
        Return remoteService.BeginReturnedStronglyTypedDS(Author, _
            callback, asyncState)
    End Function
    ' Define the End method.
    <WebMethod()> _
    Public Function EndGetAuthorRoyalties(ByVal asyncResult As _
        IAsyncResult) As AuthorRoyalties
        ' Return the asynchronous result from the other Web service.
        Return remoteService.EndReturnedStronglyTypedDS(asyncResult)
    End Function
End Class

Vedere anche

Attività

Procedura: concatenare le chiamate asincrone con un metodo del servizio Web

Concetti

Metodi di servizio Web asincroni
Comunicazione asincrona con i servizi Web XML