C# Calling list from Class [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I am currently pulling a httpwebrequest for warranty information. I have tested each section for information and it worked. Once I put it into a class it failed.
My question is, how can I effectively call the Device class ?
Dell computerRequest = new Dell(apiKey);
XDocument xlTest = new XDocument();
xlTest = computerRequest.XmlResponse(txtTag.Text.Trim());
XNamespace getAssetWarrantyA = "http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset";
XNamespace getAssetWarrantyI = "http://www.w3.org/2001/XMLSchema-instance";
List<Device> xlAsset = (from asset in xlTest.Descendants(getAssetWarrantyA + "Response")
select new Device()
{
DeviceInfo = (from defaultInfo in asset.Descendants(getAssetWarrantyA + "DellAsset")
select new DeviceBase()
{
Product = (string)asset.Descendants(getAssetWarrantyA + "MachineDescription").FirstOrDefault(),
OrderNumber = (string)asset.Descendants(getAssetWarrantyA + "OrderNumber").FirstOrDefault(),
ServiceTag = (string)asset.Descendants(getAssetWarrantyA + "ServiceTag").FirstOrDefault(),
ShipDate = (string)asset.Descendants(getAssetWarrantyA + "ShipDate").FirstOrDefault()
}).ToList(),
WarrantyInfo = (from warranty in asset.Descendants(getAssetWarrantyA + "Warranty")
select new Warranty()
{
Service = (string)warranty.Descendants(getAssetWarrantyA + "ServiceLevelDescription").FirstOrDefault(),
Provider = (string)warranty.Descendants(getAssetWarrantyA + "ServiceProvider").FirstOrDefault(),
StartDate = (string)warranty.Descendants(getAssetWarrantyA + "StartDate").FirstOrDefault(),
EndDate = (string)warranty.Descendants(getAssetWarrantyA + "EndDate").FirstOrDefault(),
TypeOfWarranty = (string)warranty.Descendants(getAssetWarrantyA + "EntitlementType").FirstOrDefault()
}).ToList(),
}).ToList();
**//FAILS -- No object set to reference**
Device testDevice = new Device();
MessageBox.Show(testDevice.WarrantyInfo.Count.ToString());
dtGrdWar.DataSource = xlAsset.ToArray();
}
public class Device
{
public List<DeviceBase> DeviceInfo{ get; set; }
public List<Warranty> WarrantyInfo { get; set; }
}
public class DeviceBase
{
public string Product { get; set; }
public string OrderNumber { get; set; }
public string ServiceTag { get; set; }
public string ShipDate { get; set; }
}
public class Warranty
{
public string Service { get; set; }
public string Provider { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public string TypeOfWarranty { get; set; }
}
XML:
<GetAssetWarrantyResponse xmlns="http://tempuri.org/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<GetAssetWarrantyResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.datacontract.org/2004/07/Dell.AWR.Domain.Asset">
<a:Faults/>
<a:Response>
<a:DellAsset>
<a:AssetParts i:nil="true"/>
<a:CountryLookupCode>11</a:CountryLookupCode>
<a:CustomerNumber>117731304</a:CustomerNumber>
<a:IsDuplicate>false</a:IsDuplicate>
<a:ItemClassCode>OC002</a:ItemClassCode>
<a:LocalChannel>66</a:LocalChannel>
<a:MachineDescription>COMPELLENT SC8000,1st,2nd,UPG</a:MachineDescription>
<a:OrderNumber>NANANANAN</a:OrderNumber>
<a:ParentServiceTag i:nil="true"/>
<a:ServiceTag>XXXXXXX</a:ServiceTag>
<a:ShipDate>2014-02-07T00:00:00</a:ShipDate>
<a:Warranties>
<a:Warranty>
<a:EndDate>2016-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5366</a:ItemNumber>
<a:ServiceLevelCode>S9</a:ServiceLevelCode>
<a:ServiceLevelDescription>4 Hour On-Site Service</a:ServiceLevelDescription>
<a:ServiceLevelGroup>5</a:ServiceLevelGroup>
<a:ServiceProvider>UNY</a:ServiceProvider>
<a:StartDate>2015-07-31T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2016-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>974-9929</a:ItemNumber>
<a:ServiceLevelCode>ZL</a:ServiceLevelCode>
<a:ServiceLevelDescription>CML - Storage Center Core Base</a:ServiceLevelDescription>
<a:ServiceLevelGroup>11</a:ServiceLevelGroup>
<a:ServiceProvider>DELL</a:ServiceProvider>
<a:StartDate>2015-07-31T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2016-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5406</a:ItemNumber>
<a:ServiceLevelCode>SV</a:ServiceLevelCode>
<a:ServiceLevelDescription>Silver Premium Support</a:ServiceLevelDescription>
<a:ServiceLevelGroup>8</a:ServiceLevelGroup>
<a:ServiceProvider>DELL</a:ServiceProvider>
<a:StartDate>2015-07-31T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2015-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5416</a:ItemNumber>
<a:ServiceLevelCode>SV</a:ServiceLevelCode>
<a:ServiceLevelDescription>Silver Premium Support</a:ServiceLevelDescription>
<a:ServiceLevelGroup>8</a:ServiceLevelGroup>
<a:ServiceProvider>DELL</a:ServiceProvider>
<a:StartDate>2014-08-01T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2015-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5376</a:ItemNumber>
<a:ServiceLevelCode>S9</a:ServiceLevelCode>
<a:ServiceLevelDescription>4 Hour On-Site Service</a:ServiceLevelDescription>
<a:ServiceLevelGroup>5</a:ServiceLevelGroup>
<a:ServiceProvider>UNY</a:ServiceProvider>
<a:StartDate>2014-08-01T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2015-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>975-0042</a:ItemNumber>
<a:ServiceLevelCode>ZL</a:ServiceLevelCode>
<a:ServiceLevelDescription>CML - Storage Center Core Base</a:ServiceLevelDescription>
<a:ServiceLevelGroup>11</a:ServiceLevelGroup>
<a:ServiceProvider>DELL</a:ServiceProvider>
<a:StartDate>2014-08-01T00:00:00</a:StartDate></a:Warranty>
<a:Warranty>
<a:EndDate>2014-07-31T23:59:59</a:EndDate>
<a:EntitlementType>EXTENDED</a:EntitlementType>
<a:ItemNumber>933-5376</a:ItemNumber>
<a:ServiceLevelCode>S9</a:ServiceLevelCode>
<a:ServiceLevelDescription>4 Hour On-Site Service</a:ServiceLevelDescription>
<a:ServiceLevelGroup>5</a:ServiceLevelGroup>
<a:ServiceProvider>UNY</a:ServiceProvider>
<a:StartDate>2014-02-07T00:00:00</a:StartDate></a:Warranty></a:Warranties></a:DellAsset></a:Response></GetAssetWarrantyResult></GetAssetWarrantyResponse>

The issue was with the class being created correctly. Here is the correction for anyone that is curious:
public class Device
{
private List<DeviceBase> _deviceInfo = new List<DeviceBase>();
private List<Warranty> _warrantyInfo = new List<Warranty>();
public List<DeviceBase> DeviceInfo
{
get
{ return _deviceInfo; }
set
{ _deviceInfo = value; }
}
public List<Warranty> WarrantyInfo
{
get
{ return _warrantyInfo; }
set
{ _warrantyInfo = value; }
}
}
public class DeviceBase
{
public string Product { get; set; }
public string OrderNumber { get; set; }
public string ServiceTag { get; set; }
public string ShipDate { get; set; }
}
public class Warranty
{
public string Service { get; set; }
public string Provider { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public string TypeOfWarranty { get; set; }
}

Related

Dapper returning null values for few objects

While mapping to the (queryasync)dapper few of the objects are returning null even though have value in database table.
Null is returning for PremiumCcy , ReportingClass2 , SyndEPI , SyndGrossEPI , SyndExposure , LimitCcyISO
I am really getting frustrated for solving this issue, already invested 3 days , any help is really appreciated over here.
My code :
var query = #$"
SELECT
p.PolicyRef ,
p.RenewedFromRef,
p.PolicyId ,
p.YOA as PolicyYOA,
p.PlacingType as PlacingType,
p.StatsCcyISO as PremiumCcy,
pl.SyndExposure,
pl.SyndEPI ,
pl.SyndGrossEPI,
repClass.ReportingClass2,
plimit.LimitCcyISO,
lsp.LineSizePct as LineSizePct,
(SELECT MAX(lastUpd)
FROM (
VALUES (p.LastUpd),
(pl.LastUpd),
(plimit.LastUpd),
(repClass.LastUpd)
) AS VALUE(lastUpd)
) as LastUpd
FROM Policy p
INNER JOIN PolicyLine pl
ON p.PolicyId = pl.PolicyId
LEFT JOIN ReportingClass repClass
ON p.YOA = repClass.PIMYear
AND repClass.Synd = pl.Synd
AND repClass.ProducingTeam = pl.ProducingTeam
AND repClass.Class1 = p.Class1
AND repClass.Class2 = p.Class2
AND repClass.Class3 = p.Class3
AND repClass.Class4 = p.Class4
LEFT JOIN PolicyLimit plimit
ON p.PolicyId = plimit.PolicyId
AND plimit.TopLimitInd = 'Y'
AND plimit.DelDate IS NULL
AND plimit.Limit IS NOT NULL
WHERE pl.Synd = '1234'
AND pl.LineStatus IN ('ABC', 'XYZ')
AND p.PlacingType <> 'DEC';
;";
var eclipsePolicy = await connection.QueryAsync<EclipsePolicy, PolicyLines, ReportingClass, PolicyLimit, LineSize, EclipsePolicy>(query,
(eclipsePolicy, policyLines, reportingClass, policyLimit, lineSize) =>
{
eclipsePolicy.PolicyLines = policyLines;
eclipsePolicy.ReportingClasses = reportingClass;
eclipsePolicy.PolicyLimits = policyLimit;
eclipsePolicy.LineSizes = lineSize;
return eclipsePolicy;
}, CommandType.Text, connection.BeginTransaction(), splitOn: "PremiumCcy,SyndExposure,ReportingClass2,LimitCcyISO");
Model I am using is
public class EclipsePolicy
{
public string PolicyRef { get; set; }
public string RenewedFromRef { get; set; }
public int PolicyId { get; set; }
public int PolicyYOA { get; set; }
public string PlacingType { get; set; }
public DateTime EclipseLastUpd { get; set; } ,
public PolicyLimit PolicyLimits { get; set; }
public string PremiumCcy { get; set; }
public ReportingClass ReportingClasses { get; set; }
public PolicyLines PolicyLines { get; set; }
public decimal Premium { get; set; }
public LineSize LineSizes { get; set; }
}
public class PolicyLines
{
public decimal SyndEPI { get; set; }
public decimal SyndGrossEPI { get; set; }
public string SyndExposure { get; set; }
}
public class ReportingClass
{
public string ReportingClass2 { get; set; }
}
public class PolicyLimit
{
public string LimitCcyISO { get; set; }
}
public class LineSize
{
public decimal LineSizePct { get; set; }
}
Better to handle mapping manually to see what's the problem like the following:
var result = await connection.QueryAsync<dynamic>(query);
return MapResult(result);
private dynamic MapResult(dynamic result)
{
var eclipsePolicy = new EclipsePolicy
{
EclipseLastUpd = result[0].EclipseLastUpd,
.
.
.
.
ReportingClasses = result[0].ReportingClasses
};
var eclipsePolicy = new PolicyLines
{
SyndEPI = result[0].SyndEPI,
.
.
.
.
SyndGrossEPI = result[0].SyndGrossEPI
};
return new {eclipsePolicy,eclipsePolicy ,...};
}

c# - Get data tree from XML file

I have an XML file which has many children:
<Docs>
<Doc>
<DocType>x</DocType>
<DocNumber>xxx</DocNumber>
<DocNumberSeries>xx</DocNumberSeries>
<DocDate>xxxx-xx-xx</DocDate>
<DocCause>
<Id>xx</Id>
<Code/>
<Name>xx</Name>
</DocCause>
<Anag>
<Name>NameCompany</Name>
<Address>xx</Address>
<ZipCode>xx</ZipCode>
<City>xx</City>
<Province>xx</Province>
<CountryCode>xx</CountryCode>
<PhoneNumber>xx</PhoneNumber>
<CellularNumber/>
<FaxNumber>xx</FaxNumber>
<EmailAddress>xx</EmailAddress>
<VatNumber>xx</VatNumber>
<PersonalID>xx</PersonalID>
</Anag>
<DestinationAddress>
<Name>xxL</Name>
<Address>xx</Address>
<ZipCode>xx</ZipCode>
<City>xx</City>
<Province>xx</Province>
<CountryCode>xx</CountryCode>
<PhoneNumber>xx</PhoneNumber>
<FaxNumber>xx</FaxNumber>
</DestinationAddress>
<Payment>
<Code/>
<Name>xx</Name>
</Payment>
<DocRows>
<DocRow>
<RowType>1</RowType>
<Product>
<Code>LSML1710</Code>
</Product>
<Description></Description>
<Quantity></Quantity>
<Price></Price>
<Tax>
<Tax>
<Id>xx</Id>
<Code/>
<Name>xx</Name>
<PercentAmount>xx</PercentAmount>
<IndPercentAmount>x</IndPercentAmount>
<FixedAmount>x</FixedAmount>
</Tax>
</Tax>
</DocRow>
</DocRows>
...
</Doc>
For example, one of the thing I want to do is to get the data inside tag Doc and then Anag.
At the moment I have these classes:
public class Anag
{
public string RagioneSociale { get; set; }
public string Indirizzo { get; set; }
public string CAP { get; set; }
public string Citta { get; set; }
public string Provincia { get; set; }
public string CodiceNazione { get; set; }
public string Telefono { get; set; }
public string Cellulare { get; set; }
public string Email { get; set; }
}
public class Doc
{
public string DocNumber { get; set; }
public Anag Anags { get; set; }
}
List<Doc> results = contentFile.Descendants("Doc").Select(doc => new Doc // var results = contentFile.Descendants("Doc").SelectMany(doc => doc.Descendants(doc.Name.Namespace + "Anag").Select(anag => new
{
DocNumber = (string)doc.Element(doc.Name.Namespace + "DocNumber"),
Anags = doc.Descendants(doc.Name.Namespace + "Anag").Select(anag => new Anag
{
RagioneSociale = (string)anag.Element(anag.Name.Namespace + "Name"),
Indirizzo = (string)anag.Element(anag.Name.Namespace + "Address"),
CAP = (string)anag.Element(anag.Name.Namespace + "ZipCode"),
Provincia = (string)anag.Element(anag.Name.Namespace + "Province"),
Telefono = (string)anag.Element(anag.Name.Namespace + "PhoneNumber"),
Cellulare = (string)anag.Element(anag.Name.Namespace + "CellularNumber"),
Email = (string)anag.Element(anag.Name.Namespace + "EmailAddress"),
Citta = (string)anag.Element(anag.Name.Namespace + "City")
}),
}).ToList();
This last code gives an error:
Cannot implicitly convert type system.collections.generic.IEnumerable<nameProject.Classes.Anag> in <NameProject.Classes.Anag>.
The final result I want is a List with the data and children of this XML:
List:
+ Doc=>
{DocNumber}
+ Anag => { Address .. , Name.. .. ) (not a list ! )
+ DestinationAddress => { Address.... Name..)
so that I can convert to a file .csv only .
Since you already went through the trouble of creating the classes, why not let the deserializer do the job for you?
Here's how:
XmlSerializer serializer = new XmlSerializer(typeof(Root));
using (var reader = new StreamReader(xmlFilePath))
{
List<Doc> docs = ((Root)serializer.Deserialize(reader)).Docs;
}
Now, you just have to add some tags into your class properties so that they match the xml names.
[Serializable, XmlRoot("Docs")]
public class Root
{
[XmlElement("Doc")]
public List<Doc> Docs { get; set; }
}
public class Doc
{
public string DocNumber { get; set; }
[XmlElement("Anag")]
public Anag Anags { get; set; }
}
public class Anag
{
[XmlElement("Name")]
public string RagioneSociale { get; set; }
[XmlElement("Address")]
public string Indirizzo { get; set; }
[XmlElement("ZipCode")]
public string CAP { get; set; }
[XmlElement("City")]
public string Citta { get; set; }
[XmlElement("Province")]
public string Provincia { get; set; }
[XmlElement("CountryCode")]
public string CodiceNazione { get; set; }
[XmlElement("PhoneNumber")]
public string Telefono { get; set; }
[XmlElement("CellularNumber")]
public string Cellulare { get; set; }
[XmlElement("EmailAddress")]
public string Email { get; set; }
}
Just try using the feature "Paste Special" to generate the classes for parsing. You will 100% not get an exception.
Crtl + A on the whole xml, copy to clipboard
Then create a new class
in your project Click "Edit" -> "Paste Special" -> "Paste XML as
classes"
Change the parsing to use type of the "RootNode" in the
pasted class
Build and try it out.

Elasticsearch null pointer when indexing parent document

I am attempting to set up a parent-child relationship in Elasticsearch, and I am consistently getting a null pointer exception from the server when trying to index the parent.
I am using Elasticsearch and NEST both at version 6.2.
I've basically been following this guide from the documentation: https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/parent-child-relationships.html
Objects: (ESDocument is the base class, ESVendor is the parent, and ESLocation is the child)
[ElasticsearchType(Name = "Doc", IdProperty = "Id")]
public abstract class ESDocument
{
public int Id { get; set; }
public JoinField MyJoinField { get; set; }
}
[ElasticsearchType(Name = "Vendor", IdProperty = "ConsigneeID")]
public class ESVendor: ESDocument
{
public int VendorID { get; set; }
public int VendorTypeID { get; set; }
public int ClientID { get; set; }
public int CompanyID { get; private set; }
public int LocationID { get; set; }
public bool Active { get; set; }
public TimeSpan? OpenTime { get; set; }
public TimeSpan? CloseTime { get; set; }
public bool AppointmentRequired { get; set; }
public decimal? Latitude { get; set; }
public decimal? Longitude { get; set; }
public bool PositionRequested { get; set; }
public bool PositionFailed { get; set; }
public int? NoteID { get; set; }
public int? OldID { get; set; }
public ESVendor(Vendor vendor)
{
if (vendor == null)
{
return;
}
Id = vendor.VendorID;
CompanyID = vendor.CompanyID;
Active = vendor.Active;
VendorID = vendor.VendorID;
Latitude = vendor.Latitude;
Longitude = vendor.Longitude;
VendorTypeID = vendor.VendorTypeID;
ClientID = vendor.ClientID;
LocationID = vendor.LocationID;
Latitude = vendor.Latitude;
OpenTime = vendor.OpenTime;
CloseTime = vendor.CloseTime;
AppointmentRequired = vendor.AppointmentRequired;
PositionRequested = vendor.PositionRequested;
PositionFailed = vendor.PositionFailed;
NoteID = vendor.NoteID;
OldID = vendor.OldID;
}
public ESVendor()
{
}
}
[ElasticsearchType(Name = "Location", IdProperty = "LocationID")]
public class ESLocation: ESDocument
{
public int LocationID { get; set; }
public int CompanyID { get; private set; }
public string Name { get; set; }
public string Address { get; set; }
public string Address2 { get; set; }
public string CityStateZipCountry { get; set; }
public string Note { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
public ESLocation(Location location)
{
if (location == null)
{
return;
}
Id = location.LocationID;
CompanyID = location.CompanyID;
Address = location.Address;
Address2 = location.Address2;
LocationID = location.LocationID;
Name = location.Name;
Note = location.Note;
City = location.City;
State = location.State;
Zip = location.Zip;
Country = location.Country;
CityStateZipCountry = location.Country + " " + location.State + " " + location.Zip + " " + location.Country;
}
public ESLocation()
{
}
}
Mapping + Indexing:
StaticConnectionPool connectionPool = new StaticConnectionPool(_nodes);
ConnectionSettings connectionSettings = new ConnectionSettings(connectionPool, sourceSerializer: SourceSerializer)
.DisableDirectStreaming()
.DefaultMappingFor<ESDocument>(m => m.IndexName("vendors").TypeName("doc"))
.DefaultMappingFor<ESVendor>(m => m.IndexName("vendors").TypeName("doc").RelationName("Vendor_Location"))
.DefaultMappingFor<ESLocation>(m => m.IndexName("vendors").TypeName("doc"));
ElasticClient esClient = new ElasticClient(connectionSettings);
IExistsResponse existsResponse = await esClient.IndexExistsAsync(new IndexExistsRequest(Indices.Parse("vendors")));
if (!existsResponse.Exists)
{
esClient.CreateIndex("vendors", c => c
.Index<ESVendor>()
.Mappings(ms => ms
.Map<ESVendor>(m => m
.RoutingField(r => r.Required())
.AutoMap<ESVendor>()
.AutoMap<ESLocation>()
.Properties(props => props
.Join(j => j
.Name(p => p.MyJoinField)
.Relations(r => r
.Join<ESVendor, ESLocation>()
)
)
)
)
));
}
using (Entities dbContext = new Entities())
{
foreach (Vendor vendor in dbContext.Vendors)
{
if (!(await esClient.DocumentExistsAsync(new DocumentExistsRequest("vendors", typeof (Vendor), vendor.VendorID))).Exists)
{
ESVendor parent = new ESVendor(vendor)
{
MyJoinField = JoinField.Root<ESVendor>()
};
var result = esClient.IndexDocument<ESDocument>(parent);
ESLocation child = new ESLocation(vendor.Location)
{
MyJoinField = JoinField.Link<ESLocation, ESVendor>(parent)
};
result = esClient.IndexDocument<ESDocument>(child);
}
}
}
It is consistently returning the below error every time it attempts to index the parent document.
Invalid NEST response built from a unsuccessful low level call on POST: /vendors/doc
# Audit trail of this API call:
- [1] BadResponse: Node: http://192.168.50.240:9200/ Took: 00:00:00.2060485
# OriginalException: Elasticsearch.Net.ElasticsearchClientException: The remote server returned an error: (500) Internal Server Error.. Call: Status code 500 from: POST /vendors/doc. ServerError: Type: null_pointer_exception Reason: "id must not be null" ---> System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
at System.Net.HttpWebRequest.GetResponse()
at Elasticsearch.Net.HttpConnection.Request[TResponse](RequestData requestData)
--- End of inner exception stack trace ---
# Request:
{"vendorID":1,"vendorTypeID":1,"clientID":349,"companyID":1,"locationID":4994,"active":true,"openTime":null,"closeTime":null,"appointmentRequired":false,"latitude":null,"longitude":null,"positionRequested":false,"positionFailed":false,"noteID":null,"oldID":1626,"id":1,"myJoinField":"Vendor_Location"}
# Response:
{"error":{"root_cause":[{"type":"null_pointer_exception","reason":"id must not be null"}],"type":"null_pointer_exception","reason":"id must not be null"},"status":500}
I've been fighting this for days now when it should have been a 30 minute task, I'm at my wits end.
Thank you in advance, your assistance is very much appreciated!
The problem is the attribute applied to EsVendor
[ElasticsearchType(Name = "Vendor", IdProperty = "ConsigneeID")]
public class ESVendor: ESDocument
{
// ...
}
The IdProperty tells NEST to infer the id for EsVendor types from the ConsigneeID property, but looking at the model provided, there is no property with that name. It looks like you don't need to specify this property in the attribute, and instead can rely on IdProperty in the ElasticsearchTypeAttribute applied to ESDocument, which will infer the Id from the Id property.
So, removing IdProperty from the attribute applied to EsVendor
[ElasticsearchType(Name = "Vendor")]
public class ESVendor : ESDocument
{
// ...
}
will now send an index request similar to (other properties skipped for brevity) for an EsVendor
PUT http://localhost:9200/vendors/doc/1?routing=1
{
"id": 1,
"vendorID": 1,
"myJoinField": "Vendor_Location"
}
We might also want to lean on the compiler too, for string values that reference properties, and use nameof(Id)
[ElasticsearchType(Name = "Doc", IdProperty = nameof(Id))]
public abstract class ESDocument
{
public int Id { get; set; }
public JoinField MyJoinField { get; set; }
}
We can in fact remove IdProperty altogether if we want, as using the Id property to infer an ID for the document is default NEST behaviour. Going further, because the types have a DefaultMappingFor<T> on ConnectionSettings, we can remove the ElasticsearchType attributes altogether, since the default mappings on connection settings will take precedence.

What is the maximum size of an object to store in a MongoDb collection using the C# Driver 2.1.1?

I use the mongoDB C# driver 2.1.1 to store and retrieve documents in a mongoDb collection. This worked well so far until one document was CORRECTLY stored but could not be read back by the driver.
I got to that conclusion using robomongo (a user interface to monitor what's inside my collection) and manipulating the object stored.
It contains a collection of elements (around 4000 sub elements) and if I remove enough of it, the document is finally retrievable again.
The line of code used is:
public Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
return MongoQueryable.FirstOrDefaultAsync(_collection, predicate, cancellationToken);
}
Before you ask, what I first thought is that one specific element of my sub elements set had encountered some issues while being serialized/deserialized and was creating the issue. I tested by removing randomly elements of the collection, and just the number seems to be the root of the problem.
The text file size of the Json document represents around 11 Mb - I will try now to get the actual size of the object which would be more relevant.
Something else I can add is that in the mondoDb log file, there is one line that only appear when I try to select an "oversized" document :
2016-06-29T19:30:45.982+0200 I COMMAND [conn12] command MYCOLLECTIONDb.$cmd command: aggregate
{ aggregate: "XXXX", pipeline: [ { $match: { _id: "5773e0e9a1152d259c7d2e50" } }, { $limit: 1 } ]
, cursor: {} } keyUpdates:0 writeConflicts:0 numYields:0 reslen:4188200 locks:{ Global:
{ acquireCount: { r: 6 } }, MMAPV1Journal: { acquireCount:
{ r: 3 } }, Database: { acquireCount: { r: 3 } }, Collection: { acquireCount: { R: 3 } } } 109ms
(XXXX being my custom object)
So I'd like to know if you have any idea of what's causing this issue, any hint of where I should try to look, because I am running out of ideas :)
EDIT_1: That is basically the structure of my object :
[DataContract]
public class MyClass
{
[DataMember]
public string _id { get; set; }
[DataMember]
public string XmlPreference { get; set; }
[DataMember]
public string XmlMarketDates { get; set; }
[DataMember]
public IEnumerable<ClassB> Lines { get; set; }
}
[DataContract]
public class ClassB
{
[DataMember]
public string UniqueId { get; set; }
[DataMember]
public string ParentId { get; set; }
[DataMember]
public Dictionary<string, ClassC> DataMapping {get; set;}
}
and ClassC is just a container of 7 ints and 1 object properties.
Still looking for something :)
EDIT 2: I reproduced the bug only using the mongoDb C# drivers 2.1.1 release version. Here is the code :
using MongoDB.Bson;
using MongoDB.Driver.Linq;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace TestingMongoDbCsharpDriver
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Debugging starting...");
try
{
MongoDB.Driver.MongoClient myClient = new MongoDB.Driver.MongoClient("mongodb://localhost:27010");
var db = myClient.GetDatabase("DocumentStoreDb");
var collection = db.GetCollection<DocumentModel>("TestOverload");
Console.WriteLine("Collection TestOverload found");
Random rand = new Random(999999999);
DocumentModel toInsert = null;
for (int lineNb = 1; lineNb < 50000; lineNb += 1000)
{
string id = rand.Next().ToString();
Console.WriteLine(string.Format("\nCreating document with id '{0}' and {1} lines...", id, lineNb));
toInsert = Funcs.Create(id, lineNb);
Console.WriteLine("Created.");
// Saving and waiting for it to finish
collection.InsertOneAsync(toInsert).Wait();
Console.WriteLine("Inserted.");
// retrieving it
var filter = new BsonDocument("_id", new BsonDocument("$eq", id));
var cursor = collection.FindAsync<DocumentModel>(filter).Result;
cursor.MoveNextAsync().Wait();
string messFilterMethod = cursor.Current.Count() == 1 ? "Retrieved" : "Not retrieved. Bug confirmed";
Console.WriteLine("With Filter: " + messFilterMethod);
var model = MongoQueryable.FirstOrDefaultAsync(collection.AsQueryable(), e => e._id == id).Result;
string mess = model != null ? "Retrieved" : "Not retrieved. Bug confirmed";
Console.WriteLine("With AsQueryable: " + mess);
// Removing it
collection.DeleteOneAsync(filter).Wait();
Console.WriteLine("Deleted.\n");
Console.ReadKey();
}
}
catch (Exception e)
{
Console.WriteLine("\n\nERROR: " + e.Message);
}
Console.WriteLine("\n\n**************** NO BUG *************\n");
}
}
public static class Funcs
{
public static DocumentModel Create(string uniqueId, int nbLines)
{
Random random = new Random(2000000);
List<MyDocumentSubElement> listOk = new List<MyDocumentSubElement>();
for (int lines = 0; lines < nbLines; ++lines)
{
Dictionary<string, SnapCellValueStyle> newDico = new Dictionary<string, SnapCellValueStyle>();
for (int i = 0; i < 10; ++i)
{
int idSnap = random.Next();
var snap = new SnapCellValueStyle()
{
alignment = idSnap,
Value = "okkkkkkkkkkzzzzkk"
};
newDico.Add("newKey_" + idSnap.ToString(), snap);
}
MyDocumentSubElement doc = new MyDocumentSubElement()
{
Icon = 516,
Name = "name du truc",
ParentId = "parent id",
type = SubElementType.T3,
UniqueId = "uniqueId_" + random.Next().ToString(),
MapColumnNameToCellValue = newDico
};
listOk.Add(doc);
}
int headerId = random.Next();
MyDocumentHeader temp = new MyDocumentHeader()
{
Comment = "comment",
Date = DateTime.Now,
ExtractionId = headerId,
Id = "id ok _ " + headerId,
Name = "Name really interesting name",
OwnerId = 95115,
RootFolioId = 51,
SnapshotViewId = MyDocumentType.Type2
};
DocumentModel toInsert = new DocumentModel()
{
_id = uniqueId,
Header = temp,
XmlMarketDates = "<xmlPrefok65464f6szf65ze4f6d2f1ergers5fvefref3e4f05e4f064z68f4xd35f8eszf40s6e40f68z4f0e8511xf340ed53f1d51zf68d4z61ef644dcdce4f64zef84zOKok>><>>",
XmlPreference = "<<zefaiunzhduiaopklzpdpakzdplapdergergfdgergekâzda4684z16ad84s2dd0486za04d68a04z8d0s1d d4az80d46az4d651s1d8 154efze40f6 4ze65f40 65ze40f6z4e>><>>>",
Lines = listOk
};
return toInsert;
}
}
// Imitation of SnapshotDocModel
[DataContract]
public class DocumentModel
{
[DataMember]
public string _id { get; set; }
[DataMember]
public MyDocumentHeader Header { get; set; }
[DataMember]
public string XmlPreference { get; set; }
[DataMember]
public string XmlMarketDates { get; set; }
[DataMember]
public IEnumerable<MyDocumentSubElement> Lines { get; set; }
}
[DataContract]
public class MyDocumentHeader
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int OwnerId { get; set; }
[DataMember]
public string Comment { get; set; }
[DataMember]
public DateTime Date { get; set; }
[DataMember]
public int RootFolioId { get; set; }
[DataMember]
public MyDocumentType SnapshotViewId { get; set; }
[DataMember]
public int ExtractionId { get; set; }
}
[DataContract]
public class MyDocumentSubElement
{
[DataMember]
public string UniqueId { get; set; }
[DataMember]
public string ParentId { get; set; }
[DataMember]
public int Icon { get; set; }
[DataMember]
public SubElementType type { get; set; }
[DataMember]
public Dictionary<string, SnapCellValueStyle> MapColumnNameToCellValue { get; set; }
[DataMember]
public string Name { get; set; }
}
public class SnapCellValueStyle
{
public object Value { get; set; }
public int alignment { get; set; }
public int Red { get; set; }
public int Green { get; set; }
public int Blue { get; set; }
public int currency { get; set; }
public int decimalPoint { get; set; }
public int kind { get; set; }
public int style { get; set; }
}
#region enum
public enum MyDocumentType
{
Undefined,
Type1,
Type2,
Type3,
Type4,
Type5
}
public enum SubElementType
{
T1,
T2,
T3
}
#endregion
}
If you test it, you will see that there is the point where the AsQueryable method does not work anymore where as the document is still retrievable by the method using a filter.
Problem fixed in the version 2.2.4 of the c# mongo db driver (probably with the bug https://jira.mongodb.org/browse/CSHARP-1645)
Thanks :)

