Here is another issue we are facing after upgrading from ServiceStack 4.5.14 to 6.9 Fields Value is Null in response.Results(Null Reference Exception in Fields Property)
We are currently facing an issue after upgrading from ServiceStack version 4.5.14 to version 6.9.
The issue revolves around a property named ‘Fields’ defined as follows:
[DataMember]
[ApiMember(ExcludeInSchema=true, DataType = "object", Verb = "NONE")]
public FieldDictionary Fields { get; set; }
When attempting to access the ‘Fields’ value in the response object’s ‘Results’ properties using the following code:
var resultFields = response.Results[0].Fields; // This is null in version 6.9
(new System.Collections.Generic.Mscorlib_CollectionDebugView<xx.ServiceModel.Location>(response.Results).Items[0]).Fields
We encounter a System.NullReferenceException with the message “Object reference not set to an instance of an object.”
We have observed that in version 4.5.14, the behavior was correct, and the ‘Fields’ property was returning the value of Fields as expected. However, in ServiceStack version 6.9, the ‘Fields’ is returning null.
We have thoroughly debugged the issue and confirmed that the ‘Fields’ value is present in the ‘httpResponse’ property of the ‘request’ object within the OnEndRequest method in the ServiceStackHost class.
The relevant portions of the code are as follows:
// Within ServiceStackHost class
new System.Collections.Generic.Mscorlib_DictionaryDebugView<string, xx.ServiceModel.IxProperty>((new System.Collections.Generic.Mscorlib_CollectionDebugView<xx.ServiceModel.Location>(((xx.ServiceModel.ObjectSearchResponse<xx.ServiceModel.Location>)((ServiceStack.Host.AspNet.AspNetResponse)((ServiceStack.Host.AspNet.AspNetRequest)request).HttpResponse).Dto).Results).Items[0]).Fields).Items[0]
public virtual void OnEndRequest(IRequest request = null)
{
try
{
if (request != null)
{
if (request.Items.ContainsKey(nameof(OnEndRequest)))
return;
request.Items[nameof(OnEndRequest)] = bool.TrueString;
}
var disposables = RequestContext.Instance.Items.Values;
foreach (var item in disposables)
{
Release(item);
}
RequestContext.Instance.EndRequest();
foreach (var fn in OnEndRequestCallbacks)
{
fn(request);
}
}
catch (Exception ex)
{
Log.Error("Error when Disposing Request Context", ex);
}
finally
{
if (request != null)
{
if (ShouldProfileRequest(request))
{
// Populated in HttpHandlerFactory.InitHandler
if (request.GetItem(Keywords.RequestActivity) is System.Diagnostics.Activity activity
&& activity.GetTagItem(Diagnostics.Activity.OperationId) is Guid id)
{
var ex = HttpError.GetException(request.Response.Dto);
if (ex != null)
Diagnostics.ServiceStack.WriteRequestError(id, request, ex);
else
Diagnostics.ServiceStack.WriteRequestAfter(id, request);
Diagnostics.ServiceStack.StopActivity(activity, new ServiceStackActivityArgs { Request = request, Activity = activity });
}
}
// Release Buffered Streams immediately
if (request.UseBufferedStream && request.InputStream is MemoryStream inputMs)
{
inputMs.Dispose();
}
var res = request.Response;
if (res is { UseBufferedStream: true, OutputStream: MemoryStream outputMs })
{
try
{
res.AllowSyncIO().Flush();
outputMs.Dispose();
}
catch (Exception ex)
{
Log.Error("Error disposing Response Buffered OutputStream", ex);
}
}
}
}
}
However, when running the below console application in version 6.9:
the value of Fields is null:
we are getting the System.NullReferenceException “Object reference not set to an instance of an object.”
var resultFields = response.Results[0].Fields; // This is null in version 6.9
The Fields value is not getting mapped correctly.
using ServiceStack;
namespace test
{
internal class Program
{
static void Main(string[] args)
{
try
{
AA Client = new Client("http://localhost/zz");
Client.Credentials = System.Net.CredentialCache.DefaultCredentials;
Locations request = new Locations()
{
q = "me",
Properties = new PropertyList( "AccessToTestInterface"),
PropertySets = new List<string> { "All" },
pageSize = 30,
PropertyValue = PropertyType.Both,
};
LocationsResponse response = Client.Get<LocationsResponse>(request);
Location loctest = response.Results[0];
string testVal = loctest.GetFieldValue("AccessToTestInterface").StringValue //Object reference not set to an instance of an object
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}\n{ex.StackTrace}\n Inner Exception: {ex.InnerException}");
}
}
}
}
The issue is occurring after upgrading from ServiceStack version 4.5.14 to version 6.9
The behavior was correct in version 4.5.14 Fields value was returing the properties but now in ServiceStack version 6.9 Fields property is returning null
The ‘Fields’ value in the response object’s ‘Results’ properties is null, leading to a Null Reference Exception.
We are seeking your guidance to understand if there are any changes or considerations in ServiceStack version 6.9 that could be causing this behavior. Are there any updates or modifications we need to make in our code to address this issue?Could you please help us out on this?