Getting started with Exchange Web Services 2010

Adam Tuttle picture Adam Tuttle · Mar 18, 2010 · Viewed 49.8k times · Source

I've been tasked with writing a SOAP web-service in .Net to be middleware between EWS2010 and an application server that previously used WebDAV to connect to Exchange. (As I understand it, WebDAV is going away with EWS2010, so the application server will no longer be able to connect as it previously did, and it is exponentially harder to connect to EWS without WebDAV. The theory is that doing it in .Net should be easier than anything else... Right?!)

My end goal is to be able to get and create/update email, calendar items, contacts, and to-do list items for a specified Exchange account. (Deleting is not currently necessary, but I may build it in for future consideration, if it's easy enough).

I was originally given some sample code, which did in fact work, but I quickly realized that it was outdated. The types and classes used appear nowhere in the current documentation.

For example, the method used to create a connection to the Exchange server was:

ExchangeService svc = new ExchangeService();
svc.Credentials = new WebCredentials(AuthEmailAddress, AuthEmailPassword);
svc.AutodiscoverUrl(AutoDiscoverEmailAddress);

For what it's worth, this was using an assembly that came with the sample code: Microsoft.Exchange.WebServices.dll ("MEWS").

Before I realized that this wasn't the current standard way to accomplish the connection, and it worked, I tried to build on it and add a method to create calendar items, which I copied from here:

static void CreateAppointment(ExchangeServiceBinding esb)
{
    // Create the appointment.
    CalendarItemType appointment = new CalendarItemType();

    ...
}

Right away, I'm confronted with the difference between ExchangeService and ExchangeServiceBinding ("ESB"); so I started Googling to try and figure out how to get an ESB definition so that the CreateAppointment method will compile. I found this blog post that explains how to generate a proxy class from a WSDL, which I did.

Unfortunately, this caused some conflicts where types that were defined in the original Assembly, Microsoft.Exchange.WebServices.dll (that came with the sample code) overlapped with Types in my new EWS.dll assembly (which I compiled from the code generated from the services.wsdl provided by the Exchange server).

I excluded the MEWS assembly, which only made things worse. I went from a handful of errors and warnings to 25 errors and 2,510 warnings. All kinds of types and methods were not found. Something is clearly wrong, here.

So I went back on the hunt. I found instructions on adding service references and web references (i.e. the extra steps it takes in VS2008), and I think I'm back on the right track.

I removed (actually, for now, just excluded) all previous assemblies I had been trying; and I added a service reference for https://my.exchange-server.com/ews/services.wsdl

Now I'm down to just 1 error and 1 warning.

Warning:
The element 'transport' cannot contain child element 'extendedProtectionPolicy' because the parent element's content model is empty.

This is in reference to a change that was made to web.config when I added the service reference; and I just found a fix for that here on SO. I've commented that section out as indicated, and it did make the warning go away, so woot for that.

The error hasn't been so easy to get around, though:

Error:
The type or namespace name 'ExchangeService' could not be found (are you missing a using directive or an assembly reference?)

This is in reference to the function I was using to create the EWS connection, called by each of the web methods:

private ExchangeService getService(String AutoDiscoverEmailAddress, String AuthEmailAddress, String AuthEmailPassword)
{
    ExchangeService svc = new ExchangeService();
    svc.Credentials = new WebCredentials(AuthEmailAddress, AuthEmailPassword);
    svc.AutodiscoverUrl(AutoDiscoverEmailAddress);
    return svc;
}

This function worked perfectly with the MEWS assembly from the sample code, but the ExchangeService type is no longer available. (Nor is ExchangeServiceBinding, that was the first thing I checked.)

At this point, since I'm not following any directions from the documentation (I couldn't find anywhere in the documentation that said to add a service reference to your Exchange server's services.wsdl -- but that does seem to be the best/farthest I've gotten so far), I feel like I'm flying blind.

I know I need to figure out whatever it is that should replace ExchangeService / ExchangeServiceBinding, implement that, and then work through whatever errors crop up as a result of that switch...

But I have no idea how to do that, or where to look for how to do it. Googling "ExchangeService" and "ExchangeServiceBinding" only seem to lead back to outdated blog posts and MSDN, neither of which has proven terribly helpful thus far.

What should I replace ExchangeService / ExchangeServiceBinding with? Is there some other location that documents working with EWS that is better than MSDN?

Answer

Joe Doyle picture Joe Doyle · Mar 24, 2010

I would recommend moving to the Managed EWS API. It's much easier to use than trying to cobble everything together yourself. It supports both Exchange 2007 SP1 and Exchange 2010. I think you'll find it much easier to use and will probably eliminate your issues.

Version 1: Here is the download from Microsoft.

And here are the MSDN docs.

For Version 2 (thanks @ruffin!)

Download - MSDN Docs


Added by question asker:

Additional links that proved helpful: