Mik*_*ike 6 c# exchangewebservices exchange-server-2010
我正在使用Microsoft Exchange Web Services 1.1 SDK并使用流连接订阅新邮件通知.一切都适用于接收通知但我偶尔会收到有关我的Exchange无法找到我的订阅的错误.
下面是我用来初始化订阅和我使用的事件的代码.
public void Subscribe()
{
var locateMailbox = new Mailbox
{
Address = "myemail"
};
var folderId = new FolderId(WellKnownFolderName.Inbox, locateMailbox);
var foldersToWatch = new[] {folderId};
StreamingSubscription streamingSubscription =
_exchangeService.SubscribeToStreamingNotifications(foldersToWatch, EventType.NewMail);
// Timeout is set at 1 minute intentionally
var streamingConnection = new StreamingSubscriptionConnection(_exchangeService, 1);
streamingConnection.AddSubscription(streamingSubscription);
streamingConnection.OnSubscriptionError += ResolveError;
streamingConnection.OnDisconnect += Reconnect;
streamingConnection.Open();
}
public void Reconnect(object sender, SubscriptionErrorEventArgs disconnectEventArgs)
{
if (!((StreamingSubscriptionConnection)sender).IsOpen)
((StreamingSubscriptionConnection)sender).Open();
}
public void ResolveError(object sender, SubscriptionErrorEventArgs errorEventArgs)
{
var streamingSubscriptionConnection =
(StreamingSubscriptionConnection) sender;
if (!streamingSubscriptionConnection.IsOpen)
streamingSubscriptionConnection.Open();
}
Run Code Online (Sandbox Code Playgroud)
ServiceLocalException - You must add at least one subscription to this connection before it can be opened.
这个例外不言而喻,我知道我可以简单地创建另一个订阅Reconnect().我希望有人能帮我理解订阅的去向.我无法想象像Exchange 2010这样的产品会丢失我的订阅.另外,我无法指出错误.有时候我可以让我的订阅保持活动状态10分钟,有时我会收到一条关于我的订阅在2-3分钟后无效的错误.
我使用Exchange 2010 SP1是值得的.
通过查看Reflector中的源代码,看起来只有两种方式可以删除订阅(除了处理StreamingSubscriptionConnection,通过调用Remove,我假设你没有做,或者通过订阅返回错误代码除了ServiceError.ErrorMissedNotificationEvents你可以通过查看errorEventArgs.Exception你的ResolveError处理程序来检查错误.如果它是一个实例ServiceResponseException,将它转换为该类型并获取ErrorCode属性.在触发OnSubscriptionError事件后,订阅将自动删除.
获取错误代码可能会帮助您找出发生这种情况的原因,但即使您无法修复它,您也可以确定何时删除订阅并在此情况下异步添加其他订阅.