I'm trying to deserialize a collection to class.
It seems that in case one of the fields named Id, I will get the error:
base {"An error occurred while deserializing the Address property of
class Person.LicenseEntity: Element 'Id' does not match any field or
property of class Person.Address"} System.FormatException
{System.IO.FileFormatException}
However, changing the field name (e.g. to Idd) in both the class and the collection resolve the problem.
Is it possible that I'm not allowed to use the Id field?
I'm pretty sure this is because the Mongo C# driver deserializes the generated _id field from the document to the property named Id in your class. This means that your Id field has nowhere to go, and explains why changing the name of the Id to Idd allowed it to work.
As you are using a class called Address, I would perhaps name your field AddressId
Have a read of the Mongo C# Driver Docs I'm sure they will be a great help.
Related
I am coding a VSTO add-in for Outlook, and in order to access MailItem properties that are not exposed by the Outlook Object Model (such as email header text) the approach put-forth by Microsoft (and here on stackoverflow) is to use code like the following, that has a "http://schemas.microsoft.com" URL:
Outlook.PropertyAccessor oPA = msg.PropertyAccessor as Outlook.PropertyAccessor;
const string PR_MAIL_HEADER_TAG = #"http://schemas.microsoft.com/mapi/proptag/0x007D001E";
try
{
string strHeaders = (string)oPA.GetProperty(PR_MAIL_HEADER_TAG);
}
catch { }
I am trying to understand what the GetProperty method is doing with the http://schemas.microsoft.com URL, and the documentation I found (here and here) has been less than helpful.
Am I correct that these URLs are just labels used to access different object namespaces?
If that's the case, why are they in an http URL format? Will my compiled executable actually reach out to the Internet to grab some info at this URL?
Typing that address into a web browser brings up the message: "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." So -- it would appear not?
Or is it using the URL to look up a locally stored HTTP document -- that the compiler inserts into the assembly similar to a resource file?
Either way -- what information is the PropertyAccessor.GetProperty method using based on the URL that is passed to it?
I'm leery about using the PropertyAccessor.GetProperty method, especially if it is in fact actually reaching out to the Internet... so any guidance on this, or additional sources I could look-to would be greatly appreciated.
Yes, it is just a label - no network connection is ever made. Think of the DASL name as a unique (to Microsoft) property name prefix used by the Outlook Object Model, Exchange Web Services (EWS), and Graph.
In Extended MAPI, a property tag is just a 4 byte unsigned int, with the upper two bytes being the property id and the lower two bytes are the property tag: i.e. PR_SUBJECT_W is 0x0037001F. 0x0037 is the property id, and 0x001F is the property type (PT_UNICODE in this case). Properties <= 0x7FFFFxxxx are fixed properties (their property id never changes), and properties >= 0x80000000 are named properties: their property id is determined at run time based on the named property GUID and some id (either string or an integer) - this is done to support custom properties without the risk of property id collision: the client app is expected to call IMAPIProp::GetIdsFromNames() and pass a list of guids and ids, and get back property ids guaranteed to uniquely map to the given guid/id/name in the particular message store.
DASL property names are designed to hide that complexity, for PR_SUBJECT_W above, its DASL property name is "http://schemas.microsoft.com/mapi/proptag/0x0037001F". For the named properties it is slightly different. E.g. for the named property commonly known as SmartNoAttach (GUID={00062008-0000-0000-C000-000000000046} - which is PSETID_Common, id = 0x8514, and property type of PT_BOOLEAN), the DASL name is "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B" - note the 8514000B part: it is id = 0x8514, and 0x000B is PT_BOOLEAN type. If the id is a string rather than an int, the format is slightly different - e.g. http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/Keywords/0x0000101F" for the named property used to store the message categories.
Some common properties can be represented by other names, e.g. you can alternatively use "urn:schemas:mailheader:keywords" or "urn:schemas-microsoft-com:office:office#Keywords" for the Categories property above.
With the DASL property name, you can safely hardcode its string representation (unlike the named property integer tag, which can differ for different message stores even in the same profile).
You can take a look at the existing MAPI property tags and their DASL names in OutlookSpy (I am its author) - click IMessage button to see property tags, DASL names, and property values.
I'm currently porting a site to ASP.NET Core 2 and am getting the following Exception thrown when I call userManager.GenerateEmailConfirmationTokenAsync(user) with a user class that extends IdentityUser<Guid>:
System.ArgumentNullException: Value cannot be null.
Parameter name: value
at System.IO.BinaryWriter.Write(String value)
at Microsoft.AspNetCore.Identity.DataProtectorTokenProvider`1.<GenerateAsync>d__11.MoveNext()
GenerateAsync() makes a call to UserManager.GetUserIdAsync(), which is returning the null value in Result, which Write() is complaining about, even though the User instance I'm passing through has 07c0423e-f36b-1410-8007-800000000000 in Id.
If I call userManager.GetUserIdAsync(user) directly on the same UserManager instance on which I'm calling GenerateEmailConfirmationTokenAsync() I get this same null value in Result.
Does anyone have any ideas how to resolve this?
Edit: As requested, a functional example can be found at github.com/BenjaminNolan/GetUserIdAsyncNullExample
The problem you have is with these lines from your User class:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[DefaultValue("NEWSEQUENTIALID()")]
new public Guid Id { get; set; }
Your use of the new modifier here is suppressing the following compiler warning:
'User.Id' hides inherited member 'IdentityUser.Id'.
This warning informs you that if you access Id via a User reference (rather than e.g. a IdentityUser<Guid> reference - the base class), you will be working with a different Id property. You can see what this looks like if you inspect the value of user.Id in your controller and compare that with the value of ((IdentityUser<Guid>)user).Id (casting to the base class in order to access its Id property).
It's easy to fix in your case - just remove the Id property from your User class: you've already specified that the existing Id property from IdentityUser<TKey> will be of type Guid and its already configured to be the key, etc.
I have created a class student and in that class contact object is created (aggregation). I am retrieving all stored objects in "object" array.
If I want to access student attribute I have to type cast it with student and if I want to access contact attribute either I have to give full path (student_object.contact_object.attribute_name) or simply type cast with contact and get the attribute value.
I have stuck at two places:
If I accept class name
from user and then want to access
the value. How can I do that? How to
type cast with textbox variable.
If field name I accept
from user in text box . How can I
access the value using text box
variable?
Jon,
Actually I have created a object oriented database in C#.NET. where above classes are there. I am Retrieving all objects in Object array. Class student has field name and age , while contact has mobileID . Now I am creating a query through textboxes. If user want to see name of all the object , then var2 name he shold put. but i am not able to get this message
messageBox.show(o0 as Student).var2);
Same if instead of Student if I give var1
messageBox.show(o0 as var1).var2);
Can I do this?
thanks/Bharti
Casting is important for compile-time information. That precludes anything that the user provides at execution time.
It sounds like you really need to just reflection:
PropertyInfo property = value.GetType().GetProperty(propertyName);
// Insert validation here...
object propertyValue = property.GetValue(value, null);
You'll need to be smarter if you want the user to be able to evaluate a path of properties such as Address.ZipCode but it should give you a starting point.
I'm testing the CTP5 for entity framwork code first, and i've run into this problem
I've got a class that has a property of type Uri (System.Uri), but it looks like it's unable to automatically identify how to store that, so i get an error like
Problem in mapping fragments starting at line 23:No mapping specified for properties WebPage.Uri in Set WebPage
How can i tell the model to map the Uri to a varchar, for example, with the url of the uri??
The actual POCO model has to bind to primitive types. You can use a complex type binding such as:
[ComplexType()]
public class UriHelper
{
public string StringRepresentation {get;set;}
public Uri ActualUri()
{
return new Uri(StringRepresentation);
}
}
And in your actual object reference this complex type as the Uri reference if you absolutely need to. Your mapping would then reference the property for the actual value as a String. The final option is to create a custom mapping from URI to string and vice versa for the EF engine to use. However, I would not advise this. The actual database property is of type varchar or nvarchar, not URI. Thus EF doesn't know what a URI is.
I'm using v0.9 of the official MongoDB driver and i'm trying to read in a collection. I have a field in the database that I don't want to read into my object but I get the following error.
"Unexpected element: Network"
The collection looks like this in the database
Merchants
- _id
- Name
- Description
- Url
- Network
When I read it into C# I want to create an object called Merchant that has all of the same properties, except "Network". How do I do this?
There's an "IgnoreExtraElements" option on the BSON serializer which you can enable to prevent that error.
Either set it as an attribute on your Merchant class:
[BsonIgnoreExtraElements]
public Merchant {
// fields and properties
}
or in code if you're using class maps:
BsonClassMap.RegisterClassMap<Merchant>(cm => {
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
});