Debugging one of the applications, I've noticed, that logs are never uploaded to the server and started to investigate why. Thing is, that someone wrote code like this:

public const string TimestampFormat = "MM/dd/yyyy HH:mm:ss"

This was used to produce file name and expect / to be in formatted date, then as long as it is not allowed symbol in file name, silently remove / symbol from time stamp string, then use wildcard to find files matching that string.

Things is, that .net framework handles "/" in date format this way:

case '/':
                    result.Append(dtfi.DateSeparator);
                    tokenLen = 1;
                    break;

and DateSeparator for various cultures might be different symbol (dot in case of Lithuanian regional settings), you would expect it not being overridden though, as you explicitly define format.

In this case it was best to enforce usage of Invariant culture in all places, where we expect date time format to be regional settings agnostic.

Comments c# 

So in my last post I've shown very simple way to filter fields, that should not be shown in documentation. In real application we're using something more complicated, as we have different documentations for different consumers.

Now the problems with previous native approach was, that:
- it didn't allowed variations between different API versions - it didn't removed unused schemas, so in documentation it might be not visible, but in REST client generation using tools like nswag it might be annoying.

So to attack first problem, I've implemented to do it in two passes. The first pass would mark which properties need to be hidden and second pass would actually hide them. This was needed, because in SchemaFilter I didn't had ApiDescription, so I could not access ActionDescriptor to see in which API versions type is used and in OperationFilter I didn't had Type information, so I would need to bind schemas to types and I wished to avoid that.

First I've created attribute for marking fields to be hidden:

public class HideInDocumentationAttribute : Attribute
{
    private readonly string[] apiVersions;

    public HideInDocumentationAttribute(params string[] apiVersions)
    {
        this.apiVersions = apiVersions;
    }

    public string[] ApiVersions
    {
        get { return apiVersions; }
    }
}

And then I can created Schema Filter:

 public class EicsSchemaFilter : ISchemaFilter
{
    public const string HiddenInVersions = "hiddenInVersions";

    public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
    {
        var hiddenProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
            .Where(x => x.GetCustomAttribute<HideInDocumentationAttribute>() != null)
            .ToDictionary(x => x.Name.ToLowerFirstLetter(), x => x.GetCustomAttribute<HideInDocumentationAttribute>().ApiVersions);                

        var machingProperties = schema.properties
            .Where(x => hiddenProperties.ContainsKey(x.Key))
            .ToList();

        foreach (var machingProperty in machingProperties)
        {
            machingProperty.Value.vendorExtensions.Add(HiddenInVersions, hiddenProperties[machingProperty.Key]);
        }            
    }
}

What it does is it goes through each processed type properties and marks them as hidden in vendor extensions.

And the second pass is performed in OperationFilter, main idea looks like this:

 private static void HideFieldDocumentation(SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        var operationVersions = apiDescription.ActionDescriptor.GetCustomAttributes<ApiVersionsAttribute>()
            .SelectMany(x => x.Versions)
            .ToList();

        foreach (var schema in schemaRegistry.Definitions)
        {
            var propertiesToHide = schema.Value.properties
                .Where(x => x.Value.vendorExtensions.ContainsKey(EicsSchemaFilter.HiddenInVersions))
                .ToDictionary(x => x.Key, x => x.Value);

            var propertiesToHideInVersion = propertiesToHide
                .Where(x => IsFieldHiddenInVersion(x, operationVersions))
                .Select(x => x.Key)
                .ToList();

            foreach (var propertyToHide in propertiesToHide)
            {
                propertyToHide.Value.vendorExtensions.Remove(EicsSchemaFilter.HiddenInVersions);
            }

            schema.Value.properties = schema.Value.properties
                .Where(x => !propertiesToHideInVersion.Contains(x.Key))
                .ToDictionary(x => x.Key, x => x.Value);
        }
    }

And the last part is for removing unused definitions, performed in DocumentFilter:

