I am trying to wire up a webhook from a 3rd party system.
When creating the subscription it hits the URL i provide and requires a validated token returned to create the hook.
When the event is triggered the hook posts to the same URL i provided with data in the body.
How can I get a Core 2.1 MVC controller/routing to see these as either two different methods on the controller or a method signature where the complex object is optional?
Either two POST methods (this creates ambiguity exception)
public async Task<IActionResult> Index(){}
public async Task<IActionResult> Index([FromBody] ComplexObject co){}
or complexObject is optional (if not it throws a Executing ObjectResult, writing value of type '"Microsoft.AspNetCore.Mvc.SerializableError" on the subscription creation step.)
public async Task<IActionResult> Index([FromBody] ComplexObject co){}
Another way around this :
public class AllowBindToNullAttribute : ModelBinderAttribute
{
public AllowBindToNullAttribute()
: base(typeof(AllowBindToNullBinder))
{
}
public class AllowBindToNullBinder : IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
var stream = bindingContext.HttpContext.Request.Body;
string body;
using (var reader = new StreamReader(stream))
{
body = await reader.ReadToEndAsync();
}
var instance = JsonConvert.DeserializeObject(body, bindingContext.ModelType);
bindingContext.Result = ModelBindingResult.Success(instance);
}
}
}
You'd use it like this:
public async Task<IActionResult> Index(
[FromBody] [AllowBindToNull] ComplexObject co = null){}
I used the empty parameter method signature and checked the body for data. Not ideal.
Related
I have a stateless service in Azure Service Fabric, and I'm using Microsoft.Extensions.DependencyInjection, although the same issue exists for any other DI frameworks. In my Program.cs, I create a ServiceCollection, add all (but one) of my registrations, create the service provider, and pass it to my service's constructor. Any service method with external entry will create a new service scope and call the main business logic class. The issue is that one of the classes I want to have scoped lifetime needs a value that is an input parameter on the request itself. Here's a code snippet of what I would like to achieve.
internal sealed class MyService : StatelessService, IMyService
{
private IServiceProvider _serviceProvider;
private IServiceScopeFactory _scopeFactory;
public MyService(StatelessServiceContext context, IServiceProvider serviceProvider)
: base(context)
{
_serviceProvider = serviceProvider;
_scopeFactory = _serviceProvider.GetRequiredService<IServiceScopeFactory>();
}
public async Task<MyResponse> ProcessAsync(MyRequest request, string correlationId, CancellationToken cancellationToken)
{
using (var scope = _scopeFactory.CreateScope())
{
var requestContext = new RequestContext(correlationId);
//IServiceCollection serviceCollection = ??;
//serviceCollection.AddScoped<RequestContext>(di => requestContext);
var businessLogic = scope.ServiceProvider.GetRequiredService<BusinessLogic>();
return await businessLogic.ProcessAsync(request, cancellationToken);
}
}
}
The cancellation token is already passed around everywhere, including to classes that don't use it directly, just so it can be passed to dependencies that do use it, and I want to avoid doing the same with the request context.
The same issue exists in my MVC APIs. I can create middle-ware which will extract the correlation id from the HTTP headers, so the API controller doesn't need to deal with it like my service fabric service does. One way I can make it work is by giving RequestContext a default constructor, and have a mutable correlation id. However, it's absolutely critical that the correlation id doesn't get changed during a request, so I'd really like the safety of having get-only property on the context class.
My best idea at the moment is to have a scoped RequestContextFactory which has a SetCorrelationId method, and the RequestContext registration simply calls the factory to get an instance. The factory can throw an exception if a new instance is requested before the id is set, to ensure no id-less contexts are created, but it doesn't feel like a good solution.
How can I cleanly register read-only objects with a dependency injection framework, where the value depends on the incoming request?
I only had the idea for a RequestContextFactory as I was writing the original question, and I finally made time to test the idea out. It actually was less code than I expected, and worked well, so this will be my go-to solution now. But, the name factory is wrong. I'm not sure what to call it though.
First, define the context and factory classes. I even added some validation checks into the factory to ensure it worked the way I expect:
public class RequestContext
{
public RequestContext(string correlationId)
{
CorrelationId = correlationId;
}
public string CorrelationId { get; }
}
public class RequestContextFactory
{
private RequestContext _requestContext;
private bool _used = false;
public void SetContext(RequestContext requestContext)
{
if (_requestContext != null || requestContext == null)
{
throw new InvalidOperationException();
}
_requestContext = requestContext;
}
public RequestContext GetContext()
{
if (_used || _requestContext == null)
{
throw new InvalidOperationException();
}
_used = true;
return _requestContext;
}
}
Then, add registrations to your DI container:
services.AddScoped<RequestContextFactory>();
services.AddScoped<RequestContext>(di => di.GetRequiredService<RequestContextFactory>().GetContext());
Finally, the Service Fabric service method looks something like this
public async Task<MyResponse> ProcessAsync(MyRequest request, string correlationId, CancellationToken cancellationToken)
{
using (var scope = _scopeFactory.CreateScope())
{
var requestContext = new RequestContext(correlationId);
var requestContextFactory = scope.ServiceProvider.GetRequiredService<RequestContextFactory>();
requestContextFactory.SetContext(requestContext);
var businessLogic = scope.ServiceProvider.GetRequiredService<BusinessLogic>();
return await businessLogic.ProcessAsync(request, cancellationToken);
}
}
Kestrel middleware could look something like this
public async Task Invoke(HttpContext httpContext)
{
RequestContext requestContext = new RequestContext(Guid.NewGuid().ToString());
var factory = httpContext.RequestServices.GetRequiredService<RequestContextFactory>();
factory.SetContext(requestContext);
httpContext.Response.Headers["X-CorrelationId"] = requestContext.CorrelationId;
await _next(httpContext);
}
Then just do the normal thing and add a RequestContext parameter to the constructor of any class that needs to get the correlation id (or any other info you put in the request context)
I am working on a sample app to query an api and I am trying to move the generic piece into QueryAsync which queries and returns Task. This function will be called from a controller action GetData which can returns Task too. Here, I am awaiting while retrieving the result from QueryAsync. I feel it is not required as QueryAsync is not returning an awaitable object. But then if I don't, i will not be able to return ActionResult from GetData controller. Is my thinking and implementation here correct? Sorry, but I am new to using Async with MVC.
private async Task<ActionResult> QueryAsync<T>(string url)
{
Task<string> response_task = HttpClientService.HttpClientService.GetRequest(url);
IEnumerable<T> model = JsonConvert.DeserializeObject<IEnumerable<T>>(await response_task);
return View(model);
}
//
// GET:
public async Task<ActionResult> GetData()
{
string _address = "https://someurl";
return await QueryAsync<ClassType>(_address);
}
You are returning an awaitable Task from QueryAsync, which is a Task that "asynchronously" runs GetRequest, then synchronously deserializes the result and returns an ActionResult. Think of those operations as a whole, squeezed into an awaitable Task.
From async/await API perspective, your implementation is correct. Your implementation is equivalent to the following (ignoring generic method):
public async Task<ActionResult> GetData()
{
var url = "https://someurl";
var response = await HttpClientService.HttpClientService.GetRequest(url);
var model = JsonConvert.DeserializeObject<IEnumerable<ClassType>>(response);
return View(model);
}
I am using MVC3, .NET4, C#.
I need to create some XHTML using a Razor View. I do this via an Action.
public ActionResult RenderDoc(int ReportId)
{
//A new document is created.
return View();
}
I then need to take the output from this and convert it to a Word Doc. I am using a 3rd party component to do this and it expects a "stream" or a "file" for the XHTML source that is read in for conversion to a DOC, like the following:
document.Open(MyXhtmlStream,FormatType.Html,XHTMLValidationType.Transitional);
My Question:
What would be a good way to call the "RenderDoc" Action and obtain the result as a stream to feed into "MyXhtmlStream".
Many thanks.
EDIT: I have had another idea !!!
1) Render the View within the action to create a String(XHTMLString). I have seen a method to do this on SO.
2) Create a MemoryStream and put this string into it.
Stream MyStream = New MemoryStream("XHTMLString and encoding method");
EDIT2: Based on Darin's answer
I need to clasyify a little further, and I hope to do this via tweaking Darin's code for my purpose.
public class XmlDocumentResult : ActionResult
{
private readonly string strXhtmlDocument;
public XmlDocumentResult(string strXhtmlDocument)
{
this.strXhtmlDocument = strXhtmlDocument;
}
public override void ExecuteResult(ControllerContext context)
{
WordDocument myWordDocument = new WordDocument();
var response = context.HttpContext.Response;
response.ContentType = "text/xml";
myWordDocument.Open(response.OutputStream, FormatType.Html, XHTMLValidationType.Transitional);
}
}
The above is closer to what I need. Note the 3rd Party WordDocument type. So there is still the issue of how I get the "strXhtmlDocument" into the "Response.OutputStream?
I would just write a custom ActionResult to handle that:
public class XmlDocumentResult : ActionResult
{
private readonly Document document;
public XmlDocumentResult(Document document)
{
this.document = document;
}
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.ContentType = "text/xml";
document.Open(response.OutputStream, FormatType.Html, XHTMLValidationType.Transitional);
}
}
You could of course adjust the response Content-Type if necessary and also append a Content-Disposition header if you want.
And then simply have my controller action return this custom action result:
public ActionResult RenderDoc(int reportId)
{
Document document = repository.GetDocument(reportId);
return new XmlDocumentResult(document);
}
Now the controller action doesn't need to handle plumbing code anymore. The controller action does what a typical controller action is supposed to do:
Query the Model
Pass this model to an ActionResult
In your case the model is this Document class or whatever it is called.
I would like to throw an exception when an ASP.NET WebAPI function returns JSON where the value is an IEnumerable and the HTTP request method is GET - hopefully to stop any JSON being generated where the top level is an array.
I've tried to do this by creating a MediaTypeFormatter. Am I able to do this? Is there another way I can go about doing this? Thanks.
Something like:
public class CustomFormatter : MediaTypeFormatter
{
public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext transportContext)
{
// Retrieve value for isGetRequest somehow...
if (value is IEnumerable && isGetRequest)
{
throw new InvalidOperationException();
}
...
}
}
It is possible as GetPerRequestFormatterInstance method has been added and can be overriden:
public class CustomFormatter : MediaTypeFormatter
{
private HttpRequestMessage _request;
private CustomFormatter(HttpRequestMessage request)
{
_request = request;
}
public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, MediaTypeHeaderValue mediaType)
{
return new CustomFormatter(request);
}
..........
So if you do that, then at the time of WriteToStreamAsync, request will have a value.
I'm trying to unit test a custom action result. I recently watched Jimmy Bogard's excellent MvcConf video ("put your controllers on a diet") http://www.viddler.com/explore/mvcconf/videos/1/ and have started to try and implement some custom action results. I've managed that without a problem, the ActionResult works fine at runtime but I'm having trouble trying to unit test them.
Unfortunately in the code download there are no unit tests for Jimmy's custom action methods... which make me wonder.
I realise that action methods just return instances of the ActionResult types and its the MVC framework that actually calls the ExecuteResult method, which of course is not available when running the unit test. So my unit test is now just creating an instance of my custom ActionResult and I then call ExecuteResult.
Unfortunatley in the ExecuteResult method of my custom ActionResult it is also calling the ExecuteResult method of a ViewResult that I passed it. At that point it blows up. How should I be mocking/stubbing these things to get my unit test working?
public class SendToAFriendActionResult : ActionResult
{
public const string INVALID_CAPTCHA = "You don't appear to have filled out the two words from the security image correctly to prove you're a human. Please try again.";
public const string INVALID_MODEL_STATE = "You don't appear to have filled out all the details correctly. Please try again.";
public const string CONTACT_FAIL = "Unfortunately we experiend a problem sending the link. Please try again later.";
public const string SEND_TO_A_FRIEND_FAIL_KEY = "ContactFail";
private RedirectResult _success;
private ViewResult _failure;
private readonly SendToAFriendModel _model;
private readonly bool _captchaValid;
private readonly MessageBuilderServiceBase _mbs;
public RedirectResult Success
{
get { return _success; }
set { _success = value; }
}
public ViewResult Failure
{
get { return _failure; }
set { _failure = value; }
}
public SendToAFriendActionResult(RedirectResult success, ViewResult failure, SendToAFriendModel model, bool captchaValid, MessageBuilderServiceBase mbs)
{
_success = success;
_failure = failure;
_model = model;
_captchaValid = captchaValid;
_mbs = mbs;
}
public override void ExecuteResult(ControllerContext context)
{
if (!_captchaValid)
{
Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = INVALID_CAPTCHA;
// On reaching this point I receive the error
// Object reference not set to an instance of an object
// as the MVC framework calls FindView
Failure.ExecuteResult(context);
return;
}
if (!context.Controller.ViewData.ModelState.IsValid)
{
Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = INVALID_MODEL_STATE;
Failure.ExecuteResult(context);
return;
}
_mbs.RecipientEmailAddress = _model.EmailRecipient;
_mbs.SendersName = _model.SendersName;
_mbs.Url = _model.URL;
var result = _mbs.sendMessage();
if (!result)
{
Failure.TempData[SEND_TO_A_FRIEND_FAIL_KEY] = CONTACT_FAIL;
Failure.ExecuteResult(context);
return;
}
Success.ExecuteResult(context);
}
}
Here's the start of my unit test ...
IMessageService _emailMessageSerivce;
IGalleryRepository _repository;
var stfModel = new SendToAFriendModel
{
SendersName = "Someone",
URL = "http://someurl.com",
EmailRecipient = "a-friend#somewherelse.com"
};
var failure = new ViewResult() {ViewName ="SendToFriend"};
const bool captchaValid = false;
var fakeControlllerContext = MockRepository.GenerateStub<ControllerContext>(null);
var stf = new SendToAFriendActionResult(null, failure, stfModel, captchaValid, null);
stf.ExecuteResult(fakeControlllerContext);
I've put comments in the SUT to show were the problem occurs.
I know I should be stubbing/mocking somehow but I just can't seem to resolve this.
From ASP.NET MVC 2 In Action (coauthored by Jimmy Bogard):
By taking that hard-to-test code out
of an action and putting it into the
Execute method of an action result,
you ensure that the actions become
significantly easier to unit-test.
That’s because when you unit-test an
action, you assert the type of action
result that the action returns and the
state of the action result. The
Execute method of the action result
isn’t executed as part of the unit
test.
Unit tests are designed to isolate behavior and concerns. You're mixing concerns by calling ExecuteResult from within your custom Action. Instead, I would have the SendToAFriendActionResult return the actual ActionResult (Failure or Success):
public ActionResult GetAction(..)
{
ActionResult result;
//logic here to determine which ActionResult to return
return result;
}
In your Controller:
public ViewResult SendToAFriend()
{
return SendToAFriendActionResult(null, failure, stfModel, captchaValid, null)
.GetAction();
}
This method will allow the MVC framework to do its job and isolates those concerns outside your custom ActionResult. Your test should assert that the correct type of Action, failure or success, is returned based on the parameters you set going in.