Return type of One-way or Simplex WCF Service

WCF is one of the most asked topics in interviews. Recently one of my reader sent me a mail. He asked, “What is error in following code?” His code snippet was as below,


[ServiceContract]
    public interface IService1
    {

        [OperationContract(IsOneWay=true)]
        string  SendHello(string name);
        
    }


In one sentence, problem with above code is in return type of Operation Contract. IsOneWay property of Operation Contract is set to True, which is making service message format as Simplex or One Way. A One way WCF Service cannot return anything. Its return type must be set to Void.

image

In given code snippet, Service is created as One Way Service and return type of operation contract is set to string and that is the reason Service is throwing run time Exception.

image

Given code snippet can be fixed by changing return type of operation contract from string to void.


[ServiceContract]
    public interface IService1
    {

        [OperationContract(IsOneWay=true)]
        void  SendHello(string name);
        
    }


Conclusion is one-way or Simplex WCF Service cannot return any data to client or its return type must be set to Void. If one-way service return type is set to any other return type then run time exception will be thrown.

I hope you find this post useful. Thanks for reading.

Day #2: Learn ASP.NET MVC 5- Adding View

Day #1: Learn ASP.NET MVC 5- Adding Controller

On Day1 we learnt about Adding Controller. Today we will learn to add View in MVC project. Let us start with understanding with is View?

View is part of MVC that renders user interface. View contains markup that gets render with ViewEngines. ViewEngines generates View in ASP.NET MVC framework. There are two kinds of View Engines. One is aspx engine and other Razor engine.

We can say View is User Interface in MVC application and it never talks to database or model directly. It does not contain any business or application logic. Controller returns a View to be rendered.

image

You can add a View, right click on Product subfolder and select Add from context menu and then select template MVC 5 View Page with Layout.

image

There are four options for Views.

  1. MVC 5 View Page
  2. MVC 5 View Page with Layout
  3. MVC 5 Layout Page
  4. MVC5 Partial Page

Let us start with adding a MVC5 View Page with Layout. Give name of the View as of the action name. For example, if we have action in controller as below,


  public ActionResult Index()
        {
            return View();
        }

Then we will create a View with name Index since action name is Index and it is returning view. Next we need to select layout page. Let us go ahead and select a Layout.cshtml from Shared folder.

image

After adding View, you will find that inside Views->Product folder index.cshtml being added.

image

By default following code gets created in Index view

image

Let us explore above code line by line,

  • We are setting layout of view as _Layout.cshtml from Shared folder.
  • ViewBag is used to pass parameters. We are setting Title of the View as Index.
  • On DOM creating some HTML elements like <h2> and <p>

Now let us go ahead and run application. On navigating Index action in Product controller, you will get index view rendered in browser.

image

Let us go ahead and explore _Layout.cshtml. We are passing title from View to layout using ViewBag.

image

On running application, you may notice that browser title is set to Index-My ASP.NET Application. On further exploring _Layout.cshtml, you will find navigation links and footer. If required you can change content of layout as well.

Next let us see how we can pass data from Controller to a View. Before we go into demo section, let us understand that View will never interact with Model directly and perform any business logic. Controller will talk to Model and perform business operation on data and then pass that to View to get rendered.

image

Some of the important points are as follows,

  1. View never perform business logic
  2. View never interacts with Model
  3. Controller talk to Model , performs business logic and pass data to View

So let us say we want to pass data from Controller to View. Controller got an Action named Data as below,


    public ActionResult Data(string fruit, int eatingtimes)
        {
            ViewBag.fruit = fruit;
            ViewBag.times = eatingtimes;
            return View();

        }

From Data action, we are passing fruit and times as parameter from Controller to View. We are using ViewBag property to pass data from Controller to View. Now these passed data can be used on View as below. We have created a View named data without any layout.


 <div>
        <h2>Data</h2>
        <ul>
            @for (int i = 0; i < ViewBag.times; i++)
            {
                <li>@ViewBag.fruit</li>
            }
        </ul>

    </div>

In Route we have added a new Route as below,


  routes.MapRoute(
               name: "Product",
               url: "{controller}/{action}/{fruit}/{eatingtimes}"
           );

Let us go ahead and run application. We are passing mango as fruit and 9 as eatingtimes. As a output Mango will get render 9 times on View.

image

This is the way you can pass parameters from Controller to View. In further posts, we will complexity around Views and working with different kind of Views. I hope you find this post useful. Thanks for reading.

Day #1: Learn ASP.NET MVC 5- Adding Controller

 

After long time, I am back to web development. At current web development paradigm, ASP.NET MVC framework is one of the most popular and heavily used web development framework. Seeing popularity and usefulness of ASP.NET MVC, I have decided to write easy to go blog series on this. In this blog post series, each day I will share my learning in form of blog posts.

In day 1, let us learn about Controller. Very first in simpler words,

“Controller does all the actions in MVC. It handles user interactions or inputs. It passes users’ data to Model and selects a View to render on UI”

image

In more coding terms, Controller can be define as below,

  1. Controller is a class
  2. It contains one or more methods called Actions
  3. An Action can return simple string or a selected View to UI
  4. Action takes all browser requests or user inputs
  5. It retrieves data from Model
  6. It selects View to be rendered on UI

You can add a controller by right clicking on Controllers folder in solution explorer. In context menu select Add->Controller. There are different types of Controller templates are available to add. Let us go ahead and select MVC 5 Controller- Empty template to add controller.

image

Next give name to Controller. I am giving name as ProductController.

image

In solution explorer, you will notice In Controller folder, ProductController.cs file being added and in Views folder, there is subfolder Product being added.

image

Next let us go ahead and remove default code created and following two actions. First Action is Index (It is default) action. Index action does not take any input parameter. Second Action added is Welcome. It takes two input parameters id and name.

 


public class ProductController : Controller
{
//
// GET: /Product/
public string Index()
{
return "this is default action";
}

public string Welcome(string name, int id)
{
return "this is welcome action" + name + "with id " + id;
}
}

Following points are worth noticing about ProductController.

  1. ProductController class extends Controller class
  2. Actions are created as methods.
  3. In this case Actions are returning string.
  4. Actions can return other types as well. We will discuss them in further posts.
  5. Actions can take zero or more input parameter.

Let us go ahead and run application. Default action of Product controller is index. On navigating Product Controller, Index action will be called. In this case Index action is returning a string.

clip_image002

We have put one more action Welcome in Product controller. This takes two input parameters Id and name. You can call Welcome action as below. We are passing two input parameters as query string.

image

Better approach to pass input parameters are as Route Data than query string. Id is default input parameter. So instead of passing id as query string, you can pass it as route data as well.

clip_image002[6]

You can find default route and parameters in RouteConfig.cs.

 

clip_image003

 

In above code as you see default controller is Home, default action is Index and default optional parameter is id. If you want to pass other input parameters as route option, you can very much do that by adding more routes.

clip_image005

We have added a route with name and id as parameter. On running application, now you can pass name and id as route option.

clip_image007

This is the basic of working with Controllers in MVC. In further posts we will get into deeper concepts of Controllers. I hope you find this post useful. Thanks for reading.

Lenovo ThinkPad T440P: My next companion

image

A batsman grow with his bat, a writer grow with his pen and a programmer grow with his machine. I was my gifted my first desktop by my father back in year 2003. That was assembled desktop with following configurations,

Processor: Pentium 3 1.7 GHz ||RAM: 128 MB || Hard Disk: 40 GB || 17 inch monitor || Assembled

I wrote first program on that assembled desktop and since then have worked on different kinds of desktops and laptops. I have worked on Dell Latitude, Lenovo ThinkPad etc. but never own them. On Friday 9th May 2014, I bought new machine for me. After bit of research online, for me Lenovo ThinkPad T440P came as a best choice for this time. This is one of the best available in market at this given time.

clip_image002

First thing you will notice is, it is not very sophisticated machine. In look it is very raw and a feel is guaranteed when you type codes using its keyboard.

Configuration of this machine is as follows,

Processor: Intel i7, 4th Generation||RAM: 16 GB || Hard Disk: 500 GB || 14 inch display || l3 cache || Windows 8 professional || Lenovo

 

clip_image004

One of the best is its keyboard and supported 1366*768 resolution display. Due to 14 inch screen size it is small and light. If you are a frequent traveller, this is your machine.

clip_image006

This machine is available in Lenovo stores throughout India. Rather buying it online, I bought it from a Lenovo store in NCR. I am very excited for my new companion Lenovo, ThinkPad T440P. Now why I am waiting let’s start coding.

First look of book: Designing Evolvable Web APIs with ASP.NET by Glenn Block

In India order this book from Amazon here

You become what you read

In this era of Kindle, tablets and EBooks, I proudly consider myself as real book lover. I get more connected to real book than any EBook on any trendy device. In order to purse my passion of collecting good books, toady I got WEB APIs book with ASP.NET by Glenn Block. Even before I start learning from this book, I am sure this book will teach me many good things about HTTP, REST and WEB API.

I got book delivered from Amazon this afternoon. It is summer in Delhi and the Sun it at its best yielding temperature 45 degree centigrade. I had to go out in this weather to collect book but trust me it was worth going out.

clip_image001

I thank from bottom of heart to Glenn Block for this wonderful gift. I can’t express in words that how I am going to enjoy and learn this book.

This book got great credential since it is authored by veterans like Glenn Block Pablo Cibraro pedro Felix Howard Dierking Darrel Miller

clip_image002

In India order this book from Amazon here

There are 17 chapters in this book. They are as follows,

  1. The Internet , the World Wide Web , and HTTP
  2. Web APIs
  3. ASP.NET Web API 101
  4. Processing Architecture
  5. The Application
  6. Media Type Selection and Design
  7. Building the API
  8. Improving the API
  9. Building the Client
  10. The HTTP Programming Model
  11. Hosting
  12. Controllers and Routing
  13. Formatters and Model Binding
  14. HTTP Client
  15. Security
  16. The OAuth 2.0 Authorization Framework
  17. Testability

I am fortunate to see evolution of WCF SOAP to WCF REST to Web API. I have not started reading yet but at the very high level I perceive this book as one of the greatest guide to learn HTTP Services. This book helps to create Web APIs harnessing the power of HTTP. I strongly recommend this book, if you are willing to create REST Services.

Our hero Scott Guthrie is also impressed by this book and he has said something like below about this book,

clip_image002

So what are you waiting for? Go and grab your copy and start learning and coding. Great job by Glenn and other authors.

In India order this book from Amazon here