public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, IApiExplorer apiExplorer)
    {           
        var usedTypes = new List<string>();

        foreach (var pathItem in swaggerDoc.paths.Values)
        {
            if (pathItem.post == null)
            {
                continue;
            }

            usedTypes.AddRange(pathItem.post.responses.Values
                .Where(x => x.schema != null)
                .Select(x => GetType(x.schema)));

            if (pathItem.post.parameters != null)
            {
                usedTypes.AddRange(pathItem.post.parameters.Select(x => GetType(x.schema)));
            }
        }

        usedTypes = usedTypes
            .Where(x => x != null)
            .Distinct()
            .ToList();            

        foreach (var usedType in usedTypes.ToList())
        {
            AddUsedTypesRecursively(usedType, schemaRegistry, usedTypes);                
        }

        var responseTypes = new HashSet<string>(usedTypes.Where(x => x != null).Select(GetKeyFromRef));

        var unusedDefinitions = schemaRegistry.Definitions
            .Where(x => !responseTypes.Contains(x.Key))
            .ToList();

        foreach (var schema in unusedDefinitions)
        {
            schemaRegistry.Definitions.Remove(schema.Key);
        }
    }

    private string GetType(Schema schema)
    {
        if (schema.@ref == null && schema.type == "array")
        {
            return schema.items.@ref;
        }

        return schema.@ref;
    }

    private void AddUsedTypesRecursively(string usedType, SchemaRegistry schemaRegistry, List<string> types)
    {            
        var key = GetKeyFromRef(usedType);

        if (!schemaRegistry.Definitions.ContainsKey(key))
        {
            return;
        }

        var type = schemaRegistry.Definitions[key];

        if (types.Contains(usedType))
        {
            return;
        }

        types.Add(usedType);

        foreach (var property in type.properties)
        {
            var propertyType = GetType(property.Value);
            if (propertyType != null && !types.Contains(GetKeyFromRef(propertyType)))
            {
                AddUsedTypesRecursively(propertyType, schemaRegistry, types);
            }
        }
    }
Comments c#  webapi 

If you're creating public API, there's no question, that Swagger is one of the best ways to create documentation for it. If you're using WebApi, swashbuckle is one of the most popular libraries to use for creating API description in Swagger 2.0 format.

Sometimes, you're trying to reuse same API for public/internal use and some of the fields are not important to public consumers, so it would be better not to show them (don't forget though, that security through obscurity is not a way to secure stuff, so even if you hide them in documentation, make sure about safe usage on backend).

Currently to do so, you have two ways:

1) Use Obsolete attribute, which is not the ideal way, because it means something different (and in our sonar configuration usage is a major issue)
2) Write own attribute and use it in SchemaFilter:

public class HideInDocumentationAttribute : Attribute
{         
}

 public class PropertyFilter : ISchemaFilter
{
    public void Apply(Schema schema, SchemaRegistry schemaRegistry, Type type)
    {
        var hiddenProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public)
            .Where(x => x.GetCustomAttribute<HideInDocumentationAttribute>() != null)
            .Select(x => x.Name.ToLowerFirstLetter())
            .ToList();

        schema.properties = schema.properties
            .Where(x => !hiddenProperties.Contains(x.Key))
            .ToDictionary(x => x.Key, x => x.Value);          
    }
}   
Comments c#  webapi 

In one of my current projects, we're using own framework on top of WPF, so it uses in several places something like this:

Margin = (Thickness)thicknessConverter.ConvertFromString(marginAttribute);

And we have installs on hundreds machines and lately, it was not working on two machines, that are next to each other, by throwing something like following:

'30,10,10,10' string cannot be converted to Length.
Source: PresentationFramework; StackTrace:    
at System.Windows.LengthConverter.FromString(String s, CultureInfo cultureInfo)
at System.Windows.ThicknessConverter.FromString(String s, CultureInfo cultureInfo)

First it was - it sure does not look like length, why it fails only on those couple PC's?

Luckily enough, stack trace gave hints right away - it shows CultureInfo parameter, although it was not used from code. I've looked at the source code of ThicknessConverter to find, that it uses TokenizerHelper class, which has following logic:

 internal static char GetNumericListSeparator(IFormatProvider provider)
 {
  char ch = ',';
  NumberFormatInfo instance = NumberFormatInfo.GetInstance(provider);
  if (instance.NumberDecimalSeparator.Length > 0 &&
     (int) ch == (int) instance.NumberDecimalSeparator[0])
    ch = ';';
  return ch;
 }

