询问者
Thread里使用 回调功能,报错,说未将对象引用到实例化(callback)

问题
-
/////接口 [ServiceContract(Name = "IServices", Namespace = "http://www.wally.com.cn", SessionMode = SessionMode.Required, CallbackContract = typeof(IServicesCallBack))] public interface IServices { [OperationContract(IsOneWay=true,Name="StopMessage")] void StopMessage(); } ////end /////class [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext=true)]//TransactionAutoCompleteOnSessionClose=true,TransactionTimeout="00:03:00")] public class Services : IServices { public void SendMessage()//string message) { try { IsExistBroadcastMessage = true; int sleep = 1000 * 100; string strMess = ""; Thread thread = new System.Threading.Thread(new ThreadStart(delegate { lock (obj) { while (IsExistBroadcastMessage) { if (File.Exists(Application.StartupPath + (@"\BroadcastMessages.xml"))) { XDocument xdoc = XDocument.Load(Application.StartupPath + (@"\BroadcastMessages.xml")); var Messages = from o in xdoc.Descendants("Messages") select o; foreach (var m in Messages) { strMess = m.Value; sleep = (m.LastAttribute == null) ? (Convert.ToInt32(m.LastAttribute.Value.ToString())) : (1000 * 60 * 5); break; } CallBack.BroadcastMessage(strMess);//这里报错(在使用多线程情况才会报错,如果不用多线程就可以正常使用) Thread.Sleep(sleep); } else Thread.Sleep(1000 * 60 * 5); } } })); thread.IsBackground = true; thread.Start(); } catch (Exception ex) { WallySoft.IDAL.ILog log = new WallySoft.BLL.SystemLog(); log.Write(ex, IDAL.LogType.Error); } } ///callback IServicesCallBack CallBack { get { return OperationContext.Current.GetCallbackChannel<IServicesCallBack>(); } } }
认真学习,不耻下问.^_^
全部回复
-
CallBack.BroadcastMessage(strMess);//这里报错(在使用多线程情况才会报错,如果不用多线程就可以正常使用)
认真学习,不耻下问.^_^
我推测是回调通道出错。OperationContext.Current.GetCallbackChannel
你调用前加个代码,看看回调通道是否可用,
Frank Xu Lei--谦卑若愚,好学若饥
专注于.NET平台下分布式应用系统开发和企业应用系统集成
Focus on Distributed Applications Development and EAI based on .NET
【老徐的网站】:http://www.frankxulei.com/
- 已建议为答案 Peter pi - MSFTModerator 2011年9月6日 1:12