Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get OrganizationServiceProxy from CrmOrganizationServiceContext in external MVC app

I'm want to be able to set the caller ID on my

XrmServiceContext : Microsoft.Xrm.Client.CrmOrganizationServiceContext

Context that has been generated for crm using svcutil.exe.

As far as I can tell I cant do this on an existing connection and I need to first create an instance of OrganizationServiceProxy set the CallerID and then pass it as a paramater to a new XrmServiceContext which I can then use instead.

However I'm kind of stuck on how I go from a CrmOrganizationServiceContext to having a OrganizationServiceProxy

The program is a separate .Net4.5 application

Any helpful tips or links?


Edit: Found this page just after posting this: http://msdn.microsoft.com/en-us/library/gg695810.aspx

So it may be as simple as:

    var connection = new CrmConnection("Xrm");
    connection.CallerId = uide;
    _serviceContext = new XrmServiceContext(connection);

Edit 2: It was not as simple as that. Doing this resulted in no change of what data I received.

    CrmConnection connection = new CrmConnection("Xrm");
    connection.CallerId = Guid.NewGuid();//u.Id;

    _serviceContext = new XrmServiceContext(connection);

It compiles and dosen't crash but I was suspicious when I used the id of a user with very low privledges but still got all data back, I then tried generating a new Guid for every instance of the XrmServiceContext but I am still getting everything back. So I am guessing it is not being used.. or I am missing something else.

Edit 3 Doing a WhoAmIRequest after the CallerID has been set still returns the same user that is set in the connection string.

Edit 4 Seems my problems are Cache related. In my implementation I need to first make a call to the service context to figure out the Guid of the user I want to impersonate. This call is made without CallerID set. If I skip this initial query and just set a specific Guid from the beginning the CallerID works. I'm guessing this is because the service context has cached my original CallerId or something similar.

Now I just have to figure out how to clear the cache in CRM 2013 SDK.

Edit 5 By turning of the cache completly using this guide: http://msdn.microsoft.com/en-us/library/gg695805.aspx I have gotten it to work. I would however prefer if I could just clear it out at the one point I need to instead of disabling it completly.

If someone can show me how to empty the service context cache using code I will mark that as the correct solution

like image 936
JensB Avatar asked Dec 19 '13 12:12

JensB


2 Answers

There is a method that can be used when dealing with your "_serviceContext"

You should be able to use: _serviceContext.ClearChanges(); This clears all tracking of a particular entity within the Cache. See the Public Methods Section

like image 91
Bactos Avatar answered Oct 30 '22 13:10

Bactos


The problem is related to the default instanceMode that is defined in the web.config under the microsoft.xrm.client section.

By default, the setting is set to PerRequest

PerRequest – returns the same first instance in the context of a Web request, for example. one instance for each HttpContext instance.

So, in this case, when you do the initial call to work out which user you want to set the CallerId to, the instance is being 'cached' (for lack of a better word) and on subsequest calls within the same request, this instance is being returned, even if you are creating a new XrmServiceContext

The solution is to change the instanceMode to PerInstance

PerInstance – returns a new instance on each call.

Modify your web.config so that the instanceMode attribute is specified correctly

 <microsoft.xrm.client>
    <contexts>
      <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" serviceName="Xrm" />
    </contexts>
    <services>
      <add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client" instanceMode="PerInstance" />
    </services>
  </microsoft.xrm.client>

Found this information in the article posted by JensB in his 5th edit: http://msdn.microsoft.com/en-us/library/gg695805.aspx

like image 1
link64 Avatar answered Oct 30 '22 12:10

link64