Handler factory HTTP

È possibile generare una nuova istanza di gestore per ogni richiesta HTTP creando una classe che implementa l'interfaccia IHttpHandlerFactory. Nell'esempio riportato di seguito viene utilizzata una handler factory HTTP per creare gestori diversi per una richiesta GET HTTP e per una richiesta POST HTTP. Uno dei gestori costituisce un'istanza del gestore sincrono dell'esempio sopra riportato, mentre l'altro rappresenta un'istanza dell'esempio di gestore asincrono.

using System;
using System.Web;

namespace Handlers
{
    class HandlerFactory : IHttpHandlerFactory
    {
        public IHttpHandler GetHandler(HttpContext context, string requestType, String url, String pathTranslated)
        {         
            IHttpHandler handlerToReturn;
            if("get" == context.Request.RequestType.ToLower())
            {
                handlerToReturn = new SynchHandler();
            }
            else if("post" == context.Request.RequestType.ToLower())
            {
                handlerToReturn = new AsynchHandler();
            }
            else
            {
                handlerToReturn = null;
            }
            return handlerToReturn;
        }
        public void ReleaseHandler(IHttpHandler handler)
        {
        }
        public bool IsReusable
        {
            get
            {
                // To enable pooling, return true here.
                // This keeps the handler in memory.
                return false;
            }
        }
    }
}


[Visual Basic]
Imports System
Imports System.Web

Namespace Handlers
    Class HandlerFactory
        Implements IHttpHandlerFactory

        Public Function GetHandler(ByVal context As HttpContext, ByVal requestType As String, ByVal url As [String], ByVal pathTranslated As [String]) As IHttpHandler Implements IHttpHandlerFactory.GetHandler            
            Dim handlerToReturn As IHttpHandler
            If "get" = context.Request.RequestType.ToLower() Then
                handlerToReturn = New SynchHandler()
            Else
                If "post" = context.Request.RequestType.ToLower() Then
                    handlerToReturn = New AsynchHandler()
                Else
                    handlerToReturn = Nothing
                End If
            End If
            Return handlerToReturn
        End Function

        Public Sub ReleaseHandler(ByVal handler As IHttpHandler) Implements IHttpHandlerFactory.ReleaseHandler
        End Sub

        Public ReadOnly Property IsReusable() As Boolean
            Get
                ' To enable pooling, return true here.
                ' This keeps the handler in memory.
                Return False
            End Get
        End Property
    End Class
End Namespace

Registrare la handler factory HTTP personalizzata creando una voce in Web.config come indicato di seguito:

<configuration>
    <system.web>
        <httpHandlers>
            <add verb="GET,POST" path="*.MyFactory"
                 type="Handlers.HandlerFactory, Handlers" />
        </httpHandlers>
    </system.web>
</configuration>

Vedere anche

Gestori HTTP | Creazione di gestori HTTP