mercredi 5 octobre 2016

Start with Azure Blob storage using Web API




Overview

Azure Blob Storage is part of the Microsoft Azure Storage service that is able to  stores unstructured data in the cloud as objects/blobs. 
Blob storage s a collection of binary data can store any type like :  text , binary data, such as a document, media file, or application installer as a single entity.
In this article, we will describe the process of creating a ASP.NET Web API project in order to upload a file directly to a container in your Azure Storage account.
Prerequisites:
We recommend to use the last version of the Azure Storage Client Library for .NET 

Blob service concepts


1- Create a storage account in Microsoft Azure

From Preview Portal, select New > Data and Storage > Storage Account

Then you put the name, the resource group and the location of your storage account.

After a few minutes your storage account will be pinned in the dashboard and ready to use!

2- Create a container to store your files

In order to store your files in Azure Blob Storage, you need to have to start by adding a container, which groups any set of blobs. A container can have two different access types:
  • Private, which does not provide anonymous access to the container or the blobs therein.
  • Public, in which case all the blobs within the container can be accessed publicly through anonymous access; however, in order to list all blobs in a container, the account credentials are necessary.
So, let’s name the container in our example “images” and set its access type to Public.

3- Storage Account Credentials



4- Set up your development environment

Now we will create a project type : Asp.net MVC after we select Web API template. ASP.NET 4.6 Templates and we leave Authentication to “No Authentication”.
  • Enable nuget packages.
Use NuGet to install the required packages: There are two packages that you'll need to install to your project:


5- Configure your storage connection string


6- Let's go to our Web API application

  • Create a Custom Stream Provider
We need to extend MultipartFormDataStreamProvider class by creating your own AzureStorageMultipartFormDataStreamProvider, which will be responsible for writing streamed data directly to a blob in your container.

We will create BlobHelper and FileDetails :


Now, we add an Upload Api Controller :
Finally, you need to create an Api Controller, which will provide the API for the file to be uploaded in your storage account.

Now, we will test our Web API using POSTMAN.
All successfully uploaded images will be available in 'directory name' container, in Azure Preview Portal.
Github : https://github.com/didourebai/AzureBlobsFileUploadSample ( Test and comment please ...).

More samples

For additional examples using Blob storage, see Getting Started with Azure Blob Storage in .NET.


mercredi 28 septembre 2016

NLayered Architecture



When we start any project,  we have to think about the architecture and use the best practices and avoid the anti-pattern (http://hamidarebai.blogspot.it/2016/03/do-you-like-spaghetti-or-lasagna.html) ,  To achieve layered architecture, ASP.NET  we have to follows the principles of Domain Driven Design. In Domain Driven Design there are four fundamental layers:

1- Infrastructure Layer : this layer is used to interact with Database using ORM framework,  like NHibernate or Entity Framework, it can be a Repository implementation or an implementation for an email provider to send emails.
2-Domain Layer : in this layer we find  business objects and their rules . This is the core of the application, it contain : entities, value objects, repositories, domain services, Unit of Work, Domain Events...
3- Application Layer : Interaction layer between the Presentation and Domain Layers. Orchestrates business objects to perform specific application tasks, we can use DTOs, DTO Mappers, Authorization, Session, Audit Logging...
4- Presentation Layer : that can present SPA or MVC or MVVM  web application, it's the user interface , the front-end part of any application and can be a mobile interface too. We can add in this layer : Localization, Navigation, Notifications...

We can use another Layer to expose services : WCF or Web API .
In presentation layer, we can use Web API Controllers integrated in Core web project .

jeudi 22 septembre 2016

Building A RESTFUL API with ASP.NET CORE and Visual Studio 2015


To start your environment, please refer to this article : http://hamidarebai.blogspot.it/2016/07/how-we-can-start-using-visual-studio_22.html

So, we consider that everything is set.

This a video that describe all :
And the source code :

mardi 20 septembre 2016

Difference between SOAP and REST Web Services (from generic to .NET)




SOAP
REST
Acronyme
Simple Object Access Protocol
REpresentational State Transfer
Architecture
SOA based architecture that use middleware interoperability.
Architecture style protocol designed for web based communication assuming only point to point communication.
Message Format
XML inside a SOAP Envelope
XML
In business logic
Uses services interfaces to expose the business logic.
Uses URI to expose business logic.
Standards
Defines standards to be strictly followed.
Does not define too much standards like SOAP.
Communication
Use WSDL for communication between consumer and provider.
Uses XML or JSON to send and receive data.
Transport
Transfer is over HTTP, Also uses other protocols such as SMTP, FTP, MIME, JMS, HHTP, etc.
Transfer is over HTTP only.
JavaScript calls
JavaScript can call SOPA but it is difficult to implement.
Easy to call from Javascript.
Security
SOAP defines its own security.
RESTful web services inherits security measures from the underlying transport.
Payload
Payload must support SOAP schema.
Payload can be of any format.
Error Handling
Doesn't support error handling.
Has built-in error handling.
External Calls
Invokes services by calling RPC method.
Simply calls services via URL path.

What do I choose, SOAP or REST?
It depends!!

Both protocols have their advantages and disadvantages, but you should choose which protocol you are using based on the requirements of the problem. Instructing you on how to accomplish that is beyond the scope of this question and answer.
And in  .NET world, you can find more details to choose  :

And REST is more popular from ASP.NET WEB API.

And the use of SOAP is here from WebServices :
https://msdn.microsoft.com/en-us/library/ms972326.aspx
http://www.codeproject.com/Articles/1231/ASP-NET-Web-Service
https://msdn.microsoft.com/en-us/library/t745kdsh.aspx

We will add a sample next post using REST ASP.NET WEB API 2.0




lundi 19 septembre 2016

Updates in .NET core 1.0.1 : updating your machine or your application to use .NET core


1.     Updating your Machine to use .NET Core 1.0.1

- Install .NET Core 1.0.1 (https://www.microsoft.com/net/core#windows)

-  If you have already the last version 1.0.0, the installation of 1.0.1 version does not delete or otherwise update your current .NET Core.

2.     Updating your Application to use .NET Core 1.0.1
 You can do the update your .NET Core metapackage reference toMicrosoft.NETCore.App 1.0.1. (https://www.nuget.org/packages/Microsoft.NETCore.App/1.0.1)
An app configured this way will no longer run on .NET Core 1.0.0, even if it’s the only .NET Core version on a machine.
So, you just update to ASP.NET Core 1.0.1 and Entity Framework 1.0.1, by referencing a newer NuGet package.
So, we can see that in project.json file:




mardi 2 août 2016

IIS tips

 Step-by-step IIS configuration guide

  •  If the following error occurs, go to applicationHost.config file in C:\Windows\System32\inetsrv\config  and set overrideModeDefault properties to "Allow" for handlers section :
  <section name="handlers" overrideModeDefault="Allow" />

  • If the following error occurs, 
proceed to ASP.Net component set up:
  • Launch :  c:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe –i to register the previous ASP.NET installation with IIS
  • If authorization are not set yet, activate them :
    1. Open IIS Manager and navigate to the site to manage.
    2. In Features View, double-click Directory Browsing.
In the Actions pane, click Enable to enable the Directory Browsing feature .
  •  If the following error occurs : 
    Error Message : Handler “PageHandlerFactory-Integrated” has a bad module “ManagedPipelineHandler” in its module list
    Installation of CGI and ISAPI extensions is required  :
  • The following errors requires addition of the folder permission
    Error Message : The current identity does not have write access to ‘C:\Windows\Microsoft.NET\v4.0.30319\Temporary ASP.NET Files’

 
  1. Problem with HTTPPost request in IIS7
    1- ASP.NET MVC4 not handling POST requests in IIS7 integrated mode
    The default configuration inserts this in the web.config:
    The problem is the "*." path, If you change this to "*", then all requests will be handled by the ExtensionlessUrlHandler, including those to static files which will not be served anymore. So the solution is: Remove the POST verb from the handler entries and add new entries for the ExtensionlessUrlHandler with the path set to "*" and only for POST requests:
    Ideally remove the ones you don't need (x86 and x64 in classic vs. integrated pipeline).
    2- IIS 7  allow POST requests to html files
    Maybe 7  routing is different in respect to handing off a POST to an action that has no argument that can accept the post form data.
     ==> For this Reason I prefer working with Windows Azure