We have many services in our system. ( integrating with a mobile company)
So, (for example) we have :
Action1 in Controller1
Action2 in Controller1
...
Action4 in Controller4
Action5 in Controller4
...
Currently, the mobile company calls each action with a single request.
But recently they told us , "can we send you a list of Actions to invoke ? instead of running single action manually each time... ?"
So I tried reflection:
ServicesController :
[HttpGet]
[AllowAnonymous]
public HttpResponseMessage AAA( )
{
Type type = typeof(UsersController);
var instance = Activator.CreateInstance(type);
MethodInfo method = type.GetMethod("Test2", BindingFlags.Instance | BindingFlags.Public);
var t= method.Invoke(instance, new object[] { "royi" });
return Request.CreateResponse(HttpStatusCode.OK, t);
}
And :
UseresController :
[HttpGet]
[AllowAnonymous]
public HttpResponseMessage Test2( string ggg)
{
return Request.CreateResponse(HttpStatusCode.OK, "hello"+ggg);
}
When I run via fiddler :
http://es.com/api/services/aaa ( GET)
It does work , but (obviously) the Request on the other side is null :
Question
How can I make Test2 run as expected ? am I on the right direction of solving this ? or does webApi has built in mechanism for this sort of thing ?
You better use the ActionInvoker to do that:
public HttpResponseMessage AAA()
{
var ctrlDesc = new HttpControllerDescriptor(this.Configuration, "UsersController", typeof(UsersController));
var actionDesc = new ReflectedHttpActionDescriptor(ctrlDesc, typeof(UsersController).GetMethod("Test2"));
var ctrlCtx = new HttpControllerContext(this.Configuration, this.Request.GetRouteData(), this.Request);
var apiCtrl = ctrlDesc.CreateController(this.Request) as ApiController;
apiCtrl.Request = this.Request;
apiCtrl.Configuration = this.Configuration;
apiCtrl.ControllerContext = ctrlCtx;
ctrlCtx.Controller = apiCtrl;
ctrlCtx.ControllerDescriptor = ctrlDesc;
ctrlCtx.Request = this.Request;
ctrlCtx.RouteData = this.Request.GetRouteData();
var actionContext = new HttpActionContext(ctrlCtx, actionDesc);
actionContext.ActionArguments.Add("ggg", "royi");
var invoker = this.Configuration.Services.GetActionInvoker();
return invoker.InvokeActionAsync(actionContext, CancellationToken.None).Result;
}
Related
How do I get Odata working with a List variable?
The following API is not working and giving error.
HttpGet
Error: {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.13","title":"Unsupported Media Type","status":415,"traceId":"00-b0b48a41f445ac4bbcebba902f8cca95-a714db4f4fd0c146-00"}
All my other APIs work,
"http://localhost:4547/api/properties/GetIdentifierPaged"
Controller:
[HttpGet[("[Action]")]
public ActionResult GetIdentifierPaged(List<string> propertyListRequest, ODataQueryOptions<PropertyDto> queryOptions)
{
propertyListRequest.Add("1110200100"); // fake data
var model = _propertyService.GetByPropertyIdentifierPaged(propertyListRequest).ToODataPageResult(queryOptions);
return Ok(model);
}
Service:
public IQueryable<PropertyDto> GetByPropertyIdentifierPaged(List<string> identifiers)
{
var identifiersEnumerable = identifiers.AsEnumerable();
var properties = _dataContext.Property
.Include(x => x.PropertyStatus)
Where(x => identifiersEnumerable.Contains(x.PropertyIdentifier))
return properties;
}
Page Result:
public static PageResult<T> ToODataPageResult<T>(this IQueryable<T> query, ODataQueryOptions<T> queryOptions)
{
var settings = new ODataQuerySettings();
settings.EnsureStableOrdering = false;
var countQuery = (IQueryable<T>)queryOptions.ApplyTo(query, settings, AllowedQueryOptions.Skip | AllowedQueryOptions.Top | AllowedQueryOptions.Select | AllowedQueryOptions.OrderBy);
long count = countQuery.Count();
var results = (IQueryable<T>)queryOptions.ApplyTo(query, settings, AllowedQueryOptions.None);
return new PageResult<T>(results, null, count);
}
This problem indicates that there is a problem with your request, but even if you came to respond, there are errors you will receive. You cannot give a value that session is open in response
return new PageResult<T>(**results.ToList()**, null, count);
I have written this controller method and this test.
Controller method:
public async Task<IActionResult> Metric(string type, string source)
{
// Check existence ...
var model = await _context
.Metrics
.FirstAsync(mt => mt.Type == metricType.AsInt() && mt.Source == source);
Response.StatusCode = HttpStatusCode.OK.AsInt();
return View(model);
}
Test:
[Fact]
public async Task MetricExistsTest()
{
// Arrange ...
// Act
var result = await _controller.Metric(Metrics.CpuLoad.ToString(), "source-1");
// Assert
var viewResult = Assert.IsType<ViewResult>(result);
Assert.Equal(HttpStatusCode.OK.AsInt(), viewResult.StatusCode.Value);
var model = Assert.IsAssignableFrom<Metric>(
viewResult.ViewData.Model
);
}
Now, the problem is here Assert.Equal(HttpStatusCode.OK.AsInt(), viewResult.StatusCode.Value);. The viewResult.StatusCode is indeed null. If I comment that line out, everything works.
What am I doing wrong? Why is it null? Do I properly set Response.StatusCode? How do I verify status code then?
Thank you!
I finally did it!
All those who helped me with answers and comments - I very much appreciate it!
It turns out that I had two problems - HttpContext does not exist (unless set manually) in testing environment and Response.StatusCode does not set StatusCode on resulting ViewResult object. (These are my observations, correct me if I'm wrong).
Problem 1 solution:
As simple as setting default HttpContext solves the problem. At least, controller method does not crash because Response is not null anymore.
var controller = new HomeController();
controller.ControllerContext = new ControllerContext();
controller.ControllerContext.HttpContext = new DefaultHttpContext();
Problem 2 solution:
It turns out that I need to set StatusCode explicitly on ViewResult object. For some reason, ASP.Core does not mirror StatusCode from Response object to resulting IActionObject. (Correct me if I'm wrong)
So here is the solution (it's another method on my controller, but it clearly demonstrates the idea):
public async Task<IActionResult> Index()
{
var model = await _context
.Metrics
.Where(mt => mt.Type == Metrics.CpuLoad.AsInt())
.ToListAsync();
var result = View(model);
result.StatusCode = (model.Any() ? HttpStatusCode.OK : HttpStatusCode.NoContent).AsInt();
return result;
}
It looks like you should be Asserting the result, not the viewresult
[Fact]
public async Task MetricExistsTest()
{
// Arrange ...
// Act
var result = await _controller.Metric(Metrics.CpuLoad.ToString(), "source-1");
// Assert
var viewResult = Assert.IsType<ViewResult>(result);
Assert.Equal(HttpStatusCode.OK.AsInt(), result.StatusCode.Value);
var model = Assert.IsAssignableFrom<Metric>(
viewResult.ViewData.Model
);
}
There is one application, the API with access to the database and one application that calls the API with RestSharp.
I implemented all async methods of RestSharp to work generic. So GET, POST, DELETE are all working.The only one I can't get to work is the PUT.
First of all this is my controllers PUT:
[HttpPut("{id}")]
public void Put(int id, [FromBody]ApplicationUser value)
{
string p = value.Email;
}
this is my method:
public Task<bool> PutRequestContentAsync<T>(string resource, object id, T resourceObject) where T : new()
{
RestClient client = new RestClient("http://localhost:54008/api/");
RestRequest request = new RestRequest($"{resource}/{{id}}", Method.PUT);
request.AddUrlSegment("id", id);
request.AddObject(resourceObject);
var tcs = new TaskCompletionSource<bool>();
var asyncHandler = client.ExecuteAsync<T>(request, r =>
{
tcs.SetResult(r.ResponseStatus == ResponseStatus.Completed);
});
return tcs.Task;
}
and this is my call in a view (all other calls of GET,... are working fine):
bool putOk = await new RepositoryCall()
.PutRequestContentAsync("Values", 2,
new ApplicationUser {
Email="test#xxxxxxx.de"
}
);
with debugging, the response-status is Completed but the PUT is never called.
Any idea what the problem could be?
So finally I got my answer myself... (sit yesterday 6 hours and no result, today one more hour and it works)
public Task<bool> PutRequestContentAsync<T>(string resource, object id, T resourceObject) where T : new()
{
RestClient client = new RestClient("http://localhost:54008/api/");
RestRequest request = new RestRequest($"{resource}/{{id}}", Method.PUT);
request.AddUrlSegment("id", id);
request.RequestFormat = DataFormat.Json;
request.AddBody(resourceObject);
var tcs = new TaskCompletionSource<bool>();
var asyncHandler = client.ExecuteAsync<T>(request, (response) => {
tcs.SetResult(response.ResponseStatus == ResponseStatus.Completed);
});
return tcs.Task;
}
the trick was to add a RequestFormat and changing AddObject to AddBody :)
I need to get the controller name from my route and this I can do if using standard routing code in WebApiConfig.
However, if I am using routing attributes it starts to get a little difficult, especially when trying to version.
Example: If I call an api/terms/bonuses and I have a BonusController and BonusV2Controller and a BonusV3Controller, this code returns the latest controller version 3. That's ok, I can live with that returning the latest and greatest version as a default.
var attributedRoutesData = request.GetRouteData().GetSubRoutes();
var subRouteData = attributedRoutesData.FirstOrDefault();
var actions = (ReflectedHttpActionDescriptor[])subRouteData.Route.DataTokens["actions"];
//This seems to get latest controller name. ie. V2
controllerName = actions[0].ControllerDescriptor.ControllerName;
Now if I request a version 1, for simplicity I'll use a querystring and call api/terms/bonuses?v=2
So this code no longer works (obviously).
How do I get the V2 controller name?
If I abandon routing attributes and just use WebApiConfig routing, this code works happily.
HttpControllerDescriptor controllerDescriptor = null;
var controllers = GetControllerMapping();
var routeData = request.GetRouteData();
var controllerName = (string)routeData.Values["controller"];
UPDATE:
Here is my full selector code.
IDictionary<string, HttpControllerDescriptor> controllers = GetControllerMapping();
var attributedRoutesData = request.GetRouteData().GetSubRoutes();
var subRouteData = attributedRoutesData.LastOrDefault(); //LastOrDefault() will get PeopleController, FirstOrDefault will get People{version}Controller which we don't want
var actions = (ReflectedHttpActionDescriptor[])subRouteData.Route.DataTokens["actions"];
var controllerName = actions[0].ControllerDescriptor.ControllerName;
//For controller name without attribute routing
//var controllerName = (string)routeData.Values["controller"];
HttpControllerDescriptor oldControllerDescriptor;
if (controllers.TryGetValue(controllerName, out oldControllerDescriptor))
{
//TODO: Different techniques for handling version api requests.
var apiVersion = GetVersionFromQueryString(request);
//var version = GetVersionFromHeader(request);
//var version = GetVersionFromAcceptHeaderVersion(request);
//var version = GetVersionFromMediaType(request);
if (!String.IsNullOrEmpty(apiVersion))
{
var newControllerName = String.Concat(controllerName, "V", apiVersion);
HttpControllerDescriptor newControllerDescriptor;
if (controllers.TryGetValue(newControllerName, out newControllerDescriptor))
{
return newControllerDescriptor;
}
}
return oldControllerDescriptor;
}
return null;
var subRouteData = request.GetRouteData().GetSubRoutes().LastOrDefault();
if (subRouteData != null && subRouteData.Route != null)
{
var actions = subRouteData.Route.DataTokens["actions"] as HttpActionDescriptor[];
if (actions != null && actions.Length > 0)
{
controllerName = actions[0].ControllerDescriptor.ControllerName;
}
}
At last I found it:
filterContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName
How can i call a Post method with multiple parameters using HttpClient?
I am using the following code with a single parameter:
var paymentServicePostClient = new HttpClient();
paymentServicePostClient.BaseAddress =
new Uri(ConfigurationManager.AppSettings["PaymentServiceUri"]);
PaymentReceipt payData = SetPostParameter(card);
var paymentServiceResponse =
paymentServicePostClient.PostAsJsonAsync("api/billpayment/", payData).Result;
I need to add another parameter userid. How can i send the parameter along with the 'postData'?
WebApi POST method prototype:
public int Post(PaymentReceipt paymentReceipt,string userid)
Simply use a view model on your Web Api controller that contains both properties. So instead of:
public HttpresponseMessage Post(PaymentReceipt model, int userid)
{
...
}
use:
public HttpresponseMessage Post(PaymentReceiptViewModel model)
{
...
}
where the PaymentReceiptViewModel will obviously contain the userid property. Then you will be able to call the method normally:
var model = new PaymentReceiptViewModel()
model.PayData = ...
model.UserId = ...
var paymentServiceResponse = paymentServicePostClient
.PostAsJsonAsync("api/billpayment/", model)
.Result;
UserId should be in query string:
var paymentServiceResponse = paymentServicePostClient
.PostAsJsonAsync("api/billpayment?userId=" + userId.ToString(), payData)
.Result;
In my case my existing ViewModels don't line up very nicely with the data I want to post to my WebAPI. So, instead of creating an entire new set of model classes, I posted an anonymous type, and had my Controller accept a dynamic.
var paymentServiceResponse = paymentServicePostClient.PostAsJsonAsync("api/billpayment/", new { payData, userid }).Result;
public int Post([FromBody]dynamic model)
{
PaymentReceipt paymentReceipt = (PaymentReceipt)model.paymentReceipt;
string userid = (string)model.userid;
...
}
(I'd be curious to hear some feedback on this approach. It's definitely a lot less code.)