Which means, that if in Region settings NumberDecimalSeparator is comma (that is the case for Lithuanian region, I'm sure there are more regions like that), it uses semicolon to parse Thickness.

Solution is simple - pass CultureInfo.InvariantCulture as parameter to ConvertFromString method.

Comments wpf  c# 

This post is more like a reminder to my self, so there's not too much original stuff.

There's an easy way to see, what currently IIS is doing by using following line in cmd:

C:\Windows\System32\inetsrv>appcmd list requests

That will list all currently executing requests on the machine.

More examples:

Find all currently executing requests in the worker process "3897":

appcmd list requests /wp.name:3897

Find all currently executing requests associated with the application pool "DefaultAppPool":

appcmd list requests /apppool.name:DefaultAppPool

Find all currently executing requests associated with the site "Default Web Site":

appcmd list requests /site.name:"Default Web Site"

Find all longer than 3 seconds running requests:

appcmd list requests /elapsed:3000

Comments iis 

We're evaluating CMS platform for using it in our projects - BetterCMS. Overall it is nice CMS, just API is quite weak so far, especially client side API.

What we needed is to add custom plugin to HTML editor they are using - CKEditor. Problem was, that you need to pass plugin configuration to CKEditor before it creates instance, after instance is created, your plugin won't be registered. I was searching for a way to do it for quite some time, as long as there was no API event, that would fire before CKEditor instance will be created, there was no direct way to override BetterCMS configuration and if I would configure CKEditor very early, better CMS would overwrite that configuration with its own.

Solution turns out is pretty simple, when you start to think in javascript:

  // CKEDITOR.replace is a function, that creates editor instance
  // so we save reference to original function
  var replaceFunction = CKEDITOR.replace;

  // then override it 
  CKEDITOR.replace = function (elementOrIdOrName, config) {

            // configure as we wish
            // ...
            // finally run original function
            return replaceFunction(elementOrIdOrName, config);
  };
Comments javascript  BetterCMS 

Distance calculation between two points on the map is not as straightforward as you would think, here approximation in C#, using Equirectangular approximation, which is good if you need performance loosing some of the accuracy:

 public static double RadianConversion = Math.PI / 180;

 public static double GetDistanceInKilometers(this IGeoCoords fromPoint, IGeoCoords toPoint)
    {
        if (fromPoint == null || toPoint == null)
        {
            return 0;
        }

        var long2 = (double)fromPoint.Longitude * RadianConversion;
        var long1 = (double)toPoint.Longitude * RadianConversion;
        var lat2 = (double)fromPoint.Latitude * RadianConversion;
        var lat1 = (double)toPoint.Latitude * RadianConversion;

        // The formula is an approximation           
        // Accuracy is somewhat complex: along meridians there are no errors, otherwise they depend on distance, bearing, and latitude, but are small enough for many purposes.
        var x = (long2 - long1) * Math.Cos((lat1 + lat2) / 2d);
        var y = lat2 - lat1;
        var d = Math.Sqrt((x * x) + (y * y)) * 6371; // 6371 - earth radius in km

        return d;
    }         
Comments c#  location 

At work, I have many cases, when in deep object hierarchy I need to get value from some property:

var name = characteristics.Materials.MainMaterial.Name;

Problem is, that in many cases I'm not sure, that higher level objects like Materials or MainMaterial in given example have value, so I need to check that to avoid NullReferenceException:

var name = characteristics.Materials == null ? 
                           null : 
                           (characteristics.Materials.MainMaterial == null ? 
                                            null : 
                                            characteristics.Materials.MainMaterial.Name);

That would be quite nasty line, or if I would convert it to if sentences, it still would be ugly. So for such cases we have following extension method:

public static TResult Access<TSource, TResult>(
                        this TSource obj, 
                        Func<TSource, TResult> expression, 
                        TResult defaultValue = default(TResult))
{
     if (obj == null)
     {
         return defaultValue;
     }

     return expression.Invoke(obj);
}

Using this extension, we can rewrite our name access like this:

var name = characteristics.Materials.Access(x => x.MainMaterial.Access(y => y.name));
Comments c# 

We're using Castle.Windsor in our projects as dependency injection library and it is great. The main drawback in using any IoC is that it brings some obscurity and if project is big, IoC configuration is complex - finding IoC configuration bugs may be a real headache, because they will tend to hide themselves under various exceptions, hard to understand situations.
Yesterday I was fighting a bug that was causing Object Disposed exception and it was driving me mad. I was able to find it only after I've stuffed dependent services with Logging and Guid's of object instances only to find out, that one service was configured as Singleton on IoC configuration. As long, as Singleton lifestyle is default in Castle.Windsor, it's an easy mistake to make - just forget to set custom lifestyle for component and you have a potential problem.

Now, there are couple solutions to this problem:

1) You can override default lifestyle in Castle.Windsor:

