What do I return in a async task when an exception occurs - entity-framework

I have the following code...
public async Task<User> GetUserAsync(string name)
{
await using var db = new ApplicationDbContext(new DbContextOptions<ApplicationDbContext>());
try
{
return await db.User.SingleAsync(x => x.Name == name);
}
catch (Exception e)
{
//return new User();
}
}
I wonder what I would return if there is an exception because the query does not find a match from the database? Do I return a empty user object or what is standard procedure?

what is standard procedure?
It's normal to let exceptions propagate until you have something else you can do with them.

Related

Rare error when trying to save data "unhandled exception on the current circuit"

I am using VS 2022, Blazor server project. When I trying to save data
async public static Task<bool> updateObject(Firecall obj)
{
Firecall r;
try
{
using (var context = new sptContext())
{
r = context.Firecalls.Where(c => c.Mguid == obj.Mguid).FirstOrDefault();
bool новое = (r == null);
if (новое)
{
r = new Firecall();
}
r.comment = obj.comment;
if (новое)
await context.Firecalls.AddAsync(r);
if (busy)
return false;
try
{
busy = true;
await context.SaveChangesAsync();
}
catch (Exception)
{
return false;
}
finally {
busy = false;
}
}
return true;
}
catch (Exception)
{
return false;
}
}
sometimes I get error:
Sometimes an error occurs, sometimes not. No error in debugger.
How to solve problem?
P.S. Data in each operation is saved as expected. Only after the operation is completed the indicated error message appear
And calling savechanges method from #code block of .razor view:
async private void SaveChanges()
{
bool rez = await firecallRepository.updateObject(_currentFireCall);
}

InvokeApiAsync<HttpResponseMessage> returns null

Can someone explain my why that client (Xamarin.Forms PCL) call returns null?
HttpResponseMessage response = await OfflineSyncStoreManager.Instance.MobileAppClient.InvokeApiAsync<HttpResponseMessage>("ResetTruckAuftragWorkflow");
response is null. When I execute that in a console app it returns the
valid http response.
I use the latest stable ZUMO nugets in client and backend. There is my ZUMO backend code:
[Authorize]
[MobileAppController]
public class ResetTruckAuftragWorkflowController : ApiController
{
private readonly RcsMobileContext _rcsMobileContext;
private readonly TruckFahrerInfo _truckFahrerInfo;
public ResetTruckAuftragWorkflowController()
{
_rcsMobileContext = new RcsMobileContext();
_truckFahrerInfo = new TruckFahrerInfo(this.User as ClaimsPrincipal);
}
// POST api/ResetTruckAuftragWorkflow
[HttpPost]
public async Task<IHttpActionResult> PostAsync()
{
if (ModelState.IsValid)
{
using (var transaction = _rcsMobileContext.Database.BeginTransaction())
{
try
{
var truckAuftragList = _rcsMobileContext.TruckAuftrags.PerUserFilter(_truckFahrerInfo.FahrerId);
var truckAppIds = truckAuftragList?.Select(ta => ta.TruckAppId).ToArray();
if (truckAppIds != null)
{
foreach (var truckAppId in truckAppIds)
{
await _rcsMobileContext.Database.ExecuteSqlCommandAsync(_tawQueryTaskStatus10, truckAppId);
await _rcsMobileContext.Database.ExecuteSqlCommandAsync(_tawQueryTaskStatus5, truckAppId);
await _rcsMobileContext.Database.ExecuteSqlCommandAsync(_talQuery, truckAppId);
await _rcsMobileContext.Database.ExecuteSqlCommandAsync(_taQuery, truckAppId);
}
}
await _rcsMobileContext.Database.ExecuteSqlCommandAsync(_taQuery, _truckFahrerInfo.FahrerId);
transaction.Commit();
}
catch (Exception e)
{
transaction.Rollback();
return BadRequest($"Transaction failed: {e}");
}
}
return Ok();
}
else
{
return BadRequest(ModelState);
}
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_rcsMobileContext.Dispose();
}
base.Dispose(disposing);
}
}
thanks
Eric
InvokeApiAsync decodes the body that is returned and de-serializes the JSON into type T. You should not use HttpResponseMessage for this purpose as it is not serializable.
If you don't care about the body, use the non-generic form of InvokeApiAsync.

Azure Mobile Apps Offline Client Throws NotSupportedException on Query

I have a Azure Mobile Apps Xamarin.Forms PCL client and have Offline Sync enabled. I tried to Pull data from my backend and afterwards query data from the offline storage with a Where clause. That throws the following exception and I don't know why.
Sync error: 'fahrerinfo.Imei.Equals("02032032030232")' is not supported in a 'Where' Mobile Services query expression.
public async Task SyncAsync()
{
ReadOnlyCollection<MobileServiceTableOperationError> syncErrors = null;
try
{
await OfflineSyncStoreManager.Instance.TruckFahrerTable.PullAsync("allTruckFahrerItems",
OfflineSyncStoreManager.Instance.TruckFahrerTable.CreateQuery());
Debug.WriteLine("SyncAsync: PUSH/PULL completed.");
}
catch (MobileServicePushFailedException e)
{
Debug.WriteLine("SyncAsync: PUSH failed.");
Debug.WriteLine(e.Message);
}
catch (Exception e)
{
Debug.WriteLine("SyncAsync: PUSH/PULL failed.");
Debug.WriteLine(e.Message);
//Debugger.Break();
}
}
public async Task<ObservableCollection<TruckFahrer>> GetTruckFaherAsync(bool syncItems)
{
try
{
if (syncItems)
{
await OfflineSyncStoreManager.Instance.SyncAsync().ConfigureAwait(false);
}
var deviceInfo = DependencyService.Get<IDeviceInfo>().GetPhoneInfo();
var imeiString = deviceInfo[trucker_rolsped.PhoneInfo.PhoneInfo.ImeiKey];
var imei = imeiString.Equals("000000000000000") ? deviceInfo[trucker_rolsped.PhoneInfo.PhoneInfo.IdKey] : imeiString;
IEnumerable<TruckFahrer> items =
await OfflineSyncStoreManager.Instance.TruckFahrerTable
//.Where(fahrerinfo => fahrerinfo.Imei.Equals(imei)) TODO: Why does that throw an exception???
.ToEnumerableAsync();
// TODO: Because above does not work
items = items.Where(fahrer => fahrer.Imei.Equals(imei));
return new ObservableCollection<TruckFahrer>(items);
}
catch (MobileServiceInvalidOperationException msioe)
{
Debug.WriteLine(#"Invalid sync operation: {0}", msioe.Message);
Debugger.Break();
}
catch (Exception e)
{
Debug.WriteLine(#"Sync error: {0}", e.Message);
Debugger.Break();
}
return null;
}
Thanks for any hint,
Eric
Are you a Java developer too? I'm and had this issue because in Java we need to compare strings with String#equals method, haha.
For some reason MobileServices doesn't allow us to use Equals in this situation.
To fix your problem, use == instead. As you can see here C# difference between == and Equals() both have the same effect in this case.
Where(fahrerinfo => fahrerinfo.Imei == imei)

Mongodriver c# 2.2.3 UpdateOneAsync Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.StringSerializer'

I'm refactoring my webapp to use the last mongo driver c# feature as async methods.
The find async methods are working well for now but I have an issue with UpdateOneAsync .
Here is my method in DAL :
public static async Task<bool> UpdateOne<T>(FilterDefinition<T> filterDefinition, UpdateDefinition<T> updateDefinition)
{
var coll = GetCollection<T>();
var result = await coll.UpdateOneAsync(filterDefinition, updateDefinition);
return result.ModifiedCount > 0;
}
And it's called from another class with this :
public virtual async Task<bool> UpdateField<T>(Expression<Func<T, bool>> findExpression, Expression<Func<T, dynamic>> fieldExpression, dynamic value)
{
try
{
var filter = Builders<T>.Filter.Where(findExpression);
UpdateDefinition<T> updateFilter = Builders<T>.Update.Set(fieldExpression, value);
var updateResult = await MongoClientWrapper.UpdateOne(filter, updateFilter);
return updateResult;
}
catch (Exception ex)
{
_logMongoRepo.ErrorFormat("(MongoRepo) Error on UpdateField : {0}", ex);
return false;
}
}
The error returned by UpdateOneAsync is :
Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.StringSerializer' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[System.Object]
When I try to use UpdateOneAsync with my object Type, it's ok.
I really don't know where the error could come from :/

CRM 2011 Plugin won't update and get emails from ActivityParty

I'm trying to get all emails (to, from, cc) from an email in a list and go through the list and check the contacts, if the Contact exists in CRM then a field on the email entity will be marked as true. When I check the to, from, and cc fields of the email it returns 0 parties, but there is no error there. Also at the end, when I'm calling service.Update(entity), it returns an error. An unexpected error occurred.
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider
.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider
.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory
.CreateOrganizationService(context.UserId);
try
{
Email entity;
if (context.MessageName == "Create")
{
if (context.PostEntityImages.Contains("PostImage")
&& context.PostEntityImages["PostImage"] is Entity)
entity = (Email)context.PostEntityImages["PostImage"].ToEntity<Email>();
else
throw new Exception("No PostEntityImages...");
}
else
throw new Exception("EmailPortalVisibilityPlugin Plugin invalid");
if(entity.LogicalName != "email")
throw new Exception("EmailPortalVisibilityPlugin invalid");
bool contactExists = false;
List<string> emails = new List<string>();
emails.AddRange(ParseAddressUsed(entity.To, trace));
emails.AddRange(ParseAddressUsed(entity.From, trace));
emails.AddRange(ParseAddressUsed(entity.Cc, trace));
foreach (String em in emails)
{
contactExists = LookupContact(em, service, trace);
if (contactExists)
break;
}
UpdateToggleState(entity, contactExists, service, trace);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("Execute '" + ex.Message + "'");
}
}
public List<string> ParseAddressUsed(
IEnumerable<ActivityParty> entity, ITracingService trace)
{
try
{
List<string> addressStrings = new List<string>();
foreach (ActivityParty party in entity)
addressStrings.Add(party.PartyId.Id.ToString());
return addressStrings;
}
catch (FaultException<OrganizationServiceFault> exceptionServiceCall)
{
throw new Exception("ParseAddressUsed FaultException");
}
catch (Exception ex)
{
throw new Exception("ParseAddressUsed Exception");
}
}
public bool LookupContact(
String emailAddress, IOrganizationService service, ITracingService trace)
{
try
{
QueryByAttribute queryByAttribute = new QueryByAttribute("contact");
queryByAttribute.ColumnSet = new ColumnSet("contactId");
queryByAttribute.Attributes.Add("emailaddress1");
queryByAttribute.Values.Add(emailAddress);
EntityCollection retrieved = service.RetrieveMultiple(queryByAttribute);
return (retrieved.Entities.Count > 0);
}
catch (FaultException<OrganizationServiceFault> exceptionServiceCall)
{
throw new Exception("LookupContact Exception");
}
catch (Exception ex)
{
throw new Exception("LookupContact Exception");
}
}
public void UpdateToggleState(
Email entity, bool toggleState, IOrganizationService service, ITracingService trace)
{
try
{
Entity email = new Entity("email");
email.Id = entity.Id;
email.Attributes.Add("new_clientfacing", toggleState);
service.Update(email);
}
catch (FaultException<OrganizationServiceFault> exceptionServiceCall)
{
throw new Exception("UpdateToggleState Exception");
}
catch (Exception ex)
{
throw new Exception("UpdateToggleState Exception");
}
}
Try to set the first argument type of function ParseAddressUsed to EntityCollection instead of IEnumerable<ActivityParty>, and do the necessary changes.
And for the final update in function UpdateToggleState, there is no need to create a new email Entity (Entity email = new Entity("email");), when you already have the entity variable. You could just set the new_clientfacing attribute and update the entity, which is already retrieved.
In your method ParseAddressUsed you are adding the PartyId GUID to the string list and you use it in LookupContact in the emailaddress1 filter as a parameters, that is probably the reason why you are not retrieving any records.
Please try to change addressStrings.Add(party.PartyId.Id.ToString()) to addressStrings.Add(party.AddressUsed) instead and see if that works.
Cheers, dimamura