I’m being forced to connect to some .net service in a VBA tool I’m developing. I use a function to return the cached instance or create a new one if it hasn’t been created yet.
If anything goes wrong with the object (such as a connection time out), the next time I try to use it, I get the error
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.
I’ve seen this error pop up all over the web, but all in proper languages that have try-catch blocks.
Rather than waiting for this error to occur when the cached instance gets used and dealing with it in an “On Error Goto” statement, I’d like to prevent it by happening when the cached object is retrieved for use. I couldn’t find an object model for this thing anywhere, and setting up a watch on the initialized object just shows . What property or test can I use at retrieval time to determine whether the object is in a faulted state?
Private Function GetMRLDIntegrationService() As Object
Const ServiceAddress = "service:mexAddress=""net.tcp://hydfwdvweb001/Integration/MrldIntegrationService.svc/Mex""," & _
"address=""net.tcp://hydfwdvweb001/Integration/MrldIntegrationService.svc/""," & _
"contract=""IMrldSimplexIntegration"", contractNamespace=""http://tempuri.org/""," & _
"binding=""SimplexIntegration"", bindingNamespace=""http://tempuri.org/"""
Static cachedInstance As Object
If Not cachedInstance Is Nothing Then
''//If *DETECT ERROR STATE HERE* Then Set cachedInstance = Nothing
End If
If cachedInstance Is Nothing Then Set cachedInstance = GetObject(ServiceAddress)
If cachedInstance Is Nothing Then Err.Raise 1, , _
"The MRLD server did not respond to the request to provide the service object."
Set GetMRLDIntegrationService = cachedInstance
End Function
Elsewhere, in another method, this is where the error occurs, and this is when it’s too late to deal with the error elegantly:
Private Sub FillFromRiskID(ByVal riskID As String)
...
Process_MRLD_Result GetMRLDIntegrationService().GetSerializedRisk(riskID, "1", uName, pWord)
...
End Sub
Thanks for the help
This alternate solution give the GetService function an option to reset the cached object (i.e. if an error occured), the problem is that I cannot find a way to use this feature such that the Faulted state error can easily be reset, but other errors do not result in redundant erronous requests before the actual error gets processed.
And the calling function: