…meie igapäevast IT’d anna meile igapäev…

2016-01-20

Adding a service reference to ASP.NET vNext/ASP.NET Core 1.0

Recently I started a new project in ASP.NET vNext aka ASP.NET 5, or as it is known since yesterday, ASP.NET Core 1.0. Since right now the framework is still in RC stage, there is next to no documentation to speak of… but that is a topic for another day.

But I had to add several WCF service references to the project to communicate with the back-end server. While we used to have a reasonably nice dialog for service references, it no longer exists in VS 2015 for ASP.NET Core 1.0. However – there is a plugin for adding “Connected services”, read about it at http://blogs.msdn.com/b/webdev/archive/2015/12/15/wcf-connected-service-visual-studio-extension-preview-for-asp-net-5-projects.aspx.

And I cannot stress this enough – while this plugin is in preview/current stage, do not use it. It is not capable of re-using code in referenced assemblies, which means it generates new classes instead of re-using old ones – and it cannot re-create generic classes. E.g. Response<User> becomes ResponseOfUserFq_SrmLzS, which is really not what is expected or wanted.

Instead, use good old svcutil from command line (you should probably create a cmd file, since you’ll probably want to update the service reference repeatedly):

svcutil.exe /l:cs /t:code /n:*,Your.Project.Desired.NameSpace /r:"fullpath-to-dto-assembly.dll" /r:"fullpath-to-model-assembly.dll" /r:"fullpath-to-vendor-assembly.dll" /o:Reference.cs http://localhost:9000/SomeServices/OurService.svc?fullwsdl

This will fetch the service info and create the service client (Reference.cs) and output.config while re-using all of the classes from assemblies specified with the /r keys. /n key allows you to specify the namespace for the generated code.

Note that the code generated with svcutil will require full .NET framework, not just .NET Core 1.0.

After I had figured out all of the above, I put my service configuration to wwwroot\web.config and expected it to work.

Nope. Enpoint configuration not found. No matter what I did, I could not get my development server (Kestrel) to read the <system.serviceModel> wwwroot\web.config. However, invalid XML in there gave an instant error… explain that?!

Googling was not all that helpful. There seems to be no way for ASP.NET Core to read service configuration from JSON files. But then I stumbled upon a lead – release announcement for ASP.NET 5 beta 7 had a single line announcing support of app.config when running on a full .NET framework.

And this is what I needed to do – instead of using good ol’ web.config (oh, how I hate thee!), you have to create an app.config in the root folder of your website (not wwwroot!), same place where project.json lives. You can just move the output.config there and rename it to app.config – and System.ServiceModel will now happily read the configuration from there.

2015-05-13

Some fun with FizzBuzz in C#

Filed under: Isiklikud,Programmeerimine — Sander @ 13:34:03
Tags: ,

image As a very first thing, I must say that I don’t like whiteboard coding exercises during the job interviews.

These exercises have almost never any relation to real-life programming problems (“Reverse a linked list?” If your reply is anything besides “linkedList.Reverse();”, your answer is wrong.). There are only a few dozen common whiteboard coding questions – and every candidate with a common sense will review those before the interview.

Finally, success with whiteboard coding seems to have next to no connection with the actual qualities of the candidate. And yes, this includes “solving a problem under pressure”.

I much prefer to give the candidates a small task, to do at home in 2..3h. This will give me much more information – what is his coding style? Can he write high-quality and well-commented code? Did he actually manage to finish all parts of the task – and if not, then why? Does he even want the position enough to do a proper job?

That all said, FizzBuzz does have one good use – it can be used to quickly weed out the candidates who cannot program at all. If they cannot do a simple logical exercise, and write a loop with a couple of if-clauses inside it, they really shouldn’t be hired as developers.

That is why ex-coworker Erti-Chris and I were rather surprised after reading “Tales of a Non-Unicorn: A Story About The Trouble with Job Titles and Descriptions” (reddit thread). A person claiming to be “designer/developer if there ever was one” goes “OMG MATH” and cannot solve FizzBuzz (which has almost nothing to do with math)?!

Out of boredom we started to play around with FizzBuzz – albeit in C#, not JS (but all our ideas work with Javascript as well, with a little prodding) – first the “standard solution” and then limiting ourselves further and further.

The “standard solution” using modulus:

  1. Enumerable.Range(1, 100).ToList().ForEach(x =>
  2. {
  3.     var d3 = x % 3;
  4.     var d5 = x % 5;
  5.  
  6.     Print(d3 == 0, d5 == 0, x);
  7. });

Method Print is used repeatedly, it is the same everywhere:

  1. private static void Print(bool three, bool five, int i)
  2. {
  3.     if (three && !five)
  4.         Console.WriteLine("Fizz");
  5.     else if (five && !three)
  6.         Console.WriteLine("Buzz");
  7.     else if (five)
  8.         Console.WriteLine("FizzBuzz");
  9.     else
  10.         Console.WriteLine(i);
  11. }

But what if we don’t allow modulus to be used?

  1. Enumerable.Range(1, 100).ToList().ForEach(x =>
  2. {
  3.     var d3 = x / 3f == Math.Round(x / 3f);
  4.     var d5 = x / 5f == Math.Round(x / 5f);
  5.  
  6.     Print(d3, d5, x);
  7. });

But what about not using neither round nor modulus?

  1. Enumerable.Range(1, 100).ToList().ForEach(x =>
  2. {
  3.     var d3 = x / 3 * 3 == x;
  4.     var d5 = x / 5 * 5 == x;
  5.  
  6.     Print(d3, d5, x);
  7. });

(and also the same limitations as above, nutcase version. Proudly mine).

  1. Enumerable.Range(1, 100).ToList().ForEach(x =>
  2. {            
  3.     var d3 = !(x / 3f).ToString().Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  4.     var d5 = !(x / 5f).ToString().Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  5.  
  6.     Print(d3, d5, x);
  7. });

Still too easy. What about not allowing modulus, rounding, multiplication and division?

  1. for (int i = 0, three = 0, five = 0; i < 100; i++, three++, five++)
  2. {
  3.     if (three == 3 && five == 5)
  4.     {
  5.         Console.WriteLine("FizzBuzz");
  6.         three = 0;
  7.         five = 0;
  8.     }
  9.     else if (three == 3)
  10.     {
  11.         Console.WriteLine("Fizz");
  12.         three = 0;
  13.     }
  14.     else if (five == 5)
  15.     {
  16.         Console.WriteLine("Buzz");
  17.         five = 0;
  18.     }
  19.     else
  20.         Console.WriteLine(i);
  21. }

Amusingly, the last version is fastest of them all.

And by now we were out of time and bored of this. It was fun, though – a sort of minor coding kata.

All the code above can be seen and executed in dotNetFiddle, https://dotnetfiddle.net/84PodI

2014-04-24

Eesti-inglise sõnastiku uusversioon

Sain valmis uue versiooni ome Eesti-inglise sõnastikust – idee poolest siis juba v4.0 (v2, v3). Muuhulgas uuenes ka aadress – dukelupus.com/dict asemel on dukelupus.net/dict. Vana versioon jääb esialgu samuti alles, kuivõrd uus versioon ei pruugi vanade brauseritega koostööd teha.

image

Eesmärkideks uue versiooni loomisel olid mobiilseadmete-sõbralikkus ja kiirus. Viimasel juhul eelkõige lehe laadimise aeg – ehk mahu ja päringute arvu vähendamine, ehkki ka otsing läks veerandi võrra kiiremaks. Tulemus – Google PageSpeed Insights annab nii mobiili- kui ka desktopi-lehele sada punkti sajast.

Võrdluseks väike tabel (GA on Google Analytics’ skript; suurus on reaalne võrgus liigutatavate andmete maht, mis tänu GZIP’imisele on väiksem kui suurus brauseris).

  dukelupus.com/dict dukelupus.net/dict dukelupus.net/dict/m
