Experienced Software Developer Still Faces to Code or Basic Acknowledge Test When Interview

As a IT guy, a Software Developer, a Microsoft technologies developer (from C++ to .NET ) with over 24 years development history, I still have to face to some awkward (embarrassed ? ) situations such as job interviews. I left school so many years, but some interviews still needed me to give correct answers for some basic computer and programming acknowledges, such as: What is C# ? What features does OO have ? or I was asked for writing code to give a good performance program to implement a scanning or searching case based on stack or queue ….

Continue reading “Experienced Software Developer Still Faces to Code or Basic Acknowledge Test When Interview”

HttpClient Properties Default Values

MaxResponseContentBufferSize: 2 G

Timeout: 100,000 milliseconds (100 seconds)
Check more from here

There is a related property: HttpSelfHostConfig MaxReceivedMessageSize, I have not found out the default this time, might be 2 G also, but will double check later.

More about WCF or Web API

We have had several posts about WCF vs Web API:

1: WCF, WCF Web API and ASP.NET Web API;

2: Abstractions map: WCF Web API vs ASP.NET Web API;

3: WCF or Web API;

4: Host ASP.NET Web API In WPF Application

Today we read another great article “WCF or ASP.NET Web APIs? My two cents on the subject” which was from Ido Flatow’s Blog, here we got more clear answers:

1: Does WCF support Web API ?

Before Web API changed the name from WCF Web API to ASP.NET Web API, we could say WCF supported Web API, but, WCF Web API was not enough to support Web API; We used WebHttpBinding to support Web API;  but now, if you want to use Web API, you maybe have to use the new tech called ASP.NET Web API without WCF, because the new ASP.NET has more features that supports Web API;

2: WCF mainly provides SOAP services, WCF WebHttpBinding provides non-SOAP service over HTTP (called RESTful services), the new Web API provides HTTP Services (Only);

3: WCF is NOT dead because in many cases you have to choose WCF other that ASP.NET Web API:

If you want to create resource-oriented services over HTTP that can use the full features of HTTP”, choose Web API;

“If you use fast transport channels such as TCP, Named Pipes, or maybe even UDP (in WCF 4.5), and you also want to support HTTP when all other transports are unavailable”,  you can use WCF with SOAP-based bindings and WebHttp binding.

—– Update:

We found another nice post about REST vs SOAP, the original page is on ASP.NET forum, the poster’s name is bruce sql work dot come, but ASP.NET forum always change, so for avoid the original link broken, let us copy part of content as following:

REST and SOAP are competing protocols. SOAP uses a xml payload and usually supports wsdl files to define the valid xml (often called a contract).  REST is a simple http protocol that uses the http verbs get,put,post,delete instead of method names. it is payload neutral (ithough xml & json are the most popular).

WCF is a soap hosting technology for .net. It defines contracts for serializing .net object to SOAP messages and back. while its been extended to support REST/JSON functionality its not a pretty picture, as there is no formal contracts with REST

WebAPI is a new .net framework in MVC4 designed to support REST apis, without the complexty WCF adds.

WCF/SOAP is best used for typed messages over SOAP. Its ideal for both .net and J2EE because they are simularly typed languages and have tooling for the wsdl.

REST/JSON is bested used when talking to dynamic languages like javascript, objective-c, php, and ruby.

WCF or Web API

About WCF and Web API, we should have a longer blog, but this time we just put several links, they are also great article about WCF vs Web API, you should get to know whether choose WCF or Web API after you reading them.

1: WCF and ASP.NET Web API

2: WCF or ASP.NET Web APIs?

3: WCF REST Service with JSON

4: Calling WCF service exposed with different bindings in JQuery

5: WCF Web API is now ASP.NET Web API

Host ASP.NET Web API In WPF Application

Now WCF Web API changed to ASP.NET Web API. ASP.NET Web API has the feature of self-host. In Microsoft official site, they also provides two simples ASP.NET Web API self-host samples. However, they are so simple and actually they looks like the “same” one because these 2 simples are Console applications and use very similar content.

The WPF is more popular for new Windows Application solutions, so we want to find a sample that how to host ASP.NET Web API in a WPF Application. Let’s start:

First, If you have not installed ASP.NET MVC 4 (at present it is still MVC 4 beta), please go to Microsoft ASP.NET website MVC 4 part to download and install it;

After the MVC 4 stuff installed, we can write the code now.

Note: We are using Visual studio 2010.

1: Open Visual Studio 2010 as Administrator :
(Note: this step is important, you must open Visual Studio as Administrator because later we need to listen at a particular HTTP address. )

wpfhostwebapi00

Or, if you do not want to open Visual Studio 2010 as Administrator, you must use Use Netsh.exe to give your current Windows user account permissions to reserve the URL first:

netsh http add urlacl url=http://+:8080/ user=machine\username   (check Microsoft sample here)

2: Create a WPF application, We give a project name WpfHostWebApiDemo, make sure .NET Framework 4 is selected:

(for avoiding the build errors which mentioned in the step 7, please read step 7 first, if you don’t want to do the target framework changing, you are still OK but you have to following everything descripted in step 7 later)

wpfhostwebapi01

3: Install ASP.NET Web API Self Host package:

We will use NuGet Package Manager to install ASP.NET Web API Self Host package. Right click the project name in  in Visual Studio 2010, and choose “Manage NuGet Packages…”, If you can not see this item, it means you have not installed NuGet Package Manager for Visual Studio 2010, Please go to NuGet project site to get help information to download and install it.

wpfhostwebapi02

About NuGet:
NuGet is a Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects that use the .NET Framework. When you add a library or tool, NuGet copies files to your solution and automatically makes whatever changes are needed in your project, such as adding references and changing your app.config or web.config file. When you remove a library, NuGet removes files and reverses whatever changes it made in your project so that no clutter is left.

When you open the Manager NuGet Packages Windows, Input “webapi selfhost” in the top right search box, The Manager can search automatically for you, You will see “ASP.NET Web API Self Host (Beta)” (Currently it is still in Beta stage) and click Install Button.

wpfhostwebapi03

After click Install button, The install process started:

wpfhostwebapi04
wpfhostwebapi05

After the installation finished, click Close button to close NuGet Package Manager.

Now you can see some information in the output windows of Visual Studio 2010:
wpfhostwebapi06

Also, you should see there are new references were added into current project:
wpfhostwebapi07

Now we are ready to star our Web API in current project.

4: In Current project, Add a folder named Models, and add a Model class named ItemModel:

namespace WpfHostWebApiDemo.Models
{
    public class ItemModel
    {
        public int ItemID { get; set; }
        public string ItemName { get; set; }
        public double ItemValue { get; set; }
    }
}

5: Add a new folder named Controllers, in this folder, add a new Item, specify the template to Web API Controller Class in Web category. Give the name ItemController

wpfhostwebapi08

Remove all generated code and add the following code into ItemController class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using WpfHostWebApiDemo.Models;

namespace WpfHostWebApiDemo.Controllers.Controllers
{
    public class ItemController : ApiController
    {
        // GET /api/<controller>
        public IEnumerable<ItemModel> Get()
        {
            return new List<ItemModel> 
            {
                new ItemModel() { ItemID = 1, ItemName = "Item1", ItemValue = 100 },
            };
        }
    }
}

6: Host service:

Add the windows loaded event in MainWindow, and add host service code, All MainWindow.xaml,cs file code is like the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Web.Http.SelfHost;
using System.Web.Http;

namespace WpfHostWebApiDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var config = new HttpSelfHostConfiguration("http://localhost:5477");

            config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });

            using (HttpSelfHostServer server = new HttpSelfHostServer(config))
            {
                server.OpenAsync().Wait();

                MessageBox.Show("Web API service started.");
            }
        }
    }
}

In above code, we listen  localhost:5477, which you can change the port number to what port number you like.

7: Compile and Build project:

Now we build the project, we will see at least 5 error messages:

The type or namespace name ‘ApiController’ could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name ‘SelfHost’ does not exist in the namespace ‘System.Web.Http’ (are you missing an assembly reference?)

The type or namespace name ‘HttpSelfHostConfiguration’ could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name ‘HttpSelfHostServer’ could not be found (are you missing a using directive or an assembly reference?)

The type or namespace name ‘HttpSelfHostServer’ could not be found (are you missing a using directive or an assembly reference?)

wpfhostwebapi09

The reason is current WPF project is based on the target framework ASP.NET Framework 4 Client Profile, which miss some libraries, Just right click the project name in Visual Studio 2010 and select property, and change the target framework to ASP.NET Framework 4.

wpfhostwebapi10

After you changed the target framework to .NET Framework 4, Visual Studio 2010 will give you a message that the project need to be closed and re-open, but looks seems the Visual Studio 2010 can not re-load the project automatically, so here you should close Visual Studio manually first, and open Visual Studio 2010 as Administrator again, and load our demo project.

(Note: for avoid to re-open Visual Studio, you’d better change Target framework in step 2)

Now let us rebuild the application, we see the old error messages gone, but we get the following new errors:

The type ‘System.Web.Routing.RouteCollection’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

The type ‘System.Web.Routing.Route’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

The reason is we still missed a dll file, let’s add reference to System.Web, the version is 4.0

wpfhostwebapi11

Now rebuild again. the build is OK now!

8: Run the Web API Service:

Click Ctrl + F5, you will see the service started message which we added in code.

wpfhostwebapi12

Open web browser and input the URL address: localhost:5477/api/item

you will see the service content as XML format on the web page:

wpfhostwebapi13

Now we know the Web API service is working! You can create a client to consume the service now.

WCF, WCF Web API and ASP.NET Web API

Most of content in this post from Microsoft and related websites:

Firstly, we start from the simplest thing: The relationship of WCF Web API and ASP.NET Web API is WCF Web API is now “merged” to ASP.NET Web API, “ASP.NET Web API is effectively the next version of WCF Web API. There will not be a separate release for WCF Web API and we will retire all WCF Web API content by the end of 2012” :

Announcement: WCF Web API is now ASP.NET Web API! ASP.NET Web API released with ASP.NET MVC 4 Beta. The WCF Web API and WCF support for jQuery content on this site wll removed by the end of 2012.
aspWebAPI00
(from WCF project)

We are happy to announce that ASP.NET Web API has now shipped with ASP.NET MVC 4 Beta!

ASP.NET Web API represents the joint efforts of the WCF and ASP.NET teams to create an integrated web API framework. You can get the bits and find articles, tutorials, samples and videos on the new ASP.NET Web API home page. We have also setup a Web API forum on the ASP.NET site where we will monitor customer questions and discussions.

What is WCF Web API ?

WCF Web API allows developers to expose their applications, data and services to the web directly over HTTP. This allows developers to fully harness the richness of the HTTP as an application layer protocol. Applications can communicate with a very broad set of clients whether they be browsers, mobile devices, desktop applications or other backend services.

What is ASP.NET Web API ?

ASP.NET Web API is a framework for building and consuming HTTP services that can reach a broad range of clients including browsers and mobile devices. It’s also a great platform for building RESTful services. ASP.NET Web API takes the best features from WCF Web API and merges them with the best features from MVC.

There is very important information which you might recognize something you need:

Why change the name?  Web APIs have a foot in two worlds: the world of service orientation and the World Wide Web. We decided to align ASP.NET Web API with the rest of the Microsoft web platform, so we went with the brand that communicates this alignment. From a technical perspective we also decided to go with a new HTTP specific dispatcher instead of trying to carry forward the WCF dispatcher, so there is virtually no WCF code in the new stack.

About more information between WCF and ASP.NET Web API,  Ido Flatow has a very good article here: WCF or ASP.NET Web APIs? My two cents on the subject:

What is the purpose of the WebAPIs?

When WCF was conceived back in its Indigo and .NET 3 days, the main goal was to support SOAP + WS-* over a wide variety of transports. However, over time it became clear that although SOAP is wide spread and supported on many platforms, it is not the only way to go when creating services. There is also a need to also support non-SOAP services, especially over HTTP, where you can harness the power of the HTTP protocol to create HTTP services: services that are activated by simple GET requests, or by passing plain XML over POST, and respond with non-SOAP content such as plain XML, a JSON string, or any other content that can be used by the consumer. Support for non-SOAP services was very much needed in WCF back then, mostly because some clients, such as web browsers, were not that suitable to handle SOAP messages (plenty of XML parsing and DOM manipulation).

About detail content, Please read above all articles which I provided Links.