title: Getting started with Exchange Web Services 2010
body: <p>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. (<em>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?!</em>)</p>
  
  <p>My end goal is to be able to <strong>get</strong> and <strong>create/update</strong> 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).</p>
  
  <p>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 <a href="http://msdn.microsoft.com/en-us/library/dd877012.aspx" rel="nofollow noreferrer">the current documentation</a>.</p>
  
  <p>For example, the method used to create a connection to the Exchange server was:</p>
  
  <pre><code>ExchangeService svc = new ExchangeService();
  svc.Credentials = new WebCredentials(AuthEmailAddress, AuthEmailPassword);
  svc.AutodiscoverUrl(AutoDiscoverEmailAddress);
  </code></pre>
  
  <p>For what it's worth, this was using an assembly that came with the sample code: <code>Microsoft.Exchange.WebServices.dll</code> ("MEWS").</p>
  
  <p>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 <a href="http://msdn.microsoft.com/en-us/library/aa563060.aspx" rel="nofollow noreferrer">copied from here</a>:</p>
  
  <pre><code>static void CreateAppointment(ExchangeServiceBinding esb)
  {
      // Create the appointment.
      CalendarItemType appointment = new CalendarItemType();
  
      ...
  }
  </code></pre>
  
  <p>Right away, I'm confronted with the difference between <code>ExchangeService</code> and <code>ExchangeServiceBinding</code> ("ESB"); so I started Googling to try and figure out how to get an ESB definition so that the <code>CreateAppointment</code> method will compile. I found <a href="http://blogs.msdn.com/exchangedev/archive/2007/12/07/generating-exchange-web-services-proxy-classes.aspx" rel="nofollow noreferrer">this blog post</a> that explains how to generate a proxy class from a WSDL, which I did.</p>
  
  <p>Unfortunately, this caused some conflicts where types that were defined in the original Assembly, <code>Microsoft.Exchange.WebServices.dll</code> (that came with the sample code) overlapped with Types in my new <code>EWS.dll</code> assembly (which I compiled from the code generated from the <code>services.wsdl</code> provided by the Exchange server).</p>
  
  <p>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.</p>
  
  <p>So I went back on the hunt. I found instructions on adding service references and web references (i.e. <a href="http://www.techtoolblog.com/archives/add-web-references-to-visual-studio-2008" rel="nofollow noreferrer">the extra steps it takes in VS2008</a>), and I think I'm back on the right track.</p>
  
  <p>I removed (actually, for now, just excluded) all previous assemblies I had been trying; and I added a service reference for <code>https://my.exchange-server.com/ews/services.wsdl</code></p>
  
  <p>Now I'm down to just 1 error and 1 warning.</p>
  
  <blockquote>
    <p><strong>Warning:</strong><br/>
    The element 'transport' cannot contain child element 'extendedProtectionPolicy' because the parent element's content model is empty.</p>
  </blockquote>
  
  <p>This is in reference to a change that was made to web.config when I added the service reference; and I just found <a href="https://stackoverflow.com/questions/1750332/the-element-transport-cannot-contain-child-element-extendedprotectionpolicy-b">a fix for that here on SO</a>. I've commented that section out as indicated, and it did make the warning go away, so woot for that.</p>
  
  <p>The error hasn't been so easy to get around, though:</p>
  
  <blockquote>
    <p><strong>Error:</strong><br/>
    The type or namespace name 'ExchangeService' could not be found (are you missing a using directive or an assembly reference?)</p>
  </blockquote>
  
  <p>This is in reference to the function I was using to create the EWS connection, called by each of the web methods:</p>
  
  <pre><code>private ExchangeService getService(String AutoDiscoverEmailAddress, String AuthEmailAddress, String AuthEmailPassword)
  {
      ExchangeService svc = new ExchangeService();
      svc.Credentials = new WebCredentials(AuthEmailAddress, AuthEmailPassword);
      svc.AutodiscoverUrl(AutoDiscoverEmailAddress);
      return svc;
  }
  </code></pre>
  
  <p>This function worked perfectly with the MEWS assembly from the sample code, but the <code>ExchangeService</code> type is no longer available. (Nor is <code>ExchangeServiceBinding</code>, that was the first thing I checked.)</p>
  
  <p>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.</p>
  
  <p>I know I need to figure out whatever it is that should replace <code>ExchangeService</code> / <code>ExchangeServiceBinding</code>, implement that, and then work through whatever errors crop up as a result of that switch...</p>
  
  <p>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.</p>
  
  <p>What should I replace <code>ExchangeService</code> / <code>ExchangeServiceBinding</code> with? Is there some other location that documents working with EWS that is better than MSDN?</p>