Tuesday, April 16, 2013

Few tips to know when considering developing MVC4 Applications in VS 2012 - Part 1

Hi Folks,

I'm writing this blog post for ASP.NET web forms developers who have been working with Web forms for years and they are hesitant to develop MVC 4 ASP.NET applications; maybe for different reasons either it is not easy to understand or there are a lot of things you need to wrap your head around to get started building your first business application!

So i decided to write these series of posts,I will try to makes it easier for you to get started in MVC 4 applications with few tips that will help you know how it works and some other considerations to think about.

1) Basic Concepts of the MVC design pattern:
In MVC design pattern implementation by Microsoft, any page you see in the browser is a view, it can be a page or a user control. it can be aspx,ascx or cshtml. MVC has introduced cshtml which is the new render engine called RAZOR, this engine allows you to wirte cs code within your html elements by prefexing any C# Code by @ symbol. For example:
 
<div>@Html.ActionLink("Home", "Index", "Home")</div>

The above line is rendered by Razor as a link with a caption called Home and Index is the method will be called in the Home Controller.

2) Controllers:
All controllers in MVC 4 are stored under a folder called controllers, if you want to create a new controller, right click on this folder and select add new controller and the new controller will be created with all functions if you picked the model "Table" you want to target for this controller.
All controllers MUST have Controller suffix.
Usually you have one controller per 1 entity, but you can design your controllers as you want.
Any action in any view should be mapped to a method in a controller, and the controller takes care of the execution and return the results to the view. That's why you see all controllers can return a view that could be a page or partial page, file, redirect, or Empty.
Check out this link for the types of result you could get from any controller method call:
http://msdn.microsoft.com/en-us/library/system.web.mvc.actionresult(v=vs.108).aspx


3) No Master page in the MVC4 web project:

You will not see a default master page in your project, the general look and feel in the project is controlled by a "Shared View", This Shared View acts as Master Page in the web forms.

If you want to modify the shared view, it is under Shared View Folder --> _layout.cshtml

You still can add a master page to your project  and link all your views to it.

4) Connecting to a database/ Creating your Model:
Model is your database/datasource file in the project. First thing you should do before creating any controllers or views is to create your entity model for your project.
There is no change in this regard. Just add any data entity file that you are familiar with such as: Entity Framework, Entity Data Model, LINQ or SQL Compact Db files.

After creating your model, you can add your controllers since the MVC 4 gives your the ability to create all your CRUD operations once you pick your entity/model and context "dbml or any data source file"


I want to keep it short and simple, I will keep posting more parts in details for building your first MVC 4 application.

Thanks and drop a line if you have any questions or feedback!


Regards,
Mostafa









Friday, April 12, 2013

WCF Error: The underlying connection was closed: The connection was closed unexpectedly.

Hi Folks,

While i was developing a WCF service API for my enterprise application, I got an error while i'm calling a search function in the WCF. The WCF function is calling a stored procedure.

I tested the stored procedure and there is no issues with it at all, and the error was being thrown in my web when the data is being serialized and sent over the network.

The error message:

The underlying connection was closed: The connection was closed unexpectedly.

The point here is that i have other functions in the WCF work perfectly with no errors but only the search function is throwing an exception.

So, it is not configuration thing, it is something else i want to nail it down!

The only way to get the actual error message from calling a WCF service, is to install a tool called "Microsoft Service Trace Viewer" . Here is the download link:
http://www.microsoft.com/en-us/download/details.aspx?id=3138


After you install it, add the following section in the web.config before the closing of the configuration attribue:

<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
<listeners>
<add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData= "traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>

Run your applicaiton, and execute the action that throws the exception then do the following:
1) Open the following path:
C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin

2) Double click on SvcTraceViewer.exe tool

3) From the File Menu, Click on Open.

4) Navigate to your project folder and you will find a file called: traces.svclog, select this file.

5) Check the actual WCF call that causes the error from the activity window and then see the details of the error in the right pane.




Hope this helps.