I am trying to Get EventMessage from Message in MS Graph API with C# but every time it is showing type as a message instead of EventMessage. Below are the code:-
public static Graph.MailFolderMessagesCollectionPage ReadInbox()
{
GetAuthenticatedClient();
var result = new Graph.MailFolderMessagesCollectionPage();
List<Graph.QueryOption> options = new List<Graph.QueryOption>
{
new Graph.QueryOption("$expand","microsoft.graph.eventMessage/event"),
new Graph.QueryOption("$filter","isread eq false")
};
try
{
var response = graphClient.Me.MailFolders.Inbox.Messages.Request(options).OrderBy("receivedDateTime DESC").GetAsync();
result = response.Result as Graph.MailFolderMessagesCollectionPage;
}
catch (Exception ex)
{ }
Call the above method ReadInbox to get type and perform some action.
var appointments = ReadInbox();
if (appointments != null)
{
foreach (dynamic request in appointments)
{
try
{
if (request.GetType().Name.Contains("EventMessage"))
{
}
else if (request.GetType().Name == "Message")
{
}
}
catch (Exception ex)
{
}
}
}
Use the IsInstanceOfType method to identify if its an eventMessage. You can also remove the expand option from the query option since eventMessages are fetched anyway as part of the get Messages call.
if (appointments != null)
{
foreach (dynamic request in appointments)
{
try
{
if (typeof(EventMessage).IsInstanceOfType(request))
{
Console.WriteLine("Is an event");
Console.WriteLine(request);
}
}
catch (Exception ex)
{
}
}
}
Related
I am trying to fetch data from CRM using this API. I get an error
Runtime binding on a null reference
whenever I try to get value from data.fullname. Is there any way I can fix it?
Thanks
var response = httpClient.GetAsync("contacts?$select=fullname,emailaddress1").Result;
if (response.IsSuccessStatusCode)
{
var accounts = response.Content.ReadAsStringAsync().Result;
var jRetrieveResponse = JObject.Parse(accounts);
dynamic collContacts = JsonConvert.DeserializeObject(jRetrieveResponse.ToString());
try
{
foreach (var data in collContacts.value)
{
// You can change as per your need here
if (data.fullname.Value != null)
{
success[i] = data.fullname.Value;
}
i ++;
}
}
catch (Exception)
{
throw;
}
}
Replace
if (data.fullname.Value != null)
with this
if (!String.IsNullOrWhiteSpace(data.fullname.Value))
OR Replace
try
{
foreach (var data in collContacts.value)
{
// You can change as per your need here
if (data.fullname.Value != null)
{
success[i] = data.fullname.Value;
}
i ++;
}
}
catch (Exception)
{
throw;
}
With
try
{
foreach (var data in collContacts.value)
{
success[i] = data?.fullname?.Value;
i ++;
}
}
catch (Exception)
{
throw;
}
I'm fairly new to EF transactions, this is the code that is used for saving
public bool Save(TbArea area, bool isNew, out string errMsg)
{
try
{
errMsg = string.Empty;
using (var oScope = new System.Transactions.TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(120)))
{
try
{
TbArea oEntity = oContext.TbArea.Where(a => a.AreaCode == area.AreaCode && a.CompanyId == MainClass.SystemCompanyId).FirstOrDefault();
if (oEntity != null)
{
if (isNew) { errMsg = Resources.ResSales.MsgRecordCodeDublicated; return false; }
oContext.TbArea.Attach(oEntity);
oContext.Entry(oEntity).CurrentValues.SetValues(area);
}
else
{
if (!isNew) { errMsg = Resources.ResSales.MsgRecordNotFoundInDB; return false; }
oContext.TbArea.Add(area);
}
oContext.SaveChangesAsync();
oScope.Complete();
return true;
}
catch (Exception ex)
{
oScope.Dispose();
errMsg = ex.Message;
return false;
}
}
I'm overriding SaveChangesAsync
so that I can save the ChangeTracker.Entries into the database.
this is a portion of the code:
dbContext.AcceptAllChanges();
logsSet.AddRange(audits);
int result = 0;
try
{
result = await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
//scope.Complete(); there was a transaction that I commented out, I thought it might overlaps the original transaction!
return result;
}
catch (Exception ex)
{
var m = ex.Message;
return result;
}
When I save an item, I get the error :
the transaction has aborted
when I remove the transaction scope the saving happens normally!
Your code is marking the transaction complete before the changes have finished saving:
oContext.SaveChangesAsync();
oScope.Complete();
You need to use await:
await oContext.SaveChangesAsync();
oScope.Complete();
If you're in a context where await can resume on a different thread, you'll probably also need to specify TransactionScopeAsyncFlowOption.Enabled.
We have an ASP.NET website running which throws a NullReference-exception along with a stacktrace and a line number that is simply impossible. And I can't make heads nor tails from it myself.
It says:
Exception at ReportService.GetReport(String reportType) in ReportService.cs:line 1458
which is funny, because that is this line:
var exports = new List<ReportExport>();
Thanks to the (very short) stacktrace, I can see that the error is triggered in the GetReport-function and not in the "GetAllUsers" or "GetAllUsersWithFilter" functions, because I would receive a different error message in my e-mailbox or I would see it pop up in the stacktrace.
So I suspect the line number is wrong, in which case there is only one other possibility and that is this line:
foreach (var userProfile in users) {
exports.Add(CreateUserProfile(userProfile));
}
But how could users ever be null?
Full (albeit simplified) code right here:
public function IList<ReportExport> GetReport(string reportType) {
try {
IQueryable<UserProfile> users = null;
switch (reportType) {
case "abc" :
users = GetAllUsersWithFilter();
break;
case default:
users = GetAllUsers();
break;
}
var exports = new List<ReportExport>();
foreach (var userProfile in users) {
exports.Add(CreateUserProfile(userProfile));
}
} catch (Exception ex) {
SendErrorMail("GetReport has failed", ex); /* I receive this error mail */
}
function IQueryable<UserProfile> GetAllUsers() {
try {
return dbContext.Users.Where(x => x.IsRegistered == true);
} catch (Exception ex) {
SendErrorMail("GetAllUsers", ex); /* I don't receive this e-mail */
return null;
}
}
function IQueryable<UserProfile> GetAllUsersWithFilter() {
try {
return GetAllUsers().Where(x => x.ExtraFilter == true);
} catch (Exception ex) {
SendErrorMail("GetAllUsersWithFilter", ex); /* I don't receive this e-mail */
}
}
function int GetNumberOfSessions(int userId) {
try {
return dbContext.Sessions.Count(x => x.UserId == userId);
} catch (Exception ex) {
SendErrorMail("GetNumberOfSessions", ex); /* I don't receive this e-mail */
}
}
function ReportExport CreateUserExport(UserProfile user) {
try {
var cnt = GetNumberOfSessions(user.Id);
return new ReportExport() {
UserId = user.Id,
NumberOfSessions = cnt
}
} catch (Exception ex) {
SendErrorMail(("CreateUserExport", ex);
}
}
If you are in production then you might be running with optimizations switched on - therefore the line number will be wrong.
But how could users ever be null?
But you are catching the Exception then returning null. You are relying on returning data - which may not be the case in GetAllUsers.
function IQueryable<UserProfile> GetAllUsers() {
try {
return dbContext.Users.Where(x => x.IsRegistered == true);
} catch (Exception ex) {
SendErrorMail("GetAllUsers", ex); /* I don't receive this e-mail */
return null;
}
}
I use following script to get data from external service and store in dB. In certain rare cases less than 1% records gets updated with null values. In below code, the "re.status=fail" we see null. let us know if any thots.
public void ProcessEnquiries()
{
List<req> request = new List<req>();
var options = new ParallelOptions { MaxDegreeOfParallelism = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MaxDegreeOfParallelism"]) };
try
{
Parallel.ForEach(request, options, currentRequest =>
{
ProcessedRequest processedRequest = null;
processedRequest = CommunicateToWS(currentRequest); // Here we call to webservice
});
}
catch (AggregateException exception)
{
foreach (Exception ex in exception.InnerExceptions)
{
// Handle Exception
}
}
}
public ProcessedRequest CommunicateToWS(req objReq)
{
ProcessedRequest re = new ProcessedRequest();
using (WebCall obj = new WebCall())
{
re.no = refnu;
try
{
retval = obj.getValue(inval);
objProxy.Close();
//get data
// parse and store to DB
}
catch (Exception e)
{
re.status = "fail";
//update DB that request has failed
//Handle Exception
obj.Close();
}
}
}
public static string GetContentFromSPList(string cValueToFind)
{
string cValueFound = "";
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://mysite"))
{
site.AllowUnsafeUpdates = true;
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList oListAbout = web.Lists["About"];
SPQuery oQuery = new SPQuery();
oQuery.Query = "<OrderBy><FieldRef Name='myField' /></OrderBy><Where><Eq><FieldRef Name='myField' /><Value Type='Choice'>" + cValueToFind + "</Value></Eq></Where>";
SPListItemCollection collListItems = oListAbout.GetItems(oQuery);
foreach (SPListItem oListItem in collListItems)
{
cValueFound = (oListItem["FieldContents"] != null ? oListItem["FieldContents"].ToString() : "");
}
}
}
return cValueFound;
});
//return cValueFound;
}
catch (Exception ex)
{
}
finally
{
//return cValueFound;
}
}
Above is the piece of code.
Problem is not allowing to return the string. It keeps on giving compilation errors. I am sure I am doing something wrong!!.
Thanks.
I suppose it's something like:
"not all codes return value".
If so, just add
public static string GetContentFromSPList(string cValueToFind)
{
string cValueFound = "";
try
{
//code
}
catch (Exception ex)
{
}
finally
{
//some cleanup
}
return cValueFound ;
}
Put this at the bottom of your method because you don't return if an exception is caught.
catch (Exception ex)
{
return cValueFound;
}
finally
{
}
}
You cannot return from finally,
(control cannot leave the body from finally clause or something)
move the return either after finally or from catch
just add your return statement below finally block.
Dont return in try blck.
I have seen developers missing this lot of times. Reason this happens is because once you define the return type of a function, then function should have a return statement at all exit points. In this case a function should have a return statement one at the end of the try block and one inside a catch block or just one right at the bottom as Tigran has defined. If you do not intend to return any thing from the catch block then just return null;
public static string GetContentFromSPList(string cValueToFind)
{
string value= "";
try
{
//code
return value;
}
catch (Exception ex)
{
return null;
}
finally
{
//some cleanup
}
}