Xml Deserialize returms null values but xml has values

I saw few topics but no one looks like my problem.
Here is my class:
namespace Framework.Cielo.Models
{
[XmlRoot("transacao", Namespace = "http://ecommerce.cbmp.com.br")]
public class TransactionResponse
{
[XmlAttribute("id")]
public string ID { get; set; }
[XmlAttribute("versao")]
public string Version { get; set; }
[XmlElement("tid")]
public string tid { get; set; }
[XmlElement("pan")]
public string pan { get; set; }
[XmlElement("dados-pedido")]
public EstablishmentOrder Order { get; set; }
[XmlElement("forma-pagamento")]
public PaymentMethod PaymentMethod { get; set; }
[XmlElement("status")]
public TransactionStatusEnum Status { get; set; }
[XmlElement("url-retorno")]
public string ReturnUrl { get; set; }
[XmlElement("autenticacao")]
public Authentication Authentication { get; set; }
}
}
and here is the authentication class
namespace Framework.Cielo.Models
{
[XmlRoot("autenticacao")]
public class Authentication
{
[XmlElement("codigo")]
public int Code { get; set; }
[XmlElement("mensagem")]
public string Message { get; set; }
[XmlIgnore]
public DateTime Date { get; set; }
[XmlElement("data-hora")]
public string FormattedDate
{
get
{
return Date.ToString("yyyy-MM-ddTHH:mm:ss");
}
set
{
DateTime kdc = DateTime.MinValue;
DateTime.TryParse(value, out kdc);
Date = kdc;
}
}
[XmlElement("valor")]
public int Value { get; set; }
[XmlElement("lr")]
public int SecurityLevel { get; set; }
[XmlElement("arp")]
public object arp { get; set; }
[XmlElement("nsu")]
public object nsu { get; set; }
}
}
Here is how I deserialize:
string serializado = File.ReadAllText("req.xml");
var stringReader = new System.IO.StringReader(serializado);
var serializer = new XmlSerializer(typeof(TransactionResponse));
TransactionResponse preAuthResponse = serializer.Deserialize(stringReader) as TransactionResponse;
and here is my XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<transacao versao="1.3.0" id="100" xmlns="http://ecommerce.cbmp.com.br">
<tid>10069930690A16DF1001</tid>
<pan>b1SQ6jpKCDt3n9C0dgD/ZkPQ1Bh+7aJESqr/CwP64P0=</pan>
<dados-pedido>
<numero>100</numero>
<valor>29900</valor>
<moeda>986</moeda>
<data-hora>2013-10-15T00:57:19.032-03:00</data-hora>
<descricao/>
<idioma>PT</idioma>
<taxa-embarque>0</taxa-embarque>
</dados-pedido>
<forma-pagamento>
<bandeira>mastercard</bandeira>
<produto>1</produto>
<parcelas>1</parcelas>
</forma-pagamento>
<status>4</status>
<autenticacao>
<codigo>4</codigo>
<mensagem>Transacao sem autenticacao</mensagem>
<data-hora>2013-10-15T00:57:19.037-03:00</data-hora>
<valor>29900</valor>
<eci>0</eci>
</autenticacao>
<autorizacao>
<codigo>4</codigo>
<mensagem>Transação autorizada</mensagem>
<data-hora>2013-10-15T00:57:19.041-03:00</data-hora>
<valor>29900</valor>
<lr>00</lr>
<arp>123456</arp>
<nsu>661215</nsu>
</autorizacao>
</transacao>
When I run this code, all the elements get right, but not ARP and NSU elements (the last 2 of autorizacao tag)
I really don't know why. This XML comes from a web service and I can't figure out why my deserialize don't work with the 2 last items but works greater with any other element.
I have tried with your code and updated following and it works.
Commented second last <autenticacao> tag.
Rename last tag <autorizacao> to <autenticacao> . May be you are getting wrong xml & last two tags are confusing so I have tried with only one tag.
In Authentication class I have changed type of ARP and NSU otherwise we are getting XMlNode type while deserializing. You can also use string instead of int.
[XmlElement("arp")]
public int arp { get; set; }
[XmlElement("nsu")]
public int nsu { get; set; }

Categories