在EWS中运行多个模拟用户时,我想在每个模拟人日历(可能为100个人)上接收通知时遇到问题。
当前,我有一个Outlook帐户,该帐户有权模拟所有其他用户,并且所有ExchangeService对象都获得此帐户凭据
简短的版本是,当我尝试通过唯一的ID绑定到约会时,只要我只运行一个线程,它就可以工作。当我启动一个包含带有自己的订阅的新Exchangeservice的新线程时,我在Appointment.Bind()-request上未收到任何响应。
当我运行程序的两个实例,每个实例只有一个线程时,它可以正常工作,但是当我使用新的ExchangeService启动新线程时,Appointment.Bind()不会给出任何响应。
奇怪的是,它在两周前就可以正常工作,但是突然它停止工作了,我没有更改代码。
我已经创建了一个快速演示我的问题:
class Program
{
static void Main(string[] args)
{
var x = new OutlookListener("[email protected]");
var y = new OutlookListener("[email protected]");
new Thread(x.Start).Start();
new Thread(y.Start).Start();
while (true)
{
}
}
}
class OutlookListener
{
private ExchangeService _ExchangeService;
private AutoResetEvent _Signal;
public OutlookListener(string emailToImp)
{
_ExchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1)
{
Credentials = new NetworkCredential("[email protected]", "password"),
Url = new Uri("exchangeUrl"),
ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, emailToImp)
};
}
public void Start()
{
var subscription = _ExchangeService.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Calendar },
EventType.Created);
var connection = CreateStreamingSubscription(_ExchangeService, subscription);
Console.Out.WriteLine("Subscription created.");
_Signal = new AutoResetEvent(false);
_Signal.WaitOne();
subscription.Unsubscribe();
connection.Close();
}
private StreamingSubscriptionConnection CreateStreamingSubscription(ExchangeService service, StreamingSubscription subscription)
{
var connection = new StreamingSubscriptionConnection(service, 30);
connection.AddSubscription(subscription);
connection.OnNotificationEvent += OnNotificationEvent;
connection.OnSubscriptionError += OnSubscriptionError;
connection.OnDisconnect += OnDisconnect;
connection.Open();
return connection;
}
private void OnNotificationEvent(object sender, NotificationEventArgs args)
{
// Extract the item ids for all NewMail Events in the list.
var newMails = from e in args.Events.OfType<ItemEvent>()
where e.EventType == EventType.Created
select e.ItemId;
foreach (var newMail in newMails)
{
var appointment= Appointment.Bind(_ExchangeService, newMail); //This is where I dont get a response!
Console.WriteLine(appointment.Subject);
}
}
private void OnSubscriptionError(object sender, SubscriptionErrorEventArgs args)
{
}
private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
{
}
}
有什么建议?
最佳答案
我遇到了同样的问题,发现我的EWS解决方案受到两个因素的限制。
System.Net.ServicePointManager.DefaultConnectionLimit默认情况下设置为2,我已更改为20,我相信它可以与Exchange Online的限制策略相匹配。
其次,可以使用ExchangeService对象上的ConnectionGroupName属性将连接池合并到不同的相关组中,这些组通过DefaultConnectionLimit属性具有并发连接限制。
覆盖设置的一种方法是将您创建的每个ExchangeService对象的ConnectionGroupName属性设置为一个唯一值。
ExchangeService exchangeService = new ExchangeService()
{
ConnectionGroupName = Guid.NewGuid().ToString()
};