mon premier Serveur WCF - pourquoi OperationContext.Le courant est nulle?

Je suis tring pour mettre en œuvre mon premier WCF retour d'appel serveur. C'est mon code:

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ILogCallback))]
public interface ILog
{
}

public interface ILogCallback
{
    [OperationContract(IsOneWay = true)]
    void Push(string callbackValue);
}

public class MyLog : ILog
{

}

class Log
{


    public static void initialize()
    {
        using (ServiceHost host = new ServiceHost(
            typeof (MyLog),
            new Uri[]
                {
                    new Uri("net.pipe://localhost")
                }))
        {

            host.AddServiceEndpoint(typeof (ILog),
                                    new NetNamedPipeBinding(),
                                    "PipeReverse");

            host.Open();
            //TODO: host.Close();
        }
    }

    public static void Push(string s)
    {
        ILogCallback callbacks = OperationContext.Current.GetCallbackChannel<ILogCallback>();
        callbacks.Push(s);
    }
}

alors j'essaie d'utiliser mon serveur à l'aide de ce code:

        Log.initialize();

        while (true)
        {
            Log.Push("Hello");
            System.Threading.Thread.Sleep(1000);
        }

Mais j'ai eu des NPE, parce que OperationContext.Le courant est nul. Pourquoi, quel est le problème et comment le corriger?

OriginalL'auteur javapowered | 2011-09-10