Calling WCF service from a CRM Plug-In

September 29, 2009

I had a requirement to call a WCF service from with a CRM plug-in.  This seemed like this should be straight forward, connecting to the service, building a service reference, setting up the config file and I should be good to go. 

All was fine and good until working with the config file.  When working with a WCF service it expects the configuration properties to be in-process.  For instance, if you’re calling a WCF service from within an ASP.NET web site, then adding something like the snippet below to the web.config will allow you to call service.

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IContract" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="9000000" maxStringContentLength="9000000" maxArrayLength="9000000"
maxBytesPerRead="9000000" maxNameTableCharCount="9000000" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" proxyCredentialType="None"
realm="" />
<!--<message clientCredentialType="UserName" algorithmSuite="Default" />-->
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://[SERVICE_URL]"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IContract"
contract="[CONTRACT]"
name="BasicHttpBinding_IContract" />
</client>
</system.serviceModel&gt

However, the plug-in is registered in the database so an in-process configuration file wasn’t being recognized.  Thankfully the configuration properties are all available via the System.ServiceModel namespace. You can add the code snippet below to setup the configuration when calling the WCF service.

 
using System.ServiceModel;

BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "BasicHttpBinding_IContract";
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TransferMode = TransferMode.Buffered;
binding.UseDefaultWebProxy = true;
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

binding.SendTimeout = new TimeSpan(0, 10, 0);
EndpointAddress endPointAddress = new EndpointAddress("http://[SERVICE_URL]");

ContractClient client = new ContractClient(binding, endPointAddress);

client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

client.ChannelFactory.Credentials.Windows.ClientCredential = new System.Net.NetworkCredential([ACCOUNT], [PASSWORD], [DOMAIN]);

client.[METHOD]();

Entry Filed under: Programming. .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

September 2009
M T W T F S S
« Jul   Nov »
 123456
78910111213
14151617181920
21222324252627
282930  

Most Recent Posts