CallContext and ApplicationHost
- by p2u
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?