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.

image

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?

https://msdn.microsoft.com/en-us/library/gg695790.aspx
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:"https://aileenexample.api.crm5.dynamics.com/XRMServices/2011/Organization.svc" /out:"XrmContext.cs" /username:admin@aileenexample.onmicrosoft.com /password:passwordme /namespace:Xrm /serviceContextName:"XrmContext"
pause 

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

While I check my web.config:

Web.Config

<microsoft.xrm.client>
    <contexts>
      <add name="Xrm" type="Xrm.XrmServiceContext, Xrm.CRM.2015.WebExample" />
    </contexts>
</microsoft.xrm.client>

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 :

Xrm.XrmServiceContext

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

image

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

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

Xrm.XrmServiceContext

to

Xrm.XrmContext

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:

http://missdynamicscrm.blogspot.sg/2014/05/crm-sdk-smart-way-to-use-crmsvcutilexe.html

Hope this saves your day!

Thanks!

1 comment:

My Name is..