Michael Friis' Blog

About


Interacting with the Dynamics CRM Web Service through WCF

Before I could get started on LinqtoCRM, I had to get Visual Studio 2008/WCF and CRM to agree on a common mode of interaction. I must confess that, in the past, I’ve only picked up just enough web services knowledge to get things humming (which was almost nothing in VS 2003/2005). WCF, with its notions of “endpoints” and other newfangled stuff, seems a bit more configuration heavy. Here’s what I did to get it to work. In “app.config”, you need this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="CrmServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
        receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
        bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
        maxBufferSize="100000000" maxBufferPoolSize="100000000" maxReceivedMessageSize="100000000"
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
        useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="100000000" maxArrayLength="100000000"
          maxBytesPerRead="4096" maxNameTableCharCount="100000000" />
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://foo/MSCRMServices/2006/CrmService.asmx"
      binding="basicHttpBinding" bindingConfiguration="CrmServiceSoap"
      contract="ServiceReference.CrmServiceSoap" name="CrmServiceSoap" />
    </client>
  </system.serviceModel>
</configuration>

And in code, do something like this:

CrmServiceSoapClient client = new CrmServiceSoapClient();
client.ClientCredentials.Windows.ClientCredential.Domain = "foo";
client.ClientCredentials.Windows.ClientCredential.UserName = "bar";
client.ClientCredentials.Windows.ClientCredential.Password = "foo";
client.ClientCredentials.Windows.AllowedImpersonationLevel =
	System.Security.Principal.TokenImpersonationLevel.Impersonation;

Addendum: I just noticed that Visual Studio still lets you create a traditional web reference (as opposed to a service reference). This may be a lower friction approach:

Visual Studio/.Net changes in Beta 2

A new beta of Visual Studio was released yesterday, you can read up on the general stuff at ScottGu’s Blog.
The release broke the various projects I’m working on in weird and wonderful ways, here are some of them:

  • The expression tree grammar has changed, some types have disappeared only for new ones to show up.
  • Anonymous types no longer have a zero-argument constructors (from the reflection API), but have to be initialized with their full complement of properties. The properties are read-only, even through the reflection API.
  • The old expression-tree visualizer doesn’t work with the new expression trees, get the new samples.
  • The IQueryable interface has been expanded to include an IQueryProvider-property. Depending on your implementation, you can get around this be returning this and implementing IQueryProvider in your IQueryable (as a side note, it would be great if someone could document the thinking behind the various interfaces floating around in System.Linq, even if it’s still subject to change).

I haven’t really pondered the deeper implications and/or motivations of these changes, merely dealt with them.

UPDATE: Matt Warren has written a coherent description of the IQueryable and associated interfaces, parts 1, 2, 3, 4 and 5.