Working with Service Fabric stateless WebAPI services
Most of the time you can use most of your generated templates and just define your custom controller class and the methods defined will work as expected.
Note: Also define the objects that are used for the request and the response. I usually put these in a separate project called <ServiceName>.Models.
But if you want to ensure that your response does exactly what you want. (ie return a different HTTP status code), you can overload with the following approach.
Here is a standard controller class:
namespaceHealthCheck.Controllers{ [ServiceRequestActionFilter]publicclassHealthCheckController:ApiController { // GET api/HealthCheck publicHealthCheckGetResponseGet() {ServiceEventSource.Current.Message("Health Check GET called");returnnewHealthCheckGetResponse() {Result ="Success"}; } // POST api/HealthCheck publicHealthCheckPostResponsePost([FromBody]HealthCheckPost value) {ServiceEventSource.Current.Message("Health Check POST called with Mode of: {0}", value?.Mode); // TODO perform an action to store the Value of Mode somewhere.returnnewHealthCheckPostResponse() { Result ="Success" }; }publicHttpResponseMessagePost([FromBody]HealthCheckPost value) {ServiceEventSource.Current.Message("Health Check POST called with Mode of: {0}", value?.Mode); // TODO perform an action to store the Value of Mode somewhere.var resp =newHealthCheckPostResponse() { Result ="Success" };returnRequest.CreateResponse(HttpStatusCode.OK, resp,Configuration.Formatters.XmlFormatter); } }}
If you wanted the response to be XML (instead of the default JSON and the HTTP Status code to be something different you could re-write the post method from:
// POST api/HealthCheck publicHealthCheckPostResponsePost([FromBody]HealthCheckPost value){ServiceEventSource.Current.Message("Health Check POST called with Mode of: {0}", value?.Mode); // TODO perform an action to store the Value of Mode somewhere.returnnewHealthCheckPostResponse() { Result ="Success" };}
to
publicHttpResponseMessagePost([FromBody]HealthCheckPost value){ServiceEventSource.Current.Message("Health Check POST called with Mode of: {0}", value?.Mode); // TODO perform an action to store the Value of Mode somewhere.var resp =newHealthCheckPostResponse() { Result ="Success" };returnRequest.CreateResponse(HttpStatusCode.OK, resp,Configuration.Formatters.XmlFormatter);}