C# OperationContext.Current在事件中:Null异常
我试图在方法中调用callbackcontract方法,该方法在触发某个事件时调用。但我在尝试获取OperationContext.Current时遇到异常。如何仅在触发此事件时调用回调方法C# OperationContext.Current在事件中:Null异常,c#,wcf,C#,Wcf,我试图在方法中调用callbackcontract方法,该方法在触发某个事件时调用。但我在尝试获取OperationContext.Current时遇到异常。如何仅在触发此事件时调用回调方法 private void recordInserted() //This method is called when the event is fired { ICallbackContract callback = OperationContext.Current.GetCallbackChannel
private void recordInserted() //This method is called when the event is fired
{
ICallbackContract callback = OperationContext.Current.GetCallbackChannel<ICallbackContract>();
callback.insertsuccessful();
}
private void recordInserted()//在触发事件时调用此方法
{
ICallbackContract callback=OperationContext.Current.GetCallbackChannel();
callback.insertsuccessful();
}
从哪里调用recordInserted
方法?服务器端?如果是这样,你就不能用这种方式神奇地向你的客户发送东西
当客户端第一次连接时,您需要存储对回调的引用,然后当触发事件时,您可以使用它将数据发送回客户端
乙二醇
公共类服务
{
ICallbackContract\u回调
公共无效登录()
{
//方法连接客户端调用
_callback=OperationContext.Current.GetCallbackChannel();
}
private void recordInserted()//触发事件时调用此方法
{
_callback.insertsuccessful();
}
}
如果您发布WCF合同规范,也会更容易解释您的recordInserted方法从何处调用?服务器端?是的,它来自服务器端。我明白你的意思。愚蠢的错误。感谢您的帮助。发布您的wcf合同规范。或者解释客户端最初是如何连接到服务器的。您不能只调用源于服务器的方法并让它将数据推回到客户端;该服务器线程对客户端一无所知(因此
OperationContext.Current
为null)
public class Service
{
ICallbackContract _callback
public void Login()
{
//method your connecting client calls
_callback = OperationContext.Current.GetCallbackChannel<ICallbackContract>();
}
private void recordInserted() //This method is called when the event is fired
{
_callback.insertsuccessful();
}
}