All posts in .net

I was working on DayTickler and authentication when the latest Azure Mobile Client was released. Being fearless, i decided to upgrade.

Turns out the new version of the nuget doesn`t support pcl, but netstandard. No problem, let`s upgrade everything to netstandard, i need it for things like the latest AutoMapper anyways.

NetStandard is the new name for portable libraries. They are not built X time for each platform but offer binary compatibility for each platform. For example, uap10 implements netstandard 1.4, meaning that any UWP project can target libraries of type netstandard 1.4 because it provides and implementation of all the 1.4 level api…

It`s really complicated, but i always go back to this post, mainly for this table :

.NET Standard 1.0 1.1 1.2 1.3 1.4 1.5 1.6 2.0
.NET Core 1.0 1.0 1.0 1.0 1.0 1.0 1.0 2.0
.NET Framework (with tooling 1.0) 4.5 4.5 4.5.1 4.6 4.6.1 4.6.2
.NET Framework (with tooling 2.0 preview) 4.5 4.5 4.5.1 4.6 4.6.1 4.6.1 4.6.1 4.6.1
Mono 4.6 4.6 4.6 4.6 4.6 4.6 4.6 vNext
Xamarin.iOS 10.0 10.0 10.0 10.0 10.0 10.0 10.0 vNext
Xamarin.Android 7.0 7.0 7.0 7.0 7.0 7.0 7.0 vNext
Universal Windows Platform 10.0 10.0 10.0 10.0 10.0 vNext vNext vNext
Windows 8.0 8.0 8.1
Windows Phone 8.1 8.1 8.1
Windows Phone Silverlight 8.0

 

Also, if, along the way, you wish to include non netstandard libraries in your netstandard library,  you can modify your project.json like this :

“frameworks”: {
“netstandard1.4”: {
“imports”: [ “portable-net45+win8+wpa81” ]
}
}

It helps !

HttpClient wows !

Categories: .net
Comments: No

I was helping a colleague doing sync stuff in a addin for a big app, and in order to recreate the problem, i decided to write some test code in a console app (which is not async).

That was relatively easy, and this is what the code looked like:

<p class="brush:csharp">var submitFileTask = system.SubmitFileAsync(new byte[] { 1, 2, 3 }, "asdf.pdf", "RequestedFile");</p>
<p class="brush:csharp">submitFileTask.Wait();</p>

That code went directly in the main method and it fixed my async in a console problem.

But that SubmitFileAsync method used HttpClient, and i had never used it. Wow, what simplicity !!!

A http get which returns json is simply a matter of these few lines:

uri = "https://something.com/getjson";
response = await httpClient.GetAsync(uri);
response.EnsureSuccessStatusCode();
var x = response.Content.ReadAsStringAsync().Result;

A http post which sends json stuff could look like this:

uri = "https://something.com/postjson";
var data= Newtonsoft.Json.JsonConvert.SerializeObject(new { MachineName = "test", Name = "Erik" });
response = await httpClient.PostAsync(uri, new StringContent(data, Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();

Then we had to send a file, and the api stated a multipart/form-data content type… So easy with this:

uri = "https://something.com/postMultiPartFormData";
MultipartFormDataContent formMultiPart = new MultipartFormDataContent("-----" + Guid.NewGuid());
formMultiPart.Add(new StringContent(Reason), "Reason");
formMultiPart.Add(new ByteArrayContent(fileContent), "File", fileName);
response = await httpClient.PostAsync(uri, formMultiPart);
response.EnsureSuccessStatusCode();

Now isn’t that simple !

I`ve been using AutoMapper for a long time. It’s a tool that allows to map from one object graph to another through a convention based configuration system. My only objection was that i was scared that the projection was done in memory, meaning i had to load up whatever data i needed, then do the projection.

On one of the projects i manage, i saw the use of AutoMapper directly in the entity framework query. My first reaction was : it will load everything, then project, then filter…

Well, surprise surprise, AutoMapper has this mode where it will push the projection stuff to the source (through expressions) and hence, allow you to map in the database engine… now that is cool !

Wireframes

Categories: .net
Comments: No

A few years ago, there was not a lot of good solutions for helping teams prototype user interfaces in an interactive method. Visio and PowerPoint were pretty much king in this domain.

In the last year, i got back into the subject for the different projects i was working on and was delighted with the new offerings.

Personally, i chose MockPlus. It seemed right and did the right thing for me.

If you want to read a more thorough review, follow this link : http://www.cooper.com/prototyping-tools

.net core 1.0

Categories: .net
Comments: No

I still remember in 2002, when Microsoft announced the .net framework and i was so happy to have a competitor to Java in the Microsoft ecosystem. It was the start of modern programming for me – no more win32, no more pointers, no more stl, mfc.

14 years later, Microsoft has now announced a new incarnation of dot net. Not a new version, but a complete revamp of the product that allows .net code to run on Windows, Macintosh or Linux. A modern .net that keeps only the usefull apis in a cross platform method. A new development process where dotnet is on github, open sourced and ready for everyone to bring their ideas. A new deployment concept where you ship the framework with your app instead of asking some administrator to do it.

Welcome to .net core. http://dot.net.

Read the full announcement blog here : https://blogs.msdn.microsoft.com/dotnet/2016/06/27/announcing-net-core-1-0/

Well, it’s 2016 and technology doesn’t stop advancing.

A couple of months ago, I though Xamarin was the best platform for creating cross platform apps from the same (or mostly same) source code. From a technology standpoint, Xamarin offers the mechanisms to use all of my Visual Studio and .NET skills to build cross platform apps. All I needed was a MacMini hidden in my basement to do compilations. Let’s not forget their Xamarin Forms technology that gives you a single thing to learn to build user interfaces for the three major platforms.

I built a lot of web apps, some with Angular or other SPA frameworks, but to be honest nothing made it worthwhile. Plus the fact that I find Angular soooo complicated to learn.

Fast forwards to today. The web has advanced, and all new Windows machines have Edge, a great browser without the need to install something else such as Chrome. Angular 2 is in beta, as well as Aurelia: new SPA frameworks that embrace the latest advances in html5. Add to that the new web UI frameworks (e.g. Framework7) that skin web apps with the same look and feel as native apps on IOS and Android (I guess UWP support will be trivial to add to these frameworks). I’ve built an Aurelia app with these technologies, and I have about 10 times less code to do the same thing as with Xamarin Forms. Coupled with ManifoldJS, I can package a web site to a mobile app…

Now where does Xamarin Forms stand in this new world ? I believe the cross platform technology for normal, business apps has a very short future ahead of it. As some of you who follow what I do know – it’s full of bugs which I keep on declaring and there are always a regression here and there. I still believe in the technology, but I believe Xamarin needs to open source it’s Xamarin Forms project. In a matter of weeks I am certain the bugs will disappear and a bunch of new functionality will emerge. That will make a difference. I believe Xamarin’s value proposition is in reusing .net skills and know how, not an cross platform API. Besides, Xamarin Forms is useless without the core Xamarin engine to generate the IOS and Android apps.

Now for my predications. In 2016, I will only start new projects with web technologies. If Xamarin Forms is open sourced, I will consider it again. Obviously, project specifics might make me use one or the other whatever my current preferences are…

 

 

 

I was playing around with Xamarin Forms, and ran into a small hiccup :  Let’s say I have a list, and one particular item is disabled – what do I do ?

It’s easy to add a “Disabled” property to the associated ViewModel, and in the associated command check if it is disabled and if so, just return. But how do alter the visual state based on this property ?

A little searching brought me to this excellent article on http://pause.coffee/blog/ which gave me the answer.

I simply created a small class named GreyLabelIfDisabeldBehavior and here is it’s code:

    public class GreyLabelIfDisabledBehavior
    {
        public static readonly BindableProperty DisabledProperty = BindableProperty.CreateAttached<GreyLabelIfDisabledBehavior, bool>(
               bindable => GreyLabelIfDisabledBehavior.GetDisabled(bindable),
               false, /* default value */
               BindingMode.OneWay,
               null,
               (b, o, n) => GreyLabelIfDisabledBehavior.OnDisabledChanged(b, o, n),
               null,
               null);

        public static bool GetDisabled(BindableObject bo)
        {
            return (bool)bo.GetValue(GreyLabelIfDisabledBehavior.DisabledProperty);
        }

        public static void SetDisabled(BindableObject bo, Command value)
        {
            bo.SetValue(GreyLabelIfDisabledBehavior.DisabledProperty, value);
        }

        public static void OnDisabledChanged(BindableObject bo, bool oldValue, bool newValue)
        {
            if (oldValue != newValue)
            {
                var color = ColorEx.Render("foreground");
                if (newValue)
                    color = ColorEx.Render("gray");
                (bo as Label).TextColor = color;
            }
        }
    }

After that, it was a simple case of using the new feature in XAML like this:
<Label Text={Binding Title} customControls:GreyLabelIfDisabledBehavior.Disabled={Binding Disabled}/>

Here is a little adventure we ran into using WCF. We are using the CustomUserNameValidator and allthough it’s so fun to use, it’s got a few nasty problems…

4 Easy steps to implement into your project:

  1. Create a classe that derives from UserNamePasswordValidator
  2. Override the Validate method, this method receives the username and password as parameters
  3. Throw an exception if the username and password are invalid
  4. Hook it up to your service using configuration

Now for the nasty stuff:

  1. All you receive is the username and password, nothing else (like the ip of the caller, a session id or message id…) because OperationContext is null
  2. No way to send information forward to the service (for exemple the time it took to perform the authentication). This was actually problematic for us so we had to store the time it took to authenticate by username, then later on try to fetch an the data and add it to the service time. You do have the problem that you might grab the wrong authentication information for a given service call but unless you are in a highly parallel environment, it kind of works. You also have to think of performing cleanup on this cache so that memory doesn’t leak. Last gotcha is that if you are using a sessionfull endpoint, only the first service call gets an authentication (which is kind of true, being there is a session).
  3. We found a nasty bug where authentication occurs sequentially, even if our endpoint is equipped for parallelism, which creates a problem because authentication in our case takes around 50ms. If we just return from the validator, we can see the service working in parallel, but since our authentication code is longer than the actual service code’s execution, we see very boring results which look like queuing. We are using an http2007Binding over SSL with no Secure Conversation Token and custom username password authentication.

The bug has been logged with Microsoft, and i hope they give us a good news quicky, through a patch and a switch to be able to turn on the feature…

For a while now it has been known that Visual Studio 2015 will ship with an Android emulator. There are a few of these on the market and in my opinion, Microsoft has the fastest and most robust one, especially when developping with Xamarin.

Now, for what i am doing lately, i end up running Visual Studio 2013 with Xamarin but require and Android Emulator. Currently that requires me to open Visual Studio 2015 RC, open the Android emulator dialog, choose an emulator image and launch it. At that point I can use it from Visual Studio 2013.

I did some digging aroud, and this is the command line tool that ends up being used to launch the emulator. I discovered it using ProcessExplorer.

"C:\Program Files (x86)\Microsoft XDE\10.0.1.0\xde.exe" /sku Android /displayName "VS Emulator 4.5\" Lollipop (5.0) XHDPI Phone" /memSize 1024 /diagonalSize 4.5 /video "720x1280" /vhd "C:\Users\Erik\AppData\Local\Microsoft\VisualStudioEmulator\Android\Containers\Local\Devices\vhd\4.5_Lollipop_(5.0)_XHDPI_Phone\image.vhd" /name "VS Emulator 4.5-inch Lollipop (5.0) XHDPI Phone.erik"

Put that in a “cmd” file and there you go, a quick way to launch the Android emulator.

p.s. this particular command line won’t work for you unless you are named Erik 🙂

Well, always on the same project, we had a requirement for having Java consumers hookup to a .net WCF web service.

Now in theory, isn’t that suppose to be simple ? After all, everybody knows that ws2007HttpBinding is suppose to have a lot of interoperability with different platforms. DotNet seems to work fine everytime, after all, it is the same technology stack. Java is super duper extra complicated.

First of all, Java by itself doesn’t provide a framework for complicated scenarios. If you want that, you need to integrate with one of the different libraries the implement the different standards. For that there is Apache CXF, Metro, Apache Axis 2 and probably more. These libraries are suppose and say they implement the different standards but can be riddled with bugs or simply : work in progress.

You also have to pay attentions to the little things, for example will localized reasons within faults be available on the Java side ?

One advantage of using Metro is that is was officially tested for interoperability, which seems important. Unfortunatly, Apache CXF is more widely used but has known bugs.

The good thing about all of this though, is that WCF is configuration driven, which means that satisfying a different kind of technology is probably as easy as exposing a second endpoint for the same service what has a different configuration.