开发者

ASP.NET Communicate between two ASHX handlers

In a small ASP.NET 2.0 web-application, I have two ASHX handlers (one sync, one async) to deal with COMET style messaging. The sync handler takes an addressee and a message, while the async handler holds the "sleeping" connections to all clients.

The Problem: I need to populate some sort of message queue in a shared state. However, even though both handlers are in the same namespace, I can't invoke any (static or non-static) method in the other. I can't even access static methods in the Global-class from any of the handlers. This line simply gives me a compiler error:

Global.SendMessage(recipient, context.Request["message"]);

Compiler Error Message: CS0103: The name 'Global' does not exist in the current context

I'm not exactly sure in which context I am.

The handlers are written directly into a .ashx file like this:

<%@ WebHandler Language="C#" Class="MyApp.MessageHandler" %>

using System;
using System.Web;

namespace MyApp
{
    public class MessageHandler : IHttpHandler

Any hints?

EDIT:

As suggested, I put the class in a code behind file, so the mark up in the ashx file looks like this:

<%@ WebHandler Language="C#" Class="MyApp.MessageHandler" CodeBehind="MessageHandler.as开发者_如何学编程hx.cs" %>

The code behind file is present, with the same name. However, this gives me an error as well:

Parser Error Message: Could not create type 'MyApp.MessageHandler'. Source Error: Line 1: <%@ WebHandler Language="C#" Class="MyApp.MessageHandler" CodeBehind="MessageHandler.ashx.cs" %>

The MessageHandler class:

using System;
using System.Web;

namespace MyApp
{
    public class MessageHandler : IHttpHandler
    {
        /*public MessageHandler()
        {
        }
        */
        #region IHttpHandler Members
        public bool IsReusable
        {
            get { return true; }
        }
        //Called to induce new message from external
        public void ProcessRequest(HttpContext context)
        {
            string recipient = context.Request["recipient"];
            context.Response.Write(recipient);
            if (recipient == "all" || string.IsNullOrEmpty(recipient))
            {
                Global.BroadcastMessage(context.Request["message"]);
            }
            else
            {
                Global.SendMessage(recipient, context.Request["message"]);
            }
        }
        #endregion
    }
}

One sample method in the Global class:

  public static void SendMessage(string recipient, string message)
    {
        Client client;
        if (clients.ContainsKey(recipient))
        {

            if (clients.TryGetValue(recipient, out client))
            {
                client.Message = message;
                client.SetCompleted(true);
            }
        }
    }


If you write your code in code behind files for ashx then you can call static/non-static methods across the handlers as that code gets compiled when you build the project, where as the code in ashx file get compiled on the fly when the handlers receive a request and hence it cannot be called during the normal project build process as it not yet available to compiler

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