Calling WCF service from a CRM Plug-In

September 29, 2009 at 7:30 pm 5 comments

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]();

Advertisement

Entry filed under: Programming. Tags: .

Dynamic Worksheet – Specified SQL server not found CRM Calendar Control Bug – DataValue returns incorrect value

5 Comments Add your own

  • 1. Dexter  |  May 13, 2010 at 7:35 pm

    Steve.
    My searches has lead me to your blog, looks very promising as I have the same requirement.

    Im trying to understand how the service model is dynamically created (and apologies for my dumb question).
    So my question is

    you have a ContractClient class, i cant seem to find where this is from. Is this from the system.servicemodel reference?

    Hope you can answer my question.
    Thanks

    Reply
  • 2. Steve Suh  |  May 13, 2010 at 8:08 pm

    Dexter,

    I should have explained that. The ContractClient just happens to be the name of the method from my custom WCF service. So once you add the Service Reference to your WCF service, you can replace ContractClient with whatever the name of your class is.

    steve

    Reply
  • 3. Daniel Sabater  |  November 10, 2010 at 8:26 am

    Interesting, but what would you do if you had to change the endpoint address? Think of Development/Testing/Production environments. I guess there must be a way to include the service configuration in the Plugin “SecureConfiguration” parameter.

    Reply
  • 4. TN  |  January 4, 2011 at 9:44 am

    Dont u put this code in the plugin? because you mentioned that

    So once you add the
    Service Reference to your WCF service,
    you can replace ContractClient with whatever the name of your class is.
    

    Did u mean once you add the service reference into your plugin and did u mean just reference (the dll of the wcf service) or service reference?

    Thank you

    Reply
  • 5. Sergio Moreno  |  November 4, 2011 at 10:07 pm

    It works!!!

    but remember to “iisreset /noforce” and restart the async service after doing it

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

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


Follow

Get every new post delivered to your Inbox.