F # Discriminative unions are compiled as abstract classes, each of which is a derived nested class. With C #, you can access cases by trying to compress the result from GetAuthBehaviour :
public HttpResponseMessage Post() { var result = Authentication.GetAuthBehaviour(); var valid = result as Types.AuthenticationResponse.Valid; if (valid != null) { int statusCode = valid.Item1; Types.ValidResponse body = valid.Item2; return this.CreateResponse(statusCode, body); } var error = result as Types.AuthenticationResponse.Error; if (error != null) { int statusCode = error.Item1; Types.ErrorResponse body = error.Item2; return this.CreateResponse(statusCode, body); } throw new InvalidOperationException("..."); }
Note that the C # compiler does not know that you handled all cases, so you need to provide a branch that handles the case when the result is neither Valid nor Error . Here, I just selected an exception as an example, but in the web API it would probably be more appropriate to return a 500 status code.
All that said, however, why are there even problems with writing and maintaining controllers in C #? You can write ASP.NET web APIs only in F # .
source share