C# querying the EWS Managed API from as asp.net core web api
Some organisations are still running on old Microsoft Exchange (AKA EWS). you can read more information here.
Let see how we can integrate the exchange and access the information.
Best practice is to register you http Client as a singleton, same with ExchangeService client.
You will need to reference “Microsoft.Exchange.WebServices.Data” in order to use this.
as part of ConfigureServices(IServiceCollection services) you will need to add
services.AddSingleton(p => { return new ExchangeService(ExchangeVersion.Exchange2016) { UseDefaultCredentials = false, Url = new Uri("<URL HERE>"), Credentials = new WebCredentials("<Username>", "<Password>") }; });
Now you can use DI to inject this service to your implimentation
public class MyExchangeService : IMyExchangeService { private readonly ExchangeService _service; public ExchangeWebService(ExchangeService service) { _service = service; } public async Task<IReadOnlyList<CalendarEvent>> GetEventsAsync(string[] emails, DateTime startTime, DateTime endTime) { var attendees = new List<AttendeeInfo>(); foreach (var email in emails) { attendees.Add(new AttendeeInfo { SmtpAddress = email , AttendeeType = MeetingAttendeeType.Required }); } return await _service.GetUserAvailability(attendees, new TimeWindow(startTime, endTime), AvailabilityData.FreeBusyAndSuggestions, new AvailabilityOptions { }); } }
That is it.