Detecting Debug mode on Net Core

To detect if the Assembly is on Debug mode: Using reflection bool debugMode = false; // Detect debug mode if (attribs.Length > 0) { var debuggableAttribute = attribs[0] as DebuggableAttribute; if (debuggableAttribute != null) { debugMode = debuggableAttribute.IsJITOptimizerDisabled ? true : false; } } Using csproj constants Another solution is to use DEBUG constants, so … Continue reading Detecting Debug mode on Net Core

C# (Net Core) – Display Friendly Name for Enumerator

To display a friendly name for enumerator element, we can use the Description annotation (we need to include the System.ComponentModel  namespace): using System.ComponentModel; public enum MyEnum { [Description("Success")] Success = 0, [Description("Error")] Error = 1 } Now we can use the normal  ToString()  to obtain the friendly name of the enumerator: MyEnum.Success.Success.ToString()

C#: create proxy class from WSDL

If we have a WSDL file and we want to create the C# proxy class we can use the svcutil.exe (Windows SDK ServiceModel Metadata Utility Tool). Suppose we have saved the WSDL file InteropService.wsdl into the folder C:\Tmp. We can execute this command to create the C# proxy class: "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.2 Tools\svcutil.exe" … Continue reading C#: create proxy class from WSDL

C# – Remove UtcOffset from DateTime

DateTime contains UTC format informations. To remove UTC informations we need to create an instance of DateTime using its Ticks property. For example: // DateTime that contains UTC info DateTime date = DateTime.Now; date.ToString("o"); // Output "2018-12-07T15:58:01.4217891+01:00" // DateTime without UTC info date = new DateTime(date.Ticks); date.ToString("o"); // Output "2018-12-07T15:58:01.4217891"

C#: how to convert a String to DateTime ignoring TimeZone

To convert a String into a DateTime we can use the DateTime.Parse method. For example: DateTime myDt = DateTime.Parse("2018-10-02 11:25:27.860"); Now the myDt contains a date with 02/10/2018 11:25:27 value (on italian format). But if our string date include TimeZone informations, if we use DateTime.Parse method, we get a wrong value: DateTime myDt = DateTime.Parse("2018-10-02T11:25:27.860Z"); Now … Continue reading C#: how to convert a String to DateTime ignoring TimeZone

C#: how to validate XML with a set of XSD schemas

When we are using an XSD schema to validate an XML document, if this schema has other nested XSD schemas, if we have a myCustomType declared into the nested XSD and we don't use XmlSchemaSet to validating the XML, we can get the error: Type 'myCustomType' is not declared For example, if we have the myCustomType defined … Continue reading C#: how to validate XML with a set of XSD schemas

Asp Net Core 2 – Angular: download file from Controller

If we have an Angular client app that needs to download a file (for example a PDF file) from an Asp Net Core 2 api Controller , we can follow what is described in this post. The first implementation is on the server side (an Asp Net Core 2 Controller). So, for example into our controller UserController, … Continue reading Asp Net Core 2 – Angular: download file from Controller

C#: get anonimous properties length

To get the number of the properties contained by an objects collection (even Anonymous) like the following: var data = new [] { new { Name="Ram", Email="ram@techbrij.com", Phone="111-222-3333" }, new { Name="Shyam", Email="shyam@techbrij.com", Phone="159-222-1596" }, new { Name="Mohan", Email="mohan@techbrij.com", Phone="456-222-4569" }, new { Name="Sohan", Email="sohan@techbrij.com", Phone="789-456-3333" }, new { Name="Karan", Email="karan@techbrij.com", Phone="111-222-1234" }, new { Name="Brij", … Continue reading C#: get anonimous properties length

LINQ – IEnumerable .Count() threw an exception of type ‘System.NullReferenceException’

When we execute a LINQ query to an objects collection, LINQ uses deferred execution and the actual LINQ query will not be executed until we call .Count(), .ToList(), etc. (this can be checked looking the SQL command logs executed by the LINQ to SQL, following the instructions given in this post). If our objects collection contains … Continue reading LINQ – IEnumerable .Count() threw an exception of type ‘System.NullReferenceException’

Net Core 2.0: resolve error “The source IQueryable doesn’t implement IAsyncEnumerable. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations.”.

Because ToListAsync() works on a IQueryable<T> only, when you turned it in to a IEnumerable<T> via AsEnumerable() you lost the ability to call it. So wath appen if you try to call .AsQueryable()ToListAsync() on a IEnumerable<T> sequence like in the following example? public class SampleController { public class PeopleType { public string Name { get; set; } … Continue reading Net Core 2.0: resolve error “The source IQueryable doesn’t implement IAsyncEnumerable. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations.”.

Linq: resolve error ‘Cannot implicitly convert type System.Linq.IQueryable to System.Threading.Tasks.Task<System.Linq.IQueryable>

When we write a Linq method that return an IQueryable object collection like this: private Task<IQueryable<SampleType>> GetSampleType (IQueriable sampleCollection) { // A simple Linq query var result = from r in sampleCollection where (r.Type = 1) select new SampleType { Name = r.Name, Type = r.Type }; return result; } We get this error: Cannot … Continue reading Linq: resolve error ‘Cannot implicitly convert type System.Linq.IQueryable to System.Threading.Tasks.Task<System.Linq.IQueryable>

C#: get all types that implement an interface with reflection

Using reflection, from C# 3.0 and above, is possible to get all types that implement an interface. For example if the interface is IMyInterface: var type = typeof(IMyInterface); var types = AppDomain.CurrentDomain.GetAssemblies()     .SelectMany(s => s.GetTypes())     .Where(p => type.IsAssignableFrom(p)); foreach (var type in types) {     //do stuff } Or another … Continue reading C#: get all types that implement an interface with reflection