I can call my service using “/api/X” path via Javascript. (POST verb)
I can call same service without request object using client.Get(serviceUrl) //client is JsonServiceClient
But client.Send(X) does not work. I’m getting weird 404 NotFound response?
Am I missing something? And how can I debug problem?
The cost is 5 hours till now!
CheckList
- X class have two string property (No enum or customType)
- X and XResponse has DataContract and DataMember attributes (Trial)
Code:
In AppHost.cs
base.SetConfig(new EndpointHostConfig
{
GlobalResponseHeaders =
{
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
},
AppendUtf8CharsetOnContentTypes = new HashSet<string> { ContentType.Html },
DebugMode = true, //Show StackTraces in service responses during development
LogFactory = new ElmahLogFactory(new Log4NetFactory())
});
//Set JSON web services to return idiomatic JSON camelCase properties
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
//Configure User Defined REST Paths
Routes.Add<JobRequest>("/job");
In Model.cs
[DataContract]
public class JobRequest : IReturn<JobRequestResponse>
{
[DataMember]
public string JobRequestEnum { get; set; }
[DataMember]
public string JobData { get; set; }
}
[DataContract]
public class JobRequestResponse : IHasResponseStatus
{
[DataMember]
public string Message { get; set; }
[DataMember]
public ResponseStatus ResponseStatus { get; set; }
}
In JobService.cs
public class JobService : ServiceStack.ServiceInterface.Service
{
public JobRepository Repository { get; set; }
public object Any(JobRequest request)
{
return new JobRequestResponse() { Message = "ok" };
}
}
In Javascript.js // IT WORKS
$.ajax({
type: 'POST',
url: "/api/job",
data: '{ "jobRequestEnum": "test", "jobData": "test" }',
dataType: "json",
contentType: "application/json",
success: function (res) {
debugger;
}
});
In CallJob.js // IT DOES NOT WORK
string serviceUrl = ConfigurationManager.AppSettings["serviceUrl"];
using (JsonServiceClient client = new JsonServiceClient(serviceUrl))
{
var request = new JobRequest() { JobData = "test", JobRequestEnum = "test" };
var response = client.Send<JobRequestResponse>(request);
}
If you add the
[Route]on your Request DTO like:The new IReturn C# Client APIs
Then the C# Client will be able to use the custom
/jobroute on the client, e.g:Whenever you don’t have the Route defined on the Request DTO or specify the Response type, e.g:
You’re not using the
IReturn<T>C# client API so it will send a POST to the pre-defined route which for this would be:Not
/jobsas you’re assuming (i.e. the C# client has no way to know of the route info, so it falls back to using the pre-defined routes).Manually specifying to use the /customroute
Otherwise if you want to keep your Route definitions in AppHost you can force it to use the pre-defined url by supplying it in the C# call, e.g:
No need for ‘Response’ suffix
Also in the new API (and when you use the
IReturn<T>marker) you no longer need the Request DTO + ‘Response’ naming convention and are free to choose any name you need, so it doesn’t have to beJobRequestResponsewhich sounds a little awkward 🙂