serviceStack and protobuff - servicestack

This is a type I define
public class NewPropVideoResponse
{
[DataMember(Order = 1)]
public int VideoId { get; set; }
}
This is the type I want to return
NewPropVideoResponse[]
How do I use protobuff serialization?

Please read the ServiceStack docs on how to register the ProtoBuf Format. Once the format is registered, if you call your Service using the ProtoBufServiceClient, the client will request the Response to be returned in application/x-protobuf and ServiceStack will respond in kind returning the response serialized in the ProtoBuf format.

Related

Change Autoquery return type in DTO generation

I want to return a custom class from my custom AutoQuery endpoint that inherits QueryResponse<T> but adds a few extra properties.
public class WritingAssignmentBlogLookUpResponse : QueryResponse<BlogDto>, IResponse
{
public bool Success { get; set; }
public string Message { get; set; }
public string DebugMessage { get; set; }
}
But if I specify request like so:
[Route("/assignment/blogs/", "POST")]
public class WritingAssignmentBlogsLookUpRequest : QueryDb<Blog, BlogDto>, IReturn<WritingAssignmentBlogLookUpResponse>
{
}
Then the return type specified in generatd DTO for client.post(req) is QueryResponse<BlogDto> and it doesn't generate WritingAssignmentBlogLookUpResponse at all.
Do I just have to specify return type as any from my typescript service or is there a way to make the types match so I can strongly type it?
You can’t change AutoQuery responses which are already fixed in their service contract definition to return a QueryResponse<T>.
You can add extra info to the Meta Dictionary of the Response DTO (exists for this reason) otherwise if you need to change the Service Contract you’d need to convert it into a normal (I.e. non-AutoQuery) API which could use the Service Gateway to call an existing AutoQuery API that decorates the response.

How do I perform model binding on a HTTP POST in Azure Functions?

I want to bind my HTTP POST parameters to Azure functions, and want to ensure the untrusted data is correctly mapped to my model class.
Right now I'm using a mime type of application/json , and I may need to also support FORM Post and Protobuf.
Since I know support for the latter is a bit complex, how can I perform simple model binding for JSON data?
I've spent a lot of time reading about HTTP triggers, skimming all pages of the Azure Function docs, but don't see anything that helps with this issue. I suspect there is a lower level technology I need to research (webjobs?) for this.
If your HttpTrigger function is configured to accept JSON POST requests, you can bind to your custom model type as follows:
using System.Net;
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
public static async Task<HttpResponseMessage> Run(Person person, HttpRequestMessage req)
{
return person.FirstName == null
? req.CreateResponse(HttpStatusCode.BadRequest, "Who are you?")
: req.CreateResponse(HttpStatusCode.OK, $"Hello {person.FirstName}!");
}

ServiceStack version 3.9.56 gives NullReferenceException error at metadata json page

I just downloaded ServiceStack with NuGet. Version 3.9.56.
I am trying simple webservice but when i open metadata json page it gives NullReferenceException error.
My service is here:
[Route("/users")]
[Alias("Users")]
public class User
{
[Alias("UserID")]
public int id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
}
public class UsersService:Service
{
public object Get(User request)
{
var result = new List<User>();
result = Db.Select<User>();
return result;
}
}
There is a known issue that might explain your null reference exception. However, you do not want IReturnVoid, unlike in your other post, so the answer here is not to wait for ServiceStack to be fixed but to improve your DTO's declaration:
Your UsersService implementation is returning a List<User> object for your User request. You can document this in ServiceStack like so:
public class User : IReturn<List<User>>
{
...
}
This may fix the issue you are seeing on the metadata page as ServiceStack now knows the type of response to expect for the User message. There are other benefits to decorating your request DTOs with IReturn:
The typed C# client will be easier to use, as the client can know the type of your response message
The Swagger UI, if you use it, will know about and automatically document the response type

Is it possible to send raw json with IRestClient

I love the simplicity of using servicestack's IRestClient to test my api, but I need to replicate a test scenario when someone sends an incomplete object. For instance if my dto looks like this:
public class Todo
{
public long? Id { get; set; }
public string Content { get; set; }
public int? Order { get; set; }
public bool? Done { get; set; }
}
And I want to do a PUT with partial json like this:
{"Id": 99999, "Order":1}
How do I send just the above with the json client. If I use the typed version the client sends the entire object with defaults.
I think it might be easier to use ServiceStack's Http Utils (link is to POST but applies to PUT) to PUT the data.
"http://localhost/todo"
.PutJsonToUrl(#"{""Id"":9999,""Order"":1}");
Also, I'm not sure exactly what you are trying to test. But, if you're testing your Service the request (your json string) will be deserialized (pretty sure using ServiceStack.Text) into the Todo class so it will get the object with defaults for missing fields. See the test below.
[Test]
public void TestJson()
{
var json = #"{""Id"": 99999, ""Order"":1}";
var todo = new ServiceStack.Text.JsonSerializer<Todo>().DeserializeFromString(json);
Assert.IsNull(todo.Done);
Assert.IsNull(todo.Content);
}

servicestack text, json deserialization Index was outside the bounds of the array exception

i was diagnosing an issue where i had json or jsv objects being received in web form or query variables and was getting an Index was outside the bounds of the array exception being thrown by servicestack. This is a rest client in another product sending this to my servicestack rest services.
I narrowed it down to de-serializing a form variable with an empty string as the value instead of json.
this is a simple test case that does the same thing. I would have expected null being returned?
v3.9.26 servicestack.text
`
class simpleDTO {
public string FirstName { get; set; }
public string LastName { get; set; }
}
[TestMethod]
public void TestMethod1()
{
var json = "";
var o = JsonSerializer.DeserializeFromString(json, typeof(simpleDTO));
Assert.IsNull(o);
}`
That issue was fixed in this commit: 6ea6f235dc and should be included in the next ServiceStack.Text release.

Resources