Null reference exception when accessing order store/select server, etc.

Discussions related to custom development with Select.
Post Reply
chris.brady
Posts: 105
Joined: Wed Oct 17, 2012 4:20 pm

Null reference exception when accessing order store/select server, etc.

Post by chris.brady »

We sometimes randomly receive a null reference exception when accessing an order store or select service object via code within a shell package.
It seems like this tends to happen when the client has been idle for a while.

For example, if trying to execute the following code: (note that _spServer is a SelectServer object that was created when the package first loaded, and is passed around)

Code: Select all

var os = _spServer.GetService<IOrderStore>();
We sometimes receive this error:

Object reference not set to an instance of an object.
at SoftPro.ClientModel.Proxies.ClientProxy`1.OnHandleFaultException(FaultException exception)
at SoftPro.ClientModel.Proxies.ClientProxy`1.HandleFaultException(FaultException exception)
at SoftPro.Select.Client.Runtime.LicensingServiceClient.AcquireLease(LicensedProduct product)
at SoftPro.Select.Client.Runtime.Licensing.SoftPro.Select.Client.Runtime.ILicensing.Acquire(LicensedProduct product)
at SoftPro.Select.Client.SelectServerObject.AcquireLicense(LicensedProduct product)
at SoftPro.OrderTracking.Client.Orders.OrderStore.SoftPro.OrderTracking.Client.Orders.IOrderStore.get_Orders()
at WorkBenchLibrary.Utilities.SoftProCommands.OpenCanceledOrder(Guid orderId) in C:\TFS\PGPTitle\SoftPro Shell Packages\EditorFactory\WorkBench\Utilities\SoftProCommands
at WorkBenchLibrary.Utilities.SoftProCommands.OpenOrder(OrderIdentifier orderId, Boolean checkIfCanceled) in C:\TFS\PGPTitle\SoftPro Shell Packages\EditorFactory\WorkBench\Utilities\SoftProCommands.cs

and a mid-tier side error

An error occured on the service boundary.Error: Object reference not set to an instance of an object.
Stack Trace:
at SoftPro.Select.Server.Runtime.Licensing.SoftPro.Select.Client.Runtime.ILicensing.Acquire(LicensedProduct product)
at SyncInvokeAcquireLease(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

Claims:
Type=http://schemas.xmlsoap.org/ws/2005/05/i ... laims/hash, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=System.Byte[]
Type=http://schemas.xmlsoap.org/ws/2005/05/i ... identifier, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... t/identity, Resource=System.IdentityModel.Tokens.SamlNameIdentifierClaimResource
Type=http://schemas.xmlsoap.org/ws/2005/05/i ... identifier, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=System.IdentityModel.Tokens.SamlNameIdentifierClaimResource
Type=http://schemas.softprocorp.com/claims/sessionidentifier, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=d4f1f2ba-8dd2-4c65-b7e1-e81452ab9222
Type=http://schemas.softprocorp.com/claims/trusteeidentifier, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=16eab2cc-ec99-4f45-b8b3-075739766b9c
Type=http://schemas.softprocorp.com/claims/trusteeusername, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=PMCORP\skulbersh
Type=http://schemas.softprocorp.com/claims/t ... emberships, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=7d900037-cad5-485c-9526-4d6d93bfd067,e8ba81d1-8c99-df11-a957-0022fb5d6ef2,75e04731-2fe7-e311-9153-0050569d224b,8acf6cde-11b1-e311-bbd6-0050569d224b,94a15337-3c17-e411-89e8-0050569d27a0,37adf7bf-47f7-df11-a72c-0050569d568a,dfc29e4a-76c4-4fff-85d0-a2dcb8d27d...
Type=http://schemas.softprocorp.com/claims/d ... identifier, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=00000000-0000-0000-0000-000000000000
Type=http://schemas.softprocorp.com/claims/d ... identifier, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=00000000-0000-0000-0000-000000000000
Type=http://schemas.softprocorp.com/claims/aiparticipant, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=True
Type=http://schemas.softprocorp.com/claims/a ... ntationkey, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=4bed6907-345f-475d-9394-982af2e01739
Type=http://schemas.softprocorp.com/claims/a ... ntationkey, Right=http://schemas.xmlsoap.org/ws/2005/05/i ... ssproperty, Resource=Error
Action=http://schemas.softprocorp.com/select/r ... quireLease, ReplyTo=http://schemas.microsoft.com/2005/12/Se ... /Anonymous, FaultTo=

A similar issue sometimes happens when trying to get the select server object through the IOrder object

Code: Select all

var ss = order.GetService<SelectServer>();
However, this only throws the null reference exception with no meaningful stack trace

Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at FieldCodeTracker.PgpFieldCodeTracker.PropertyChangedListener(Object sender, PropertyChangedEventArgs args) in C:\TFS\PGPTitle\SoftPro Shell Packages\Core Package\FieldCodeTracker\FieldCodeTracker.cs

Object reference not set to an instance of an object.
at FieldCodeTracker.Trackers.OrderInstance.OnSettlementDateChanged(Object sender, PropertyChangedEventArgs args) in C:\TFS\PGPTitle\SoftPro Shell Packages\Core Package\FieldCodeTracker\Trackers\OrderInstance.cs

Is there a recommended surefire way to get a fresh copy of the select server, even if the connection to the server has wobbled (NOT been fatally severed)?
BobRichards
Posts: 1376
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Null reference exception when accessing order store/select server, etc.

Post by BobRichards »

Without seeing the rest of the code, I don't know what the problem is. For some reason, the underlying value retained in "_spServer" became null.

However, best practice is to never hold on to interfaces for longer than you need it. When you want it, get it (it's very fast):

Code: Select all

# In shell package...
SelectServer ss = GetService<SelectServer>();
IOrderStore os = ss.GetService<IOrderStore>();
Bob Richards, Senior Software Developer, SoftPro
chris.brady
Posts: 105
Joined: Wed Oct 17, 2012 4:20 pm

Re: Null reference exception when accessing order store/select server, etc.

Post by chris.brady »

So let's say our shell package calls a WinForm from a button click, and our user sits on the form for 4 hours before acting on it. How would we go about refreshing an interface from the form in that case? Would we have to make the package class public and, say, pass it into the constructor of the called form? That feels like asking for trouble, but maybe I'm being paranoid.

It has been our practice to pass in the SelectServer instance only because we've known of no way to get a fresh instance each time we need it.
BobRichards
Posts: 1376
Joined: Wed Jan 15, 2014 3:50 pm
Location: Raleigh, NC
Contact:

Re: Null reference exception when accessing order store/select server, etc.

Post by BobRichards »

Since you are working with a Shell App, why not pass a reference to your startup package class. It inherits from SoftPro.Select.Shell.Package so at any point in time the WinForm can get the services it wants.

Code: Select all

// Pass the startup class that inherits from "Package" to new window form.
winform = new winform(this);
Inside the winform class...

Code: Select all

# Constructor saved passed reference to private class field "_clientPackage".

SelectServer ss = _clientPackage.GetService<SelectServer>();
IOrderStore os = ss.GetService<IOrderStore>();
This is the preferred way so you don't hold any service reference for a long time. Doing the GetServer<T> call is a simple dictionary lookup so it's very fast.

As an aside, I have found that if I quickly try to create lots of orders (os.NewOrder(spec)), I may get a Timeout Exception or an Aggregate Exception containing a single Timeout Exception. If this happens, you should add logic to try again. I'm not sure why this occurs but for whatever reason, the mid-tier gets busy and can't create the order in time to keep the communication channel open.
Bob Richards, Senior Software Developer, SoftPro
Post Reply