I have recently been writing a CRM 4 plugin. Usually when I code a plugin, I write the some unit tests and use the CrmService to create entities, retrieve entities etc.
I usually structure the code so the plugin code calls the same function in a seperate class. This means I can test the code without having to deploy the plugin,
This works ok in CRM 2011 but in CRM 4 plugins use an interface ICrmService.
this forum posts discusses the problem
The problem is you can cast a ICrmService to a CrmService, which basically meant I had to duplicate the methods I had written, one to use CrmService and another to use iCrmService.
To get round the problem I have written a wrapper class which implements the ICrmService interface and basically wraps the CrmService. I found the answer for this solution on this post although it was my plan to do this after I had quickly got the code working with duplicating the methods.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Crm.Sdk; using Microsoft.Crm.SdkTypeProxy; namespace Metaphorix.Crm.Plugin.OrderValidation { class CrmServiceWrapper : ICrmService { private bool _disposed; private readonly CrmService _service; public CrmServiceWrapper(CrmService service) { _service = service; } public Guid Create(BusinessEntity entity) { return _service.Create(entity); } public void Delete(string entityName, Guid id) { _service.Delete(entityName, id); } public object Execute(object request) { return _service.Execute((Request)request); } public string Fetch(string fetchXml) { return _service.Fetch(fetchXml); } public BusinessEntity Retrieve(string entityName, Guid id, global::Microsoft.Crm.Sdk.Query.ColumnSetBase columnSet) { return _service.Retrieve(entityName, id, columnSet); } public BusinessEntityCollection RetrieveMultiple(global::Microsoft.Crm.Sdk.Query.QueryBase query) { return _service.RetrieveMultiple(query); } public void Update(BusinessEntity entity) { _service.Update(entity); } public void Dispose() { _service.Dispose(); } } }