简体   繁体   English

我的第一个WCF服务器 - 为什么OperationContext.Current为null?

[英]my first WCF Server - why OperationContext.Current is null?

I'm tring to implement my first WCF call-back server. 我想实现我的第一个WCF回叫服务器。 This is my 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);
    }
}

then I try to use my server using this code: 然后我尝试使用此代码使用我的服务器:

        Log.initialize();

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

But I got NPE, because OperationContext.Current is null. 但我得到了NPE,因为OperationContext.Current为null。 Why, what's wrong and how to fix that? 为什么,出了什么问题以及如何解决这个问题?

Because you are NOT in the context of an operation. 因为您不在操作的上下文中。

You're simply calling a static method of the Log class. 您只是调用Log类的静态方法。

For you to be in an Operation Context your call MUST have been come from a client that is being serviced by your WCF server. 为了使您处于操作上下文中,您的调用必须来自您的WCF服务器正在服务的客户端。

You are missing the subscriber's subscription. 您缺少订阅者的订阅。 The way you do this is to create a [oneway] operation in your MyLog WCF server called something like: " void SendMeLogs() ". 这样做的方法是在MyLog WCF服务器中创建一个[oneway]操作,例如:“ void SendMeLogs() ”。 This will open the client callback channel. 这将打开客户端回调通道。 You Then have to implement SendMeLogs() in lines of something like: 然后你必须在以下行中实现SendMeLogs()

void SendMeLogs()
{
  while(CheckLogsForNewData())
 {
   PushOnTheClient();
 }
}

Since the SendMeLogs() function is oneway, the client will not block, but will start the subscription to your log server. 由于SendMeLogs()函数是单向的,因此客户端不会阻塞,但会启动对日志服务器的订阅。 (you can search the net for sample code for duplex calculator in wcf for a good example of this architecture). (您可以在网上搜索wcf中双工计算器的示例代码,以获得此架构的一个很好的示例)。 The key however is that you must have a nice unsubscribe method like " StopSendingMeLogs " to break the loop, and also make the PushOnTheClient function fail safe, in case the client terminates or the specific client connection goes down. 关键是你必须有一个很好的取消订阅方法,比如“ StopSendingMeLogs ”来打破循环,并且还要使PushOnTheClient函数失败,以防客户端终止或特定客户端连接断开。 The " CheckLogsForNewData " function should ideally be a shared (static) implementation in your case 理想情况下,“ CheckLogsForNewData ”函数应该是您的案例中的共享(静态)实现

OperationContext.Current is a thread-static property that is initialized when request arrives to the server. OperationContext.Current是一个线程静态属性,在请求到达服务器时初始化。 Here's what you do to call the callback 以下是您调用回调的方法

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

public class MyLog : ILog
{
  void PushOnTheClient()
  {
        ILogCallback callbacks = OperationContext.Current.GetCallbackChannel<ILogCallback>();
        callbacks.Push(s);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OperationContext.Current中的WCF MessageHeaders - WCF MessageHeaders in OperationContext.Current 当OperationContext.Current为null时访问WCF MessageHeader - Accessing a WCF MessageHeader when OperationContext.Current is null 为什么IAuthorizationPolicy.Evaluate()中的OperationContext.Current为null,而只有前两次? - Why is OperationContext.Current null in IAuthorizationPolicy.Evaluate() but only the first two times? 如何模拟OperationContext.Current(WCF消息) - How to mock OperationContext.Current (WCF Message) 访问WCF编码器中的传入消息属性(OperationContext.Current为null) - Get access to incoming message properties in WCF encoder (OperationContext.Current is null) WCF Windows服务中当前OperationContext为null - current OperationContext is null in WCF Windows Service 将其存储到静态变量时无法访问OperationContext.Current - I cannot reach OperationContext.Current when I store it to a static variable OperationContext.Current.SessionId为null - OperationContext.Current.SessionId getting null WCF服务中的Mock OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name - Mock OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name in WCF service 在异步方法中,OperationContext为null - OperationContext is null in async methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM