BrooklynSon
BrooklynSon

Reputation: 2103

Sometime IOrganizationService object is null

I've noticed usually after a request that has a lot of records if I try to make another Organization request the IOrganizationService object will be null for some time.

CrmServiceClient conn = new CrmServiceClient(connectionString);

IOrganizationService service = (IOrganizationService)conn.OrganizationWebProxyClient != null ?
                    (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;    

OrganizationServiceContext ocontext = new OrganizationServiceContext(service);

Right at the point where I'm trying to new up an OrganizationServiceContext the debugger will complain the service is null.

Anyway to avoid this?

Upvotes: 0

Views: 1228

Answers (2)

BrooklynSon
BrooklynSon

Reputation: 2103

This is what I put so far to keep it from blowing up, however sometimes it takes long though for the connection to be recreated in a ready state.

    public static CrmServiceClient getCrmServiceClient(string connectionString)
    {
        CrmServiceClient conn;
        conn = new CrmServiceClient(connectionString);

        while (conn.IsReady == false)
        {
            conn = new CrmServiceClient(connectionString);
            if(conn.IsReady == false)
            {
                System.Threading.Thread.Sleep(2000);
            }
        }
        return conn;
    }

Upvotes: 0

gnud
gnud

Reputation: 78618

The CrmServiceClient implements IOrganizationService itself. You shouldn't need to reach into the internals to get the "real" service. It's in theory better to use the outer service, because it handles token refresh automatically.

In my experience, when the inner service is null, it's because the connection failed somehow. Did you look at conn.IsReady and conn.LastCrmError?

Upvotes: 3

Related Questions