CallContext and ApplicationHost

Posted by p2u on Stack Overflow See other posts from Stack Overflow or by p2u
Published on 2010-03-24T15:31:17Z Indexed on 2010/03/24 15:33 UTC
Read the original article Hit count: 603

Filed under:
|

I tried to create an ApplicationHost. But I had errors like SerializationException and FileNotFoundException.

Then I found this blog entry, where it's seem to be a remoting problem. In my little application I use the CallContext, so I tried some approaches. When I empty the CallContext before I create the ApplicationHost, it works:

Programm class:

namespace ApplicationHostDemo
{
    public class Program
    {

    public static void Main(string[] args)
    {

        Evil evil = new Evil();
        CallContext.SetData(Evil.CALLCONTEXT, evil);
        CallContext.FreeNamedDataSlot(Evil.CALLCONTEXT);

        Console.WriteLine("Simple Host-Demo\r\n");
        Host host = CreateHost();

        CallContext.SetData(Evil.CALLCONTEXT, evil);
        host.ProcessRequest("Index.aspx");

        Console.WriteLine("\r\n\r\nSimple Host-Demo end");
        Console.ReadLine();
    }

    public static Host CreateHost()
    {
        return (Host)ApplicationHost.CreateApplicationHost(typeof(Host), "/", Directory.GetCurrentDirectory());
    }

    public class Host : MarshalByRefObject
    {
        public void ProcessRequest(string page)
        {
            SimpleWorkerRequest swr = new SimpleWorkerRequest(page, "", Console.Out);
            HttpRuntime.ProcessRequest(swr);
        }
    }
}

}

Evil class:

namespace ApplicationHostDemo
{
    [Serializable]
    public class Evil : ILogicalThreadAffinative
    {
        public const string CALLCONTEXT = "evil";

        public string Name { get; set; }
    }
}

Do you know or could you explain why it works?

© Stack Overflow or respective owner

Related posts about c#

Related posts about applicationhost