Saturday, 9 May 2015

How to Solve: Could not load type 'Xrm.XrmServiceContext'

Problem Introduction

It is very often to see this error, especially when you are developing a custom app/website connecting to CRM using Portal Extension method or Simplified Connection.


And actually, I learn it from mistake, I realized my mistake, small mistake that making my example not working.

So here is the step to resolve:

Have you followed this step?
When I say ‘have you’, are you really really comfortable to say yes? Have you really-really followed the instruction?
Well, if you think you ‘have’, it is not your fault, because I also did the same thing.
Now, I still face the error…What should I do?

1. Are you using the correct framework?

If you are using CRM 2015, then you should use .NET 4.5.2.
For CRM 2013, you should use .NET 4.5
And for CRM 4.0 is .NET 4.0

2. Try to check whether you have inserted the generated code

Please check again in your Project, have you inserted the generated code you generated from CrmSvcUtil?

3. Check again your web.config and ContextTypeName (in the ASPX) vs CRMScvUtil Parameters

This is very important, because this was the root of failure I did not realize before.
Here, you need to check first the CrmSvcUtil..
In fact..I generate this:

CrmSvcUtil Parameters

CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization,Microsoft.Xrm.Client.CodeGeneration" /url:"" /out:"XrmContext.cs" / /password:passwordme /namespace:Xrm /serviceContextName:"XrmContext"

You seem my parameter for serviceContextname is “XrmContext”, not the “XrmServiceContext”!!

While I check my web.config:


      <add name="Xrm" type="Xrm.XrmServiceContext, Xrm.CRM.2015.WebExample" />

And also I check my .ASPX code
<asp:LinqDataSource ID="Contacts" ContextTypeName="Xrm.XrmServiceContext" TableName="ContactSet" runat="server" />

Well, I use both in my code :


While, I generate the code using servicecontext parameter to this:


Of course, it wont be able to load the type.

Then, what I do is, I change my web.config and my ASPX from




Or you can re-generate the file and change the the /serviceContextName param from

“XrmContext” to “XrmServiceContext”

It is common mistake and sometimes is hard to realize!

Tips: Since you can have many changes in your CRM Metadata, you might need to re-generate your generated code very often, then I recommend you to consider the batch execution approach.

See this for detail:

Hope this saves your day!


1 comment:

My Name is..