container.Kernel.ComponentModelCreated += new ComponentModelDelegate(ComponentModelCreated);

void ComponentModelCreated(ComponentModel componentModel)
{
    if (componentModel.LifestyleType == LifestyleType.Undefined)
    {
        componentModel.LifestyleType = LifestyleType.Transient;
    }
}

2) You can have a test, that allows only components from white list to be with Singleton lifestyle:

[Test]
public void GivenIocRegistration_Container_ShouldNotHaveComponentsWithSingletonLifestyle()
{
   var allowedSingletonHandlers = new[]
                  {
                         "Castle.TypedFactory.DelegateFactory",
                         "Castle.TypedFactory.DefaultInterfaceFactoryComponentSelector",
                         "Castle.TypedFactory.DefaultDelegateFactoryComponentSelector",
                         "MySingletonComponent"
                   };

   var handlers = container.Kernel.GetAssignableHandlers(typeof(object));
   var singletonHandlers = handlers
       .Where(x =>
              x.ComponentModel.LifestyleType == LifestyleType.Singleton &&
              !allowedSingletonHandlers.Contains(x.ComponentModel.ComponentName.Name))
       .ToList();

   Assert.AreEqual(0, singletonHandlers.Count);
}
Comments c#  Ioc  Castle.Windsor 

In one of our production servers, something was knocking IIS down, logs had nothing (well almost nothing - they had records about application pool failing - but no reason why). Usually that means stackoverflow exception, question was only where and why, could not get any pattern when that was happening.
So to catch the problem, we've installed DebugDiag - tool that helps to catch hard to catch exceptions on production servers. We have quite a lot requests coming in, so we had to tune DebugDiag a bit to reduce performance hit, but in result it gave us line, that was causing problems:

var items = dataContext.Items()
      .InAnsiStringRange(x => x.Sku, 2000, skus)
      .ToDictionary(x => x.Sku, x => x);

Having that, we could reproduce that on development machines, but reason still wasn't obvious - it was failing somewhere inside Linq lib. If you've noticed, in that line there's specific method - InAnsiStringRange. We use it to workaround two problems:

  1. Sql server does not support more than 2100 parameters and Contains method generates in sequence with as many parameters, as there are in searched collection, so if you're searching for more than 2100 Skus for example, query will fail. So we execute query in chunks to avoid this problem.
  2. Linq2Sql has a bug when Contains is used on ANSI strings, it converts everything to Nvarchar - and that is a big performance issue. To avoid that we generate large condition from joined ORs (that may be solved better with rewriting IN condition parameters that were generated by Contains, but we didn't wanted to mess with DbCommand)

So we had this code:

 IEnumerable<Expression> expressions = collection.Select(value => (Expression)Expression.Equal(selector.Body, Expression.Constant(value, typeof(TValue))));
 Expression expression = expressions.Aggregate(Expression.Or);

Debugging it gave idea what is wrong. Thing is, that expression tree is tree-like data structure, where each node is an expression - a method call or a binary operation such as x = y. So it was constructing tree like this: Or aggregated tree

As you can see - tree is unbalanced - one branch is from single element, and another is going deep down. Looks like .NET is recognizing that as recursion and gives stackoverflow exception.

So tree needs to be constructed like this to prevent behavior mentioned above:
Or balanced tree

As you can see it is not as deep as first one.

Problem will still be here, just it will handle much more search items (in my test first type of tree handled ~750 search items, second type of tree handled 200000 search items and I think it is able to handle even more).

To form tree as in second picture, following code was used:

public Expression GenerateTree(List<Expression> expressions, int start, int end) {
    if (start == end) return expressions[start];

    var middle = start + ((end - start) / 2);
    var left = GenerateTree(expressions, start, middle);
    var right = GenerateTree(expressions, middle + 1, end);

    return Expression.Or(left, right);
}
Comments c#  linq