ASP.NET Core - call one controller method from another - c#

I need to call from create method delete for cleanup. I want to call DeleteDevice like lazy cleanup and do not care if it will succeed or not.
People suggested to use this way:
DeleteDevice(param).ConfigureAwait(false);
Is it safe?
public async Task<ActionResult<Device>> CreateDevice([FromBody] CreateDeviceRequest request)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
try
{
var registeredDevice = await RegisterDevice(request.DisplayName);
bool isCreatePrinterSucceed = false;
try
{
var updatedDevice = await UpdateDevice(registeredDevice.Id);
isCreatePrinterSucceed = true;
return Ok(new DeviceReference
{
DisplayName = request.DisplayName,
Id = updatedDevice.Id
});
}
finally
{
if (!isCreatePrinterSucceed)
{
var param = new DeleteDeviceRequest()
{
Id = registeredDevice.Id,
AzureUserBearerToken = request.AzureUserBearerToken
};
DeleteDevice(param).ConfigureAwait(false); ;
}
}
}
catch (Exception ex)
{
return StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);
}
}

Related

Entities Framework SaveChanges() Not Working

I have a problem when calling API for update and savechanges() is not working (the data is not update).
However, when I add Thread.Sleep(1000); the data update correctly.
Working Methods
public async Task<ResponseBaseModel> AddOrderRemark2(AddOrderRemarkRequestModel model)
{
try
{
using (ChatEntities context = new ChatEntities(CurrentUsername))
{
List<string> statusList = getPendingStatus(context).Result;
OrderHeader orderHeader = getOrderHerderByOrderCode(context, model.OrderCode, model.SalesChannelId).Result;
if (statusList.Contains(orderHeader.Status))
{
if (orderHeader != null)
{
Thread.Sleep(1000);
orderHeader.Remark = model.Remark;
context.DBEntry(orderHeader, EntityState.Modified);
context.SaveChanges();
}
}
}
return new ResponseBaseModel(MessageCode.OK);
}
catch (Exception ex)
{
return new ResponseBaseModel(MessageCode.Fail, ex.InnerException.Message);
}
}
Fail Methods
public async Task<ResponseBaseModel> AddOrderRemark2(AddOrderRemarkRequestModel model)
{
try
{
using (ChatEntities context = new ChatEntities(CurrentUsername))
{
List<string> statusList = getPendingStatus(context).Result;
OrderHeader orderHeader = getOrderHerderByOrderCode(context, model.OrderCode, model.SalesChannelId).Result;
if (statusList.Contains(orderHeader.Status))
{
if (orderHeader != null)
{
orderHeader.Remark = model.Remark;
context.DBEntry(orderHeader, EntityState.Modified);
context.SaveChanges();
}
}
}
return new ResponseBaseModel(MessageCode.OK);
}
catch (Exception ex)
{
return new ResponseBaseModel(MessageCode.Fail, ex.InnerException.Message);
}
}
Edit
I have realise that there are two APIs call at the same times from client sides. Moreover, these two APIs update on the same table 'OrderHeader' which contain both receiver info and remark that why it causes this issue!!. How can I prevent this issue guys?
[HttpPost]
[ActionName("AddReceiverAddress")]
[ChatAuthentication]
public async Task<ResponseBaseModel> AddReceiverAddress(AddReceiverAddressRequestModel model)
{
return _orderService.Value.AddReceiverAddress(model).Result;
}
[HttpPost]
[ActionName("AddOrderRemark")]
[ChatAuthentication]
public async Task<ResponseBaseModel> AddOrderRemark(AddOrderRemarkRequestModel model)
{
return _orderService.Value.AddOrderRemark(model).Result;
}
You are not using async properly. Try this instead
public async Task<ResponseBaseModel> AddOrderRemark2(AddOrderRemarkRequestModel model)
{
try
{
using (ChatEntities context = new ChatEntities(CurrentUsername))
{
List<string> statusList = await getPendingStatus(context);
OrderHeader orderHeader = await getOrderHerderByOrderCode(context, model.OrderCode, model.SalesChannelId);
When you call this method, did you await or Wait() for a result?
When you call the method you have to do either one of them as below sample.
await AddOrderRemark2(model);
Or
AddOrderRemark2(model).Wait();

How to avoid duplicate "using" code

I have a wcf api and wish to wrap all requests inside a transaction
Currently my code looks like this in each endpoint
public MyCompleteList ReadOrganisations()
{
MyCompleteList resp = new MyCompleteList ();
try
{
using (TransactionScope scope = new TransactionScope())
{
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
DC_Base browser_request = new DC_Base(PROJECT);
browser_request.cmd_user_id = coreDb.GetUserIDFromLoginName(PROJECT,
HttpContext.Current.User.Identity.Name);
resp =
new MyCompleteList (coreSc.User_Read_All_Organisations(browser_request, utils,
validation, coreSc, coreDb));
scope.Complete();
}
else
{
resp.SetResponseNotLoggedIn();
}
}
}
catch (TransactionAbortedException ex)
{
resp.SetResponseServerError();
}
catch (ApplicationException ex)
{
resp.SetResponseServerError();
}
return resp;
}
As you can see if I am to use the "using" transaction scope part in every endpoint (approx 300) its going to be a lot of duplicated code.
is there anyway to reduce the amount of duplication?
You can write a helper method, that handles the transaction logic while calling your actual code as a lambda.
public static T Execute<T>(Func<T> func, TransactionExecutionOptions options = null)
{
options = options ?? TransactionExecutionOptions.Default;
T res;
using (var tx = new TransactionScope(options))
{
res = func();
tx.Complete();
}
return res;
}
Depending on your needs you can provide additional arguments to the Func argument; for example, the Execute method could also open a database connection and pass that to the func (then having Func<IDbConnection, T> as parameter type). YMMV.
For your example:
public MyCompleteList ReadOrganisations()
{
MyCompleteList resp = new MyCompleteList ();
try
{
resp = Execute(() => {
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
DC_Base browser_request = new DC_Base(PROJECT);
browser_request.cmd_user_id = coreDb.GetUserIDFromLoginName(PROJECT,
HttpContext.Current.User.Identity.Name);
resp =
new MyCompleteList (coreSc.User_Read_All_Organisations(browser_request, utils,
validation, coreSc, coreDb));
scope.Complete();
}
else
{
resp.SetResponseNotLoggedIn();
}
});
}
catch (TransactionAbortedException ex)
{
resp.SetResponseServerError();
}
catch (ApplicationException ex)
{
resp.SetResponseServerError();
}
return resp;
}
If possible, you can also factor the SetResponse*() methods out into a base class or interface (say IMyResponse), thus making it possible to handle this aspect inside the Execute method as well.
public static T Execute<T>(Func<T> func, TransactionExecutionOptions options = null) where T : IMyResponse
{
options = options ?? TransactionExecutionOptions.Default;
T res;
try
{
using (var tx = new TransactionScope(options))
{
res = func();
tx.Complete();
}
}
catch (TransactionAbortedException ex)
{
res.SetResponseServerError();
}
catch (ApplicationException ex)
{
res.SetResponseServerError();
}
return res;
}
1- Create a ServiceBase class as follows
public class ServiceBase
{
protected void ExecuteOperation(Action codetoExecute)
{
try
{
using (TransactionScope scope = new TransactionScope())
{
codetoExecute.Invoke();
scope.Complete();
}
}
catch (TransactionAbortedException ex)
{
// handle exception
}
catch (ApplicationException ex)
{
// handle exception
}
}
}
2- Each new service must inherits from ServiceBase and call ExecuteOperation instead. Code as follows:
ExecuteOperation(() =>
{
// Custom code here
});
3- Atomic transactions are useful when executing operations that dont expect results in return.

Asp Mvc 5 Async/Await Issue

public async Task<ActionResult> Index()
{
var service = new CoreServiceFactory().GetImpersonatingService();
try
{
var data = new Impersonation()
{
ImpersonatingId = "dac733c3-01ad-447b-b0df-3a7c21fef90b",
UserId = "dac733c3-01ad-447b-b0df-3a7c21fef90b"
};
var imp = await service.Add(data);
}catch(Exception ex) { throw ex; }
return View();
}
Above is one of my controllers action method. And this works fine when the insertion is successful. This should fail if the data already exists in database(unique constraints). So when i intentionally try to make it fail(i manually add the same record in the db and then try to add it again via this action method) the action method goes into a loop or something, the exception is never thrown , chrome keeps me showing me the loading icon , looks like it went into some deadlock state. Can someone please help me understand why it goes into deadlock state when exception is thrown and how can i handle it?
Below are the reference methods
service.Add(data)
public async Task<Impersonation> Add(Impersonation t)
{
if (ValidateData(t))
{
using (var uow = GetUnitOfWork())
{
var r = GetRepository(uow);
var item = r.Add(t);
try
{
var ret = await uow.Save();
if (ret > 0)
{
return item;
}
else
{
return null;
}
}
catch (Exception ex)
{
throw ex;
}
}
}
else
{
throw new ValidationException(null, "error");
}
}
uow.Save()
public class BaseUnitOfWork : IUnitOfWork
{
public DbContext _Context { get; private set; }
public BaseUnitOfWork(DbContext context)
{
this._Context = context;
}
public async Task<int> Save()
{
try
{
var ret = await this._Context.SaveChangesAsync();
return ret;
}catch(Exception ex)
{
throw ex;
}
}
}
Here is my suggestion: in uow.Save, log the error in the catch block and return zero (do not throw any exceptions).
public class BaseUnitOfWork : IUnitOfWork
{
public DbContext _Context { get; private set; }
public BaseUnitOfWork(DbContext context)
{
this._Context = context;
}
public async Task<int> Save()
{
try
{
var ret = await this._Context.SaveChangesAsync();
return ret;
}catch(Exception ex)
{
// log the error here
return 0;
}
}
}
I'm not sure if returning the null in the Add service is a good idea or not, you might need to handle that differently too.

How to return JsonResult through generic Func<T>?

Having an Action that uses a base method that expect a Func
public class HomeController: BaseController
{
public JsonResult HomeController()
{
var model = ExecuteHandledJTableJsonOperation(() =>
{
//do some stuff
}, LocalResources.CommonErrorMessage);
return Json(model);
}
}
And the base method that expect Func
public class BaseController : Controller
{
public T ExecuteHandledJTableJsonOperation<T>(Func<T> actionToExecute, string errorMessage)
{
try
{
return actionToExecute.Invoke();
}
catch (Exception ex)
{
LogEntry entry = new LogEntry();
entry.AddErrorMessage(ex.Message);
entry.AddErrorMessage(String.Format("Inner Exception:", ex.InnerException.Message));
//entry.Message = ex.Message;
entry.Priority = 1;
entry.EventId = 432;
entry.Severity = System.Diagnostics.TraceEventType.Error;
writer.Write(entry);
return Json(new { Result = "ERROR", Message = errorMessage });
}
}
}
It retrieves me an error when I trying to return Json(new { Result = "ERROR", Message = errorMessage });
Cannot implicitly convert type 'System.Web.Mvc.JsonResult' to 'T'
I know that is better if I create an override of ExecuteHandledJTableJsonOperation that expect two action, one to execute/return normally and the second to execute when the operation has an excetion.
Something like this:
return ExecuteHandledJTableJsonOperation(() =>
{
//do something
return Json(new { Result = "OK", Records = excepciones, TotalRecordCount = excepciones.Count() });
}, () =>
{
return Json(new { Result = "ERROR", Message = Properties.Resources.CommonErrorMessage });
});
But I want to know how to solve the first case:
Cannot implicitly convert type 'System.Web.Mvc.JsonResult' to 'T'
Thanks.
It is not entirely clear why your method is generic as you seem to want to always return a JsonResult as such simply change your method to this.
public class BaseController : Controller
{
public JsonResult ExecuteHandledJTableJsonOperation<T>(Func<JsonResult> actionToExecute, string errorMessage)
{
try
{
return actionToExecute.Invoke();
}
catch (Exception ex)
{
LogEntry entry = new LogEntry();
entry.AddErrorMessage(ex.Message);
entry.AddErrorMessage(String.Format("Inner Exception:", ex.InnerException.Message));
//entry.Message = ex.Message;
entry.Priority = 1;
entry.EventId = 432;
entry.Severity = System.Diagnostics.TraceEventType.Error;
writer.Write(entry);
return Json(new { Result = "ERROR", Message = errorMessage });
}
}
}
I guess you don't need that to be soo generic. Providing that you will use that only in your actions you can return ActionResult as JsonResult derived from it. Think adding constraint of ActionResult will be sufficient in your case:
public T ExecuteHandledJTableJsonOperation<T>(Func<T> actionToExecute, string errorMessage)
where T: ActionResult
{
//code
}

Need to wrap functions in a standard try/catch function in c#

I know there are lots of delegate/func examples but I can't find any examples that will work for me, or I just don't understand them.
I'm using asp.net MVC for a website, and the website needs some web service calls for an outside application to interact with my app. These all need a function to execute (going to db and whatnot), and return a similar data model every time. I want to wrap each call in a try/catch and populate the model.
Here is the generic code that happens every call.
var model = new ResponseDataModel();
try
{
//execute different code here
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return View(model); // will return JSON or XML depending on what the caller specifies
This is one of the controller methods/ functions that I am using
public ActionResult MillRequestCoil()
{
var model = new ResponseDataModel();
try
{
/* edit */
//specific code
string coilId = "CC12345";
//additional code
model.Data = dataRepository.doSomethingToCoil(coilId);
//replaced code
//model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" };
model.Message = string.Format("Coil {0} sent successfully", coilId);
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return View(model);
}
I would like to be able to somehow convert the specific function to a variable to pass into the generic code. I've looked at delegates and anonymous funcs but it's pretty confusing until you do it yourself.
Put the following somewhere accessible:
public static ActionResult SafeViewFromModel(
Action<ResponseDataModel> setUpModel)
{
var model = new ResponseDataModel();
try
{
setUpModel(model);
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return View(model);
}
and then call it like:
public ActionResult MillRequestCoil()
{
return MyHelperClass.SafeViewFromModel(model =>
{
string coilId = "CC12345";
model.Data = new {
Coil = coilId,
M3 = "m3 message",
M5 = "m5 message" };
model.Message = string.Format("Coil {0} sent successfully", coilId);
});
}
public interface IAction{
public void doAction(ResponseDataModel model);
}
public class Action1 : IAction
{
public void doAction(ResponseDataModel model)
{
string coilId = "CC12345";
model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" };
model.Message = string.Format("Coil {0} sent successfully", coilId);
}
}
class Class1
{
public ActionResult MillRequestCoil(IAction action)
{
var model = new ResponseDataModel();
try
{
//specific code
action.doAction(model);
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return View(model);
}
}
Use:
var result = MillRequestCoil(new Action1());
or execute other code
var result = MillRequestCoil(new ActionXY());
This is a variation of Rawling's answer, that I believe has better readability:
public ActionResult MillRequestCoil()
{
var model = CreateResponseDataModel(RunSpecificCode);
return View(model);
}
public ActionResult MillDoSomethingElse ()
{
var model = CreateResponseDataModel(RunOtherCode);
return View(model);
}
private ResponseDataModel CreateResponseDataModel(Action<ResponseDataModel> action)
{
var model = new ResponseDataModel();
try
{
action(model);
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return model;
}
private void RunSpecificCode(ResponseDataModel model)
{
/* edit */
//specific code
const string coilId = "CC12345";
//additional code
model.Data = _dataRepository.DoSomethingToCoil(coilId);
//replaced code
//model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" };
model.Message = string.Format("Coil {0} sent successfully", coilId);
}
private void RunOtherCode(ResponseDataModel obj)
{
// some other piece of code
}
No lambdas, and nice separation of concerns between your specific code and the template code for the try/catch and model building stuff.

Categories