Datetimeoffset serialization issues?

Why does this:


[Route("/Request", "GET,POST,PUT,DELETE")]
public class Request : IReturn<RequestResponse>
{
    public int Id {get; set; }
    
    public DateTimeOffset DeliveryDate { get; set; }
    
    public int ContactId { get; set; }
    public int Count { get; set; }
    
    public string Notes { get; set; }

    public DateTimeOffset? CreatedOn { get; set; }
}

public class RequestResponse
{
    public List<Request> Requests { get; set; } = new();

    public ResponseStatus ResponseStatus { get; set; }
}

and this:

    public class Request
    {
        [PrimaryKey] [AutoIncrement] public int Id { get; set; }
 
        [Required] 
        public DateTimeOffset DeliveryDate { get; set; }
        
        [Reference] public Contact Contact { get; set; }

        [Required]
        [References(typeof(Contact))]
        public int ContactId { get; set; }

        [Required]
        public int Count { get; set; }
        
        [StringLength(2000)] public string Notes { get; set; }
        
        [Required] 
        public DateTimeOffset CreatedOn { get; set; }
    }

and this

  var no = new Request
  {
     ContactId = (int)leRequestContact.EditValue,
     Notes = txtModelNotes.Text,
     Count = ct,
     DeliveryDate = deRequestDeliveryDate.DateTimeOffset.ToUniversalTime()
  };

  var res = await Main.Client.PostAsync(no);

always result with this error:

Save failed

Error saving Request! Message: Could not deserialize ‘application/json; charset=utf-8’ request using Elves.ServiceModel.Dto.Request’
Error: The JSON value could not be converted to System.DateTimeOffset. Path: $.DeliveryDate | LineNumber: 0 | BytePositionInLine: 48.

json looks like

no.ToJson()
"{\"Id\":0,\"DeliveryDate\":\"\\/Date(1758013322000)\\/\",\"ContactId\":2,\"Count\":22,\"Notes\":\"\"}"

stacktrace

Save failed

Error saving Request! Message: Could not deserialize 'application/json; charset=utf-8' request using Elves.ServiceModel.Dto.Request'
Error: The JSON value could not be converted to System.DateTimeOffset. Path: $.DeliveryDate | LineNumber: 0 | BytePositionInLine: 48.

i cant even get to the point where i can see it coming in in the back end, it just explodes in the console. tried it with DateTime as well. i have other projects that use DateTimeoffset a lot. no issues.

    <PackageReference Include="ServiceStack.HttpClient" Version="8.8.0" />
    <PackageReference Include="ServiceStack.Text" Version="8.8.0" />

i tried using the jsonapiclient and none of that stuff even works. always a not found error message

i am at a total loss here.

even the sample doesnt work and throws a Not found error with the Hello example. Not found.

8.8.1 didnt fix it either

using bruno and posting this worked:

Request Body
{
  "deliveryDate": "2025-09-09T02:02:02+00:00",
  "contactId": 1,
  "count": 10,
  "notes": "aaa",
  "createdOn": "2025-09-16T18:55:09.1701832+00:00"
}

this is what SS posted via service client (captured by fiddler)

POST http://localhost:5000/Request HTTP/1.1
Host: localhost:5000
Accept-Encoding: gzip, deflate
Accept: application/json
User-Agent: ServiceStackClient/8.81
Connection: Keep-Alive
Content-Type: application/json
Content-Length: 86

{"Id":0,"DeliveryDate":"\/Date(1758064414470)\/","ContactId":1,"Count":123,"Notes":""}

well well well this fixed it

   
    JsConfig.DateHandler = DateHandler.ISO8601;

but why?

fiddler sees this now

{
    "Id": 0,
    "DeliveryDate": "2025-09-16T23:26:43.3173991+00:00",
    "ContactId": 1,
    "Count": 111,
    "Notes": ""
}

but all i did was use x mix to create a new app. why did the defaults not work?

What App did you create? If you created a new ASP .NET Identity Auth template then it would be using Endpoint Routing and your APIs would use System.Text.Json by default in which case your clients can be configured to use the same System.Text.Json Serialization with:

ClientConfig.UseSystemJson = UseSystemJson.Always;

Alternatively if your client needs to use ServiceStack.Text Json Serialization than you can configure it to be more compatible with:

JsConfig.SystemJsonCompatible = true;

Also for .NET 6+ Clients you should use the JsonApiClient that’s in ServiceStack.Client instead of ServiceStack.HttpClient.

I can get the exact command but what should I use for x mix or app mix to get the recommended net 8 or on project that runs kestrel and I can put behind nginx?

I can’t see how to even use the API client. Is there a more fleshed out example than the hello one? Do routes all need to be lower case?

I was using the jsonclient vs the service client and some of my routes kept coming back with not found. Switching back to serviceclient and it all worked again.

I just want to know how to get a skeleton for a rest based set of apis in the most modern, recommended way possible.

Thanks.

You’d start with the project that best matches what you’re trying to build, if you don’t need a Web UI you can use empty web template.

It’s not clear what’s not working? You should just be able to configure your client to use System.Text.Json and then use JsonApiClient as you would a normal ServiceClient, e.g:

ClientConfig.UseSystemJson = UseSystemJson.Always;

var client = new JsonApiClient("https://web.web-templates.io");

var api = await client.ApiAsync(new Hello { Name = "World" });
var ret = await client.GetAsync(new Hello { Name = "World" });

I’d use JsonApiClient directly like this for single user Console or Desktop Apps, but if you’re calling ServiceStack APIs from Web Apps I’d recommend using Dependency Injection for HttpClient instead:

// Program.cs
builder.Services.AddJsonApiClient(builder.Configuration["BaseUrl"]);

// Dependency Injection
class MyService(JsonApiClient client)
{
}

As for routing, when using Endpoint Routing route Definitions use ASP.NET Core’s Routing System which is mostly a superset of ServiceStack Routing with additional features like Route Constraints.

perhaps this is/was my issue. i dont recall ever having to do that in the past

It’s needed when using external clients that uses ServiceStack.Text JSON to call APIs in new ASP .NET Core Templates which use Endpoint Routing and System.Text.Json APIs, you can learn about changes to the ServiceStack templates and improved integration with ASP .NET Core 8 in the release notes starting with ServiceStack v8.1 Release Notes.

seems to be ok now. i will go back and adjust for the new style. thanks!