c# – WCF发布/订阅并使用回调将数据发送给特定用户
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WCF发布/订阅并使用回调将数据发送给特定用户,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4481字,纯文字阅读大概需要7分钟。
内容图文
![c# – WCF发布/订阅并使用回调将数据发送给特定用户](/upload/InfoBanner/zyjiaocheng/821/3e672d21b00044b0911d54afa70ac32b.jpg)
我正在做一个项目,现在变得有点卡住了.我正在创建一个客户端服务器应用程序,它允许客户端订阅服务器以将消息转发给它.
我遇到的问题是,当客户订阅时,我希望他们只收到与他们相关的更新.系统基本上从服务器监视的SQL服务器DB传递消息.收到新邮件后,服务器应仅根据客户端计算机上记录的内容将邮件转发给其应用的客户端.
我已经查看并找到了代码示例,这些代码示例注册要在已订阅的所有客户端上广播的消息,但不会显示如何识别单个客户端以及消息是否适用于它们的任何客户端.
如果有人可以帮助或指出我正确的方向,将不胜感激.
编辑只是为了更清楚我不是在想如何操作回调和订阅但是如何操作订阅服务,当用户订阅时,他们可以提供用户ID以及回调信息,然后可以用来识别特定用户,需要发送消息.
您现在可以在下面找到我的一些代码:
namespace AnnouncementServiceLibrary
{
[ServiceContract(CallbackContract = typeof(IMessageCallback))]
public interface IMessageCheck
{
[OperationContract]
void MessageCheck();
}
}
namespace AnnouncementServiceLibrary
{
public interface IMessageCallback
{
[OperationContract(IsOneWay = true)]
void OnNewMessage(Mess message);
}
}
订阅/退订:
private static readonly List<IMessageCallback> subscribers = new List<IMessageCallback>();
public bool Subscribe()
{
try
{
IMessageCallback callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
//If they dont already exist in the subscribers list, adds them to it
if (!subscribers.Contains(callback))
subscribers.Add(callback);
return true;
}
catch
{
//Otherwise if an error occurs returns false
return false;
}
}
/// <summary>
/// Unsubscribes the user from recieving new messages when they become avaliable
/// </summary>
/// <returns>Returns a bool that indicates whether the operation worked or not</returns>
public bool Unsubscribe()
{
try
{
IMessageCallback callback = OperationContext.Current.GetCallbackChannel<IMessageCallback>();
//If they exist in the list of subscribers they are then removed
if (subscribers.Contains(callback))
subscribers.Remove(callback);
return true;
}
catch
{
//Otherwise if an error occurs returns false
return false;
}
}
最后,当用户在循环时订阅时,这暂时不起作用我希望它根据用户userID过滤LINQ查询:
#region IMessageCheck Members
/// <summary>
/// This method checks for new messages recieved based on those who have subscribed for the service
/// </summary>
public void MessageCheck()
{
//A continuous loop to keep the method going
while(true)
{
//Changes the thread to a sleep state for 2 mins?
Thread.Sleep(200000);
//Go through each subscriber based on there callback information
subscribers.ForEach(delegate(IMessageCallback callback)
{
//Checks if the person who wanted the callback can still be communicated with
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
//Creates a link to the database and gets the required information
List<Mess> mess = new List<Mess>();
List<Message> me;
List<MessageLink> messLink;
AnnouncementDBDataContext aDb = new AnnouncementDBDataContext();
me = aDb.Messages.ToList();
messLink = aDb.MessageLinks.ToList();
//Query to retrieve any messages which are newer than the time when the last cycle finished
var result = (from a in messLink
join b in me
on a.UniqueID equals b.UniqueID
where b.TimeRecieved > _time
select new { b.UniqueID, b.Author, b.Title, b.Body, b.Priority, a.Read, b.TimeRecieved });
//Foreach result a new message is created and returned to the PC that subscribed
foreach (var a in result)
{
Mess message = new Mess(a.UniqueID, a.Author, a.Title, a.Body, a.Priority, (bool)a.Read, a.TimeRecieved);
callback.OnNewMessage(message);
}
}
//If the requesting PC can't be contacted they are removed from the subscribers list
else
{
subscribers.Remove(callback);
}
});
//Sets the datetime so the next cycle can measure against to see if new messages have been recieved
_time = DateTime.Now;
}
}
#endregion
解决方法:
有很多方法可以实现这一目标.考虑到您使用静态列表来维护订阅者,您可以生成一个新对象,如下所示:
class Subscriber
{
public string UserName { get; set; }
public IMessageCallback CallBack { get; set; }
}
然后将您的订阅者存储在List< Subscriber>中.而不是List< IMessageCallback>宾语.
然后,您可以修改您的Subscribe()方法以获取用户名的字符串参数.这将允许您使用linq到对象查询来查找要向其发送消息的用户.
这种技术可以用于任何标识符,但我不确定您是如何尝试过滤消息的.看起来你想用它来用户名,这就是我在这里使用这个选项的原因.但是你可以很容易地为它们的订阅类型设置一个标记Enum并将其传递给它.
如果您想要一个替代方法将您的订阅者存储在静态列表中,您可以查看我写的关于Throttling WCF的文章,我使用GenericDelegates.这可能会为您提供更多选择和想法. http://www.codeproject.com/KB/WCF/wcfesb.aspx本文还将向您展示维护订阅者比在每次调用时检查上下文状态更简单的方法.
内容总结
以上是互联网集市为您收集整理的c# – WCF发布/订阅并使用回调将数据发送给特定用户全部内容,希望文章能够帮你解决c# – WCF发布/订阅并使用回调将数据发送给特定用户所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。