Dynamics CRM as development platform: interacting with CrmService.

This is the first of a series of posts about extending Dynamics CRM 3.0. These posts were originally planned for some months ago, but I have had stuffs to do. Anyway, I recently restarted my activities on the product and I will share my considerations and some implementation details. Let’s start with CrmService. CrmService is a web service and contains the methods you need to write code against all of the entities in the system. A proxy can be obtained (with Visual Studio) adding a web reference to this url:

http://<yourservername>/mscrmservices/2006/crmservice.asmx.

I’m not going to spend words on the web service methods, they are basically CRUD operation. The most important think is, I think, the way your code interacts with the web service. Your code shouldn’t directly use the proxy class generated by Visual Studio, is better to wrap it in a class that, just to begin, exposes the same functionalities the proxy do. Than you can start to write your code against your wrapper and, at later time, introduce enhancement in your wrapper distributing the benefits of these enhancements to all the callers of your wrapper and without modifying them. These enhancements can include:

  • Increase the performance of the HTTP/SOAP calls by implementing this simple but power solution. I already wrote about this, but it’s so powerful!
  • Increase the data retrieving performance by substituting web service calls with database access (on filtered views). A will dedicate an entire post to this.
  • Data caching, for data that isn’t critical and doesn’t change frequently.
  • Data integrity constraints enforcement. Constraints you apply to your entity attributes are enforced only by the UI, not by the web service. But you can read those constraints from the MetadataService and apply them before passing data to CrmService. May be I will write a post even on this.

Here an example of how the wrapper can expose the functionalities the proxy do, with some little features…

public class CrmServiceClient
{
	public virtual BusinessEntity RetrieveEntity(string entityName, Guid entityId)
	{
		return RetrieveEntity(entityName, entityId, new AllColumns());
	}	

	public virtual BusinessEntity RetrieveEntity(string entityName, Guid entityId, ColumnSetBase cols)
	{
		BusinessEntity entity = null;

		try
		{
			entity = m_CrmService.Retrieve(entityName, entityId, cols);
		}
		catch (SoapException ex)
		{
			LogException(ex);

			string msg = ExtractSoapExceptionMessage(ex);
			throw new Exception(msg);
		}
		catch (Exception ex)
		{
			LogException(ex);

			throw;
		}

		return entity;
	}

	// other mehods...
}

And here the way I implemented the HTTP/SOAP calls enhancement explained in the article I linked above (note the use I make of the ConnectionGroupName property to limit the security treats that may occur by setting UnsafeAuthenticatedConnectionSharing to true):

public class CrmServiceClient
{
	// other mehods...	

	private CrmService ActivateCrmService(NetworkCredential credentials, bool unsafeAuthentication)
	{
		CrmService service = new CrmService();
		service.Credentials = credentials;

		if (unsafeAuthentication)
		{
			service.UnsafeAuthenticatedConnectionSharing = true;
			service.ConnectionGroupName = CreateSecureGroupName(credentials);
		}

		return service;
	}

	private string CreateSecureGroupName(NetworkCredential credentials)
	{
		string toEncode = credentials.UserName + credentials.Password + credentials.Domain;
		Byte[] bytes = Encoding.UTF8.GetBytes(toEncode);

		SHA1Managed Sha1 = new SHA1Managed();
		Byte[] updHash = Sha1.ComputeHash(bytes);

		return Encoding.Default.GetString(updHash);
	}
}

I know there’s nothing special in these snippets, but consider this post as an intro. Something more interesting is coming soon.

Advertisements

~ by Matteo on March 28, 2007.

One Response to “Dynamics CRM as development platform: interacting with CrmService.”

  1. >> Increase the performance of the HTTP/SOAP calls by implementing this simple but power solution…
    Goes to some video but no explanation…

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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: