Async ctp MVC4 and workflows - asp.net-mvc

I have the below code which calls a workflow. I am wondering if I can changed the controller to be asynchronous using the async ctp.
public ActionResult Index()
{
var input = new Dictionary<string, object>();
input["ViewData"] = this.ViewData;
var userState = "BeginInvoke example";
var invoker = new WorkflowInvoker(HelloMvcDefinition);
Task workflowTask = Task.Factory.FromAsync<IDictionary<string, object>>(
invoker.BeginInvoke(input, WorkflowCompletedCallback, userState),
invoker.EndInvoke);
workflowTask.Wait();
return View();
}
I have tried this but I cannot seem to get it to work:
public async Task<ActionResult> Index()
{
var input = new Dictionary<string, object>();
input["ViewData"] = this.ViewData;
var userState = "BeginInvoke example";
var invoker = new WorkflowInvoker(HelloMvcDefinition);
Task workflowTask = Task.Factory.FromAsync<IDictionary<string, object>>(
invoker.BeginInvoke(input, WorkflowCompletedCallback, userState),
invoker.EndInvoke);
await workflowTask;
return View();
}
Unfortunately the view does not seem to work. Any ideas on what I am doing wrong?
EDIT
After taking advice I have changed the method to this
public class HelloController : AsyncController
{
private static readonly HelloWorkflow HelloMvcDefinition = new HelloWorkflow();
public Task<ViewResult> Index()
{
var input = new Dictionary<string, object>();
input["ViewData"] = ViewData;
const string userState = "BeginInvoke example";
var invoker = new WorkflowInvoker(HelloMvcDefinition);
return Task.Factory.FromAsync<IDictionary<string, object>>(
invoker.BeginInvoke(input, WorkflowCompletedCallback, userState),
invoker.EndInvoke).ContinueWith(t => View());
}
static void WorkflowCompletedCallback(IAsyncResult result)
{
}
}
Which works fine so the problem must be how I am using the async keyword.
Thanks

We can use the TAP pattern for invoking windows workflow as follows -
(Details are mentioned in my blog post http://tweetycodingxp.blogspot.com/2013/06/invoke-workflow-wf-using-task-based.html)
public async Task<ActionResult> Index(string id)
{
var wfInputArgs = new Dictionary<string, object>
{
...
};
var wfOutputArgs = await Task<IDictionary<string, object>>.Factory.StartNew(
() => WorkflowInvoker.Invoke(new MyWorkflow(), wfInputArgs));
var result = wfOutputArgs["Output1"] as IEnumerable<Class1>;
...
return View(model);
}

Derive from AsyncController instead of Controller.
EDIT: You may also be experiencing a known bug where ASP.NET MVC4 will hang if the action returns a completed Task. You can work around this bug by adding await Task.Yield(); at the top of the action method.
On an unrelated note, this code is more efficient (and shorter, too):
var workflowTask = Task.Factory.FromAsync(invoker.BeginInvoke, invoker.EndInvoke,
input, userState);

Related

Azure Mobile Service ListAsync hangs forever

How can I make a synchronous call to a Mobile Service Table from MVC?
When I run the following code, My application keeps hanging forever waiting the result:
public class HomeController : Controller
{
MobileServiceClient mobileClient = new MobileServiceClient("My-Mobile-Server", "My-Key");
public ActionResult Index()
{
var clientTable = mobileClient.GetTable<Clients>();
var client = clientTable.ToListAsync().Result;
if (client.Count == 0)
ViewBag.TemCliente = "Não";
else
ViewBag.Cliente = client.First().Name;
return View();
}
}
Funny, the same code in my Unit Test works perfectly.
How can I list my Mobile Service Table clients Synchronously?
It looks like the problem you're having is because you're not using await with the async methods.
public class HomeController : Controller
{
MobileServiceClient mobileClient = new MobileServiceClient("My-Mobile-Server", "My-Key");
public ActionResult Index()
{
var clientTable = mobileClient.GetTable<Clients>();
var client = await clientTable.ToListAsync().Result;
if (client.Count == 0)
ViewBag.TemCliente = "Não";
else
ViewBag.Cliente = client.First().Name;
return View();
}
}
Be sure to use this doc as a reference on best practices: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-windows-dotnet-how-to-use-client-library/

Unit Testing a RedirectToAction with parameter

I have an MVC Controller Class that I am trying to Unit Test.
The particular ActionResult is like this
public ActionResult Create(Shipment newShipment)
{
do some stuff to create a shipmentID
...
return RedirectToAction("AddUnit",newShipment.ShipmentID);
}
I have mocked up the controller context etc and now I want to test that the newShipment.ShipmentID passed to the RedirectToAction call is what I expect.
I have a test (with lots of mocking of things in the setup phase)
[Test]
public void CreateSuccess()
{
//Arrange
var shipment = new Shipment();
shipment.Widgets = 2; //Make sure it a valid shipment otherwise
//Act
var result = controller.Create(shipment) as RedirectToRouteResult;
//Assert
Assert.IsNotNull(result);
Assert.AreEqual("AddUnits", result.RouteValues["action"]);
Assert.IsNull(result.RouteValues["controller"]);
...
And now I want to find an Assert to check that the shipmentID I pass to RedirectToAction is the right one. How do I retrieve its value?
(I believe this code works for real (ie the actual view gets the correct shipmentID) but I want to write a unit test ).
Actually, RedirectToActions can take parameters; below is a simplified example of how I tested for correct parameters in one. I'm using NUnit & FluentAssertions here.
[HttpPost]
public ActionResult RedirectMethod()
{
return RedirectToAction("Index", "Home", new { parameter = "Value" });
}
and then the test:
[Test]
public void RedirectSetsExpectedParameters()
{
var result = controller.RedirectMethod();
var redirectResult = result.As<RedirectToRouteResult>();
var expectedRedirectValues = new RouteValueDictionary
{
{ "parameter", "Value" },
{ "action", "Index" },
{ "controller", "Home" }
};
redirectResult
.RouteValues
.ShouldBeEquivalentTo(expectedRedirectValues,
"The redirect should look as I expect, including the parameters");
}
Here i given one sample for Redirect to action
public ActionResult Signupsuccess(string account_code, string plan)
{
..........
do some stuff
return RedirectToAction("SubscriptionPlan", "Settings");
}
I have mocked up controllercontext and passed some parameter to RedirectToAction call from controller to test method.Finally it tested both the expected and actual
[TestMethod()]
public void SignupsuccessTest()
{
HomeController target = new HomeController(); // TODO: Initialize to an appropriate value
string account_code = "B898YB7"; // TODO: Initialize to an appropriate value
string plan = "Application-BASIC"; // TODO: Initialize to an appropriate value
var ControllerContext = new Mock<ControllerContext>();
var context = target.HttpContext;
ControllerContext.SetupGet(p => p.HttpContext.Session["MerchantID"]).Returns("15");
ControllerContext.SetupGet(p => p.HttpContext.Session["FriendlyIdentifier"]).Returns("3d649876-19f5-48d2-af03-ca89083ae712");
target.ControllerContext = controllerContext.Object;
var action = (RedirectToRouteResult)target.Signupsuccess(account_code, plan);
action.RouteValues["action"].Equals("SubscriptionPlan");
action.RouteValues["controller"].Equals("Settings");
Assert.AreEqual("SubscriptionPlan", action.RouteValues["action"]);
Assert.AreEqual("Settings", action.RouteValues["controller"]);
}
I Hope really it will helpful for you.Thank You

View doesn't refresh after RedirectToAction is done

Here is my problem:
[HttpPost]
public ActionResult AddData(CandidateViewModel viewModel)
{
var newCandidateId = 0;
newCandidateId = this._serviceClient.AddCandidate(viewModel);
return RedirectToAction("DisplayCandidate",new {id=newCandidateId});
}
public ActionResult DisplayCandidate(int id)
{
var candidateViewModel= this._serviceClient.GetCandidate(id);
return View(candidateViewModel);
}
After filling the form viwemodel sends to server. After data were stored, flow is redirected to DisplayCandidate action and it goes there but page didn't refresh. I don't understand why! Help, please.
Because you are using Ajax Post
public ActionResult AddData(CandidateViewModel viewModel)
{
var newCandidateId = 0;
newCandidateId = this._serviceClient.AddCandidate(viewModel);
string ReturnURL = "/DisplayCandidate/"+newCandidateId;
return JSON(ReturnURL);
}
and in your Ajax Post Method:
Onsuccess(function(retURL){ window.location(retURL); })
This will take to the new Action and that Action will return View.
If you're using Ajax, return a script results to execute the navigation
instead of
return RedirectToAction("DisplayCandidate",new {id=newCandidateId});
try
var viewName = "/Path/ViewName";
var id = 1;
var urlNavigate = string.Format("location.href='{0}?id={1}'", viewName, id);
return new JavaScriptResult() { Script = urlNavigate };

Using Moq to test methods that accept non-primative arguments

I'm trying to write a test for an ASP.Net MVC controller action.
I'd like to test that the action invokes a particular method on an injected service, so I'm mocking the service and using .Verify.
So in the simple case, I have the following action in my controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(string title)
{
_cmsService.AddPage(title);
return View("Edit");
}
using the service interface...
public interface ICmsService
{
void AddPage(string request);
}
and the following test...
//Arrange
const string pageTitle = "Test Page";
var cmsService = new Mock<ICmsService>();
cmsService.Setup(service => service.AddPage(pageTitle));
var controller = new PageController(cmsService.Object);
//Act
var result = controller.Create(pageTitle) as ViewResult;
//Assert
cmsService.Verify(service => service.AddPage(pageTitle), Times.Once());
Now I want to refactor my service operation to use request and response objects...
public interface ICmsService
{
CmsServiceAddPageResponse AddPage(CmsServiceAddPageRequest request);
}
So I change my action accordingly...
public ActionResult Create(string title)
{
var request = new CmsServiceAddPageRequest()
{
PageName = title
};
var response = _cmsService.AddPage(request);
return View("Edit");
}
and also my test...
//Arrange
const string pageTitle = "Test Page";
var cmsService = new Mock<ICmsService>();
var request = new CmsServiceAddPageRequest() {PageName = pageTitle};
cmsService.Setup(service => service.AddPage(request));
var controller = new PageController(cmsService.Object);
//Act
var result = controller.Create(pageTitle) as ViewResult;
//Assert
cmsService.Verify(service => service.AddPage(request), Times.Once());
But now when I run the test, I get the following message...
TestCase 'Web.Test.PageControllerTest.CreateNewPagePost'
failed: Moq.MockException :
Invocation was performed more than once on the mock: service => service.AddPage(value(Web.Test.PageControllerTest+<>c__DisplayClass1).request)
at Moq.Mock.ThrowVerifyException(IProxyCall expected, Expression expression, Times times)
at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
at Moq.Mock.Verify[T,TResult](Mock mock, Expression`1 expression, Times times, String failMessage)
at Moq.Mock`1.Verify[TResult](Expression`1 expression, Times times)
PageControllerTest.cs(67,0): at Web.Test.PageControllerTest.CreateNewPagePost()
What should I be doing to test a method that accepts a non-primitive type?
Thanks
Sandy
I think a better alternative to the first answer would be to implement a custom matcher rather than change code to match your testing framework. From:http://code.google.com/p/moq/wiki/QuickStart
// custom matchers
mock.Setup(foo => foo.Submit(IsLarge())).Throws<ArgumentException>();
...
public string IsLarge()
{
return Match<string>.Create(s => !String.IsNullOrEmpty(s) && s.Length > 100);
}
If you override Equals in the CmsServiceAddPageRequest object it should compare them correctly.

How to mock the Request on Controller in ASP.Net MVC?

I have a controller in C# using the ASP.Net MVC framework
public class HomeController:Controller{
public ActionResult Index()
{
if (Request.IsAjaxRequest())
{
//do some ajaxy stuff
}
return View("Index");
}
}
I got some tips on mocking and was hoping to test the code with the following and RhinoMocks
var mocks = new MockRepository();
var mockedhttpContext = mocks.DynamicMock<HttpContextBase>();
var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>();
SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest);
var controller = new HomeController();
controller.ControllerContext = new ControllerContext(mockedhttpContext, new RouteData(), controller);
var result = controller.Index() as ViewResult;
Assert.AreEqual("About", result.ViewName);
However I keep getting this error:
Exception
System.ArgumentNullException:
System.ArgumentNullException : Value
cannot be null. Parameter name:
request at
System.Web.Mvc.AjaxRequestExtensions.IsAjaxRequest(HttpRequestBase
request)
Since the Request object on the controller has no setter. I tried to get this test working properly by using recommended code from an answer below.
This used Moq instead of RhinoMocks, and in using Moq I use the following for the same test:
var request = new Mock<HttpRequestBase>();
// Not working - IsAjaxRequest() is static extension method and cannot be mocked
// request.Setup(x => x.IsAjaxRequest()).Returns(true /* or false */);
// use this
request.SetupGet(x => x.Headers["X-Requested-With"]).Returns("XMLHttpRequest");
var context = new Mock<HttpContextBase>();
context.SetupGet(x => x.Request).Returns(request.Object);
var controller = new HomeController(Repository, LoginInfoProvider);
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
var result = controller.Index() as ViewResult;
Assert.AreEqual("About", result.ViewName);
but get the following error:
Exception System.ArgumentException:
System.ArgumentException : Invalid
setup on a non-overridable member: x
=> x.Headers["X-Requested-With"] at Moq.Mock.ThrowIfCantOverride(Expression
setup, MethodInfo methodInfo)
Again, it seems like I cannot set the request header.
How do I set this value, in RhinoMocks or Moq?
Using Moq:
var request = new Mock<HttpRequestBase>();
// Not working - IsAjaxRequest() is static extension method and cannot be mocked
// request.Setup(x => x.IsAjaxRequest()).Returns(true /* or false */);
// use this
request.SetupGet(x => x.Headers).Returns(
new System.Net.WebHeaderCollection {
{"X-Requested-With", "XMLHttpRequest"}
});
var context = new Mock<HttpContextBase>();
context.SetupGet(x => x.Request).Returns(request.Object);
var controller = new YourController();
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
UPDATED:
Mock Request.Headers["X-Requested-With"] or Request["X-Requested-With"] instead of Request.IsAjaxRequest().
For anyone using NSubstitute I was able to modify the above answers and do something like this... (where Details is the Action method name on the controller)
var fakeRequest = Substitute.For<HttpRequestBase>();
var fakeContext = Substitute.For<HttpContextBase>();
fakeRequest.Headers.Returns(new WebHeaderCollection { {"X-Requested-With", "XMLHttpRequest"}});
fakeContext.Request.Returns(fakeRequest);
controller.ControllerContext = new ControllerContext(fakeContext, new RouteData(), controller);
var model = new EntityTypeMaintenanceModel();
var result = controller.Details(model) as PartialViewResult;
Assert.IsNotNull(result);
Assert.AreEqual("EntityType", result.ViewName);
Here is a working solution using RhinoMocks. I've based it on a Moq solution I found at http://thegrayzone.co.uk/blog/2010/03/mocking-request-isajaxrequest/
public static void MakeAjaxRequest(this Controller controller)
{
MockRepository mocks = new MockRepository();
// Create mocks
var mockedhttpContext = mocks.DynamicMock<HttpContextBase>();
var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>();
// Set headers to pretend it's an Ajax request
SetupResult.For(mockedHttpRequest.Headers)
.Return(new WebHeaderCollection() {
{"X-Requested-With", "XMLHttpRequest"}
});
// Tell the mocked context to return the mocked request
SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest);
mocks.ReplayAll();
// Set controllerContext
controller.ControllerContext = new ControllerContext(mockedhttpContext, new RouteData(), controller);
}
Is AjaxRequest is an extension method. So you can do it the following way using Rhino:
protected HttpContextBase BuildHttpContextStub(bool isAjaxRequest)
{
var httpRequestBase = MockRepository.GenerateStub<HttpRequestBase>();
if (isAjaxRequest)
{
httpRequestBase.Stub(r => r["X-Requested-With"]).Return("XMLHttpRequest");
}
var httpContextBase = MockRepository.GenerateStub<HttpContextBase>();
httpContextBase.Stub(c => c.Request).Return(httpRequestBase);
return httpContextBase;
}
// Build controller
....
controller.ControllerContext = new ControllerContext(BuildHttpContextStub(true), new RouteData(), controller);
Looks like you are looking for this,
var requestMock = new Mock<HttpRequestBase>();
requestMock.SetupGet(rq => rq["Age"]).Returns("2001");
Usage in Controller :
public ActionResult Index()
{
var age = Request["Age"]; //This will return 2001
}
In current .NET (v 5):
var controller = new SomeController(); // SomeController that inherits Microsoft.AspNetCore.Mvc.ControllerBase
var httpContext = new DefaultHttpContext(); // DefaultHttpContext class is part of Microsoft.AspNetCore.Http namespace
httpContext.Request.Headers.Add("origin", "0.0.0.1"); // Add your custom headers to request
controller.ControllerContext.HttpContext = httpContext;
You need to mock HttpContextBase and put it into your ControllerContext property, like that:
controller.ControllerContext =
new ControllerContext(mockedHttpContext, new RouteData(), controller);
To make IsAjaxRequest() to return false during Unit test you need to setup Request Headers as well as request collection value both in your test method as given below:
_request.SetupGet(x => x.Headers).Returns(new System.Net.WebHeaderCollection { { "X-Requested-With", "NotAjaxRequest" } });
_request.SetupGet(x=>x["X-Requested-With"]).Returns("NotAjaxRequest");
The reason for setting up both is hidden in implementation of IsAjaxRequest() which is given below:
public static bool IsAjaxRequest(this HttpRequestBase request)<br/>
{
if (request == null)
{
throw new ArgumentNullException("request");
}
return ((request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest")));
}
It uses both request Collection and header this is why we need to create setup for both Header and Request Collection.
this will make the request to return false when it is not a ajax request. to make it return true you can do the following:
_httpContext.SetupGet(x => x.Request["X-Requested-With"]).Returns("XMLHttpRequest");
I found other way to add a HttpRequestMessage object into your request during Web API as follow
[Test]
public void TestMethod()
{
var controllerContext = new HttpControllerContext();
var request = new HttpRequestMessage();
request.Headers.Add("TestHeader", "TestHeader");
controllerContext.Request = request;
_controller.ControllerContext = controllerContext;
var result = _controller.YourAPIMethod();
//Your assertion
}
(A bit late to the party but I went for a different route so thought I'd share)
To go for a pure code/mocking way of testing this without creating mocks for Http classes I implemented an IControllerHelper which has an Initialise method which takes the Request as a parameter and then exposed properties I want eg:
public interface IControllerHelper
{
void Initialise(HttpRequest request);
string HostAddress { get; }
}
public class ControllerHelper : IControllerHelper
{
private HttpRequest _request;
public void Initialise(HttpRequest request)
{
_request = request;
}
public string HostAddress => _request.GetUri().GetLeftPart(UriPartial.Authority);
}
Then in my controller I call initialise at the start of the method:
_controllerHelper.Initialise(Request);
And then my code is only dependent on mockable dependacies.
return Created(new Uri($"{_controllerHelper.HostName}/api/MyEndpoint/{result.id}"), result);
For functional tests I just override the iControllerHelper in the composition for a substitute.

Resources