Lehe laadimine (suurus) 101.1KB (15.4KB GA) 43.2KB (10KB GA) 13.1KB (10KB GA)
Lehe laadimine 1.94s 1.68s 964ms
Lehe taaslaadimine (suurus) 15KB 4.6KB 2.1KB
Lehe taaslaadimine (aeg) 839ms 196ms 190ms
Otsingu tulemus (suurus) 1.4KB 777B 565B
Otsingu aeg 305ms 240ms 224ms

Mis on muutunud?

  • Mobiiliversioonist loe allpoolt
  • HTML5, CSS3.
  • Sõnastik ise pole enam tõlgitav
  • Veelgi minimalistlikum kasutajaliides. Suuna vahetuse raadionuppude asemel on nüüd kaks otsingunuppu.  Suunda saab endiselt TAB-klahvi abil muuta.
  • Serveripoolse otsingu aja asemel näidatakse koguaega, st. lisanduvad võrgu latentsus jm.
  • JSONP’i pole enam (loe selle lisamisest eelmisele versioonile). Seda ei kasutatud – kui aga keegi peaks soovima, siis saab lihtsalt nii CORS kui ka JSONP toe lisada.
  • jQuery UI autocomplete asemel on HTML5 <datalist> element. Kahjuks käitub see aga eri brauseritel täiesti erinevalt, ning võimalik et olen sunnitud jQuery UI peale tagasi minema.
  • Sõnapaare on veidi puhastatud, samuti natuke termineid lisatud.

Mobiiliversioon

image

Mobiili või pihuarvutit kasutavad sõnastiku kasutajad suunatakse automaatselt väiksema resolutsiooniga seadmete lehele, kuid võimalik on kasutada ka otseteed – http://dukelupus.net/dict/m.

Play Store’s on nüüdseks vist pea kümneid eesti-inglise sõnastikke – miks ma siis spetsiaalse mobiiliversiooni peale aega raiskasin?

Esiteks, need kõik võtavad su seadmes ruumi – pahatihti 10+ MB. Online-versioon aga ei võta ruumi, ei eelda paigaldamist ega uuendamist, ei näita reklaame, ei paigalda pahavara jne.

Teiseks, pea kõik neist kasutavad Eesti Keele Instituudi avalikku sõnakogu, mis on aga väga piiratud. Minu sõnastikus on aga hetkel 185605 eesti-inglise ja 198319 inglise-eesti vastet.

Küll aga eeldab online-sõnastik iseenesest mõistetavalt võrgu olemasolu. Kuivõrd tegin sõnastiku mobiiliversiooni tehniliselt äärmiselt minimalistliku (ei ühtegi skripti, välja arvatud GA – lehe esmalaadimine on 3 HTTP requesti, taaslaadimine vaid 1) – ehk teisisõnu, isegi Edge ühenduse korral on sõnastik kasutatav.

Erinevused tavalehest on järgmised:

  • Mobiilseadmete suurusele/resolutsioonile sobiv kujundus, tumedamate värvidega ekraani akukulu vähendamiseks. Touchscreen-sõbralikkus.
  • Autocomplete puudub, et veelgi vähendada liigutavate andmete mahtu
  • Maksimaalselt 10 vastet, mitte 25
  • Puudub otsingu ajakulu.
  • Otsingut ei panda URL’i/ei alustata URL’ist (so, võrdluseks http://dukelupus.net/dict#&term=s%C3%B5nastik&direction=1).
  • Otsingu suunda ei jäeta meelde cookie abil. Ühe sessiooni piires jäetakse otsingu suund meelde (enteriga otsingute tegemiseks).

Nagu ikka, kui sul on ideid, soovitusi, kommentaare või leiad bugisid, anna nendest palun teada postituse kommentaarides.

Järgmine lehekülg »

Blog at WordPress.com.