Sunday, November 30, 2014

How to get 50 GB of FREE High-speed Secure Storage on the Cloud


This post is about How to get 50 GB of FREE High-speed Secure Storage on the Cloud, using the Mega storage service , which is very fast and very secure, and count as the more big , fast and secure FREE Cloud Storage by the date we're writing this article.
We'll use the 50 GB FREE cloud storage offer from Mega , not just because it's fast and secure, but also because it can synchronize folders on our machines, and because has private encryption, deleted files recovery, files versioning, and also offers very good mobile apps.
By the time we write this, Dropbox and SpiderOak  are offering 2 GB of free storage, Box 10 GB, Google 15 GB   shared between Gmail, Google Drive and other services, Microsoft's OneDrive (SkyDrive) and Copy (Barracuda Networks) offer also 15 GB, and BitCasa just 20 GB .
Tencent Weiyun is now offering 10 TB free storage , and you can take a look at their web site. But at first the UI is in chinese, and security matters are far more developed on Mega.
As Dropbox and other storage services, Mega has a folder's synchronization virtual drive, but it's better since it can be configured to synchronize not only one single folder, but any folders we want.
At Mega Storage Cloud, we can share any folder to whoever we want,  also setting permissions  for readonly, readwrite or full access. This way, we can give read-write permissions to a co-worker, which will be required to have another (FREE) Mega account. However, if we intend to share publicly some file, this won't require the downloaders to hold Mega accounts.
The Mega Storage Cloud is very fast: there are reports of 10 Mbps for uploadings, and ~30 Mbps for downloadings.
Mega security is far stronger than other Cloud Storages: all files and data are stored fully encrypted in the cloud , and they are decrypted only in the downloader machine, yours or whoever's you shared your data with. The master encryption key is based on your personal password: be very careful to save it in a safe place, because if lost, all your data is lost.
Restoring deleted files is as easy like to selecting them from the "Rubbish Bin", where our deleted files are kept, and selecting restore.
How to get 50 GB of FREE High-speed Secure Storage on the Cloud



 How to get 50 GB of FREE High-speed Secure Storage on the Cloud



First , we create a FREE personal account in the Mega web site:
How to get 50 GB of FREE High-speed Secure Storage on the Cloud 1

How to get 50 GB of FREE High-speed Secure Storage on the Cloud 2



How to get 50 GB of FREE High-speed Secure Storage on the Cloud 3



Then, you'll have to confirm your account from your email personal account:


How to get 50 GB of FREE High-speed Secure Storage on the Cloud 4


You can select the FREE 50 GB account, or some account more suited to your necessities:

How to get 50 GB of FREE High-speed Secure Storage on the Cloud 5





After you confirmed your account, you are exposed to the Mega UI:


How to get 50 GB of FREE High-speed Secure Storage on the Cloud 6

The "Cloud Drive" is the place where all our files are stored. The "Rubbish Bin" is where our deleted files are saved, and the "Contacts" are the emails of the people which we shared files with:



How to get 50 GB of FREE High-speed Secure Storage on the Cloud 7


You then can proceed to you first upload.


How to get 50 GB of FREE High-speed Secure Storage on the Cloud 8

You'll be surprised of the speed and security of this Cloud Storage.

 Enjoy!!!!


By Carmel Shvartzman

עריכה: כרמל שוורצמן

Saturday, November 29, 2014

Step by step how to send an HTTP PUT Request to a RESTful WCF Service using Fiddler

In this post we'll learn how to send an HTTP PUT Request to a RESTful WCF application using Fiddler
We'll use Fiddler to test an RESTful WCF application, sending an HTTP PUT request. We'll start with a working WCF application,  and we'll update an entry using Fiddler, showing as follows :



First of all, we have to download the FREE Fiddler tool from this web site :



After installing it , study the settings of the Operation Contract at the WCF Service Contract, in order to fill exactly what it is expecting as a request:
http put request to restful wcf using fiddler

The WebInvoke handles the PUT HTTP verb, and the request format must be JSON, and so the response format. Therefore, we'll send exactly what the WCF wants.
Open Fiddler and click the "Composer" option:




Then type in the URL and select "PUT" HTTP request method from the list. Also, set the request Headers as follows: (don't worry about the Content-Length, because Fiddler will fill  it for you):



Also, fill the Request Body :  be careful to fill adequately the field names of the Entity you want to update, most of all, the ID of the record to change.

Press the "EXECUTE" button at the Fiddler Composer, to send the request. If you set a breakpoint inside the WCF HTTP PUT handler, you'll see the bindings in action :


As you see, WCF succeeded at binding the JSON it got with the Blog object expected.
The response is then sent to Fiddler, with an "true" value:


And the database shows the updated values:



If you want to take a look at the RESTful WCF which we're using here, see this tutorial.

That's all... 
In this tutorial we've learned how to send an HTTP PUT Request to a RESTful WCF application using Fiddler. 
Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן










Wednesday, November 26, 2014

How to use ExecuteStoreQuery with parameters on the Entity Framework


This tutorial is an example of  How to use the ExecuteStoreQuery method with parameters on the  Entity Framework, thus directly executing SQL commands against the Model Data Source.
We'll use a stored procedure that takes in a string parameter and returns some records, which we'll store inside a generic List<>. Using SQL commands or stored procedures is an effective way to retrieve only the records that you need, instead of fetching ALL the table data, to apply on it some kind of filtering using Where() or Single() methods, with all the performance costs that this later imply.

We'll exemplify running commands against the database on two ways:
1) using Stored Procedures
2) using an SQL command


 How to use ExecuteStoreQuery with parameters on the Entity Framework



1) For our first example, we create a stored procedure which takes a parameter and return three records using the received argument:

How to use ExecuteStoreQuery with parameters on the Entity Framework



Next, we define an SqlParameter for sending some text to the procedure:
How to use ExecuteStoreQuery with parameters on the Entity Framework 1

 SqlParameter p_code = new SqlParameter("code", "TEST");
            List<string> resultComplaintStateCode =
                                (from c in  Context
                                     .ExecuteStoreQuery<string>("GetText @code" ,p_code
                                 )
                                 select c).ToList();



 Notice that we use Linq to insert the returned records inside a generic List<> :

How to use ExecuteStoreQuery with parameters on the Entity Framework 2



Is really straightforward. However, if you use ExecuteMethodCall() method instead of ExecuteStoreQuery(), you'll need to update the Entity Framework Data Model, importing the stored procedure:
How to use ExecuteStoreQuery with parameters on the Entity Framework 3



How to use ExecuteStoreQuery with parameters on the Entity Framework 4




2) As the ExecuteStoreQuery method's name implies, we can also use it to execute some SQL command against the database.
For instance, let's copy the same SQL code from the stored procedure, and execute it directly from the C# code:
How to use ExecuteStoreQuery with parameters on the Entity Framework 5


 As you see, the SQL command was executed according to the three parameters that we sent :

How to use ExecuteStoreQuery with parameters on the Entity Framework 6




That's all!!!!


By Carmel Shvartzman

עריכה: כרמל שוורצמן

Monday, November 24, 2014

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'


In this article we see How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'.
How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'




 How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled'


This serialization error is originated when your Data Model contains an Entity with a property which references an object of the same type of the container class, or when you have many entities with relationships between them, and several of them are referenced by others in such a way that creates a circular self reference.
Let's say we have the following data Model , in which a "Blog" contains a reference to its "Blogger" , but this later owns many "Blogs":

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 1

That means, if you open some Blog , and open its Blogger property, you will then see the Blogs that the Blogger owns, and , between them , the original Blog that you opened earlier, and that is a circular reference.
This can happen for instance, when you have a WCF service or a Web API which use eager loading:


How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 2

The MSDN web page for this "contains cycles and cannot be serialized if reference tracking is disabled" error  addresses only the first study case, in which an object reference itself: for example, a "Person" class contains a property referencing some other "Person":

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 3

As it states, the solution is to add to the DataContract Attribute the "IsReference" boolean value set to "true". However , in most cases, you're prone to find the second XML-JSON serialization problem, in which several entities are related by ONE-TO-MANY relationships:

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 4

In this case, just make a list of the classes that appear REFERENCED by other classes, that means, the entities which are at the " 0 .. 1 " side of any one-to-many relationship. In our example, there are ONLY two classes like that: Blogger and Blog, because "Comment" is not referenced by any other.
Therefore, open the classes files and add to the DataContract attribute the "IsReference" set to true:

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 5

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 6

This way, the reference tracking will be enabled and the circular references will be fixed.
If you like, it's possible to define "Metadata classes" and set the Attributes there: continuing with our example, that will be like this:
How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 7



After compilation-run, the WCF or WebAPI will render the data avoiding cycles, as follows:

How to fix the WCF - Web API Error 'contains cycles and cannot be serialized if reference tracking is disabled' 8


Here you can see that a "Blog" contains a "Blogger" property, and when it tries to reference the containing object, it will be use the reference " z:Id=i1 " . 
Same thing for the "Comments", while trying to reference the containing "Blog".

That's all!!!!


By Carmel Shvartzman

עריכה: כרמל שוורצמן

Wednesday, November 19, 2014

How to expose a WCF Service through many endpoints and multiple bindings

  
In this post we'll explore how to expose one single WCF Service through many  endpoints and multiple bindings. We'll have only one C# WCF code, and we'll make it implement several interfaces in order to expose several endpoints with many different bindings. Technically, we'll create several WCF services via creating several interfaces, but the C# class implementing them will be running the SAME CODE. We'll enable that unique C# class code to handle both Soap and HTTP requests, proceeding from backends calls, from jQuery Ajax calls, and from standard HTTP web calls. Some calls will be made programmatically in C# from some Application Back-End . Other, REST calls, will proceed from Ajax or from regular web calls. But ALL requests will be handled by the SAME C# service class.

How to expose a WCF Service through many endpoints and multiple bindings


Another interesting feature of this exercise will be, we are not creating a WCF Application, but adding a WCF Web Service to an existing MVC application.
The schema of our WCF app will be as follows : one unique C# Class, which implements two Interfaces as Services Contracts, finally exposed through three Endpoints:
How to expose a WCF Service through many endpoints and multiple bindings



One of the Endpoints will handle Soap calls, one HTTP web calls, and another one Ajax HTTP web calls. The first one will use an basicHttpBinding, and the former two, an webHttpBinding.

The WCF service we are building will handle HTTP requests based on the REST (REpresentational State Transfer) architecture, designed by Dr. Fielding at 2000. Our WCF sample will receive HTTP requests depending what it intends to do:

1) CREATE an item :     HTTP "POST" request
2) RETRIEVE  an item : HTTP "GET" request
3) RETRIEVE  ALL items : HTTP "GET" request

We'll want to create an WCF Web Service with CRUD functionality, which fetches and persists all data proceeding from a RESTful Web Service, based on a Model as follows:


Step 1 - Add a WCF Service to your application



Step 2 - Write the C# code for the WCF Service


[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class BlogService : IBlogService,IBlogService1
    {
        public string GetBlogs()
        {
            Models.BlogEntities ctx = new Models.BlogEntities();
            ctx.Configuration.ProxyCreationEnabled = false;
            List<Blog> data = ctx.Blogs.ToList();

            return new JavaScriptSerializer().Serialize(data);
        }

        public string GetBlog(string Id)
        {
            Models.BlogEntities ctx = new Models.BlogEntities();
            ctx.Configuration.ProxyCreationEnabled = false;
            int id = Convert.ToInt32(Id);
            Blog data = ctx.Blogs.Where(b => b.BlogID == id).FirstOrDefault();

            return new JavaScriptSerializer().Serialize(data);
        }

        public string CreatePost(Blog post)
        {
            Blog responsePost = post;
            return new JavaScriptSerializer().Serialize(responsePost);
        }
    }

Why do we create first the service implementation and after that the interfaces? Because we know exactly what our class is supposed to do, but only later we'll decide how many Service Contracts we'll need.

Step 3 - Create the C# interfaces as Service Contracts

In this example, we create two Service Contracts through two Interfaces: the first one look this way:



[ServiceContract]
    public interface IBlogService
    {
        [OperationContract]
        [WebGet]
        string GetBlogs();
        [OperationContract]
        [WebGet]
        string GetBlog(string Id);
        [WebInvoke(Method = "POST")]
        [OperationContract]
        string CreatePost(Blog post);
    }

The second one is similar but has the Operation Contracts names changed:


[ServiceContract]
    public interface IBlogService1
    {
        [OperationContract(Name = "GetBlogsSoapREST")]
        [WebGet]
        string GetBlogs();
        [OperationContract(Name = "GetBlogSoapREST")]
        [WebGet(UriTemplate="GetBlog1/{id}")]
        string GetBlog(string Id);
        [WebInvoke(Method = "POST")]
        [OperationContract(Name = "CreatePostSoapREST")]
        string CreatePost(Blog post);
    }

Then, we make our Service Class to implement the Interfaces:




Step 4 - Declare the WCF Service & its Endpoints

At the web.config, declare the service with its service behavior:



This service behavior states that the service metadata can be fetched , using the declaration:

<serviceBehaviors >
        <behavior  name="MultipleInterfacesAndEndpointsSvc" >
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

Also we want that , at this development stage of our application, the errors could be view by us:
includeExceptionDetailInFaults="true"

Now we create three Endpoints:


 <service name="WCF_RESTful.BlogService" 
               behaviorConfiguration="MultipleInterfacesAndEndpointsSvc" >
       <endpoint 
         address="RESTfulAjax" 
         behaviorConfiguration="RESTfulAjaxBehavior"  
         binding="webHttpBinding" 
         contract="WCF_RESTful.IBlogService" />
        <endpoint 
          address="RESTfulNoAjax" 
          behaviorConfiguration="RESTfulNoAjaxBehavior"
          binding="webHttpBinding" 
          contract="WCF_RESTful.IBlogService1" />
        <endpoint 
          address="SoapEndpoint"  
          binding="basicHttpBinding" 
          contract="WCF_RESTful.IBlogService1" />
      </service>

The first two are for REST calls, that's why we use webHttpBinding. Each one exposes a different Service Contract (interface).
The last is for Soap calls, and therefore uses basicHttpBinding.
Now , to handle regular HTTP REST calls, we need to bind a "webHttp" behavior to the Endpoint:



 <behavior  name="RESTfulNoAjaxBehavior" >
          <webHttp />
        </behavior>

And to handle Ajax web calls, we declare another behavior using "enableWebScript", which also includes the "webHttp" directive:

Step 5 - Routing

Finally, reroute the path since we are inside an MVC application:

routes.IgnoreRoute("BlogService.svc/{*pathInfo}");


Build & run, and let's call our WCF from :
1) Soap request: we create a service reference in some MVC application, and discover our WCF with its methods:

The WSDL schema appears as this:



Now, just build the back end client Proxy and use the service.

2) Web HTTP REST request: XML response : 


And for getting just one record :



As you see, the response is given in XML format. Notice that we needed to call an "GetBlog1" method: that's because we defined an UriTemplate at the Operation Contract , and because of the format "GetBlog1/{id}" we don't need to specify the "GetBlog1?id=4" argument: 


3) Ajax REST HTTP request: Json response : 



Here, the response is Json. And to get only one record:




That's all!! 
In this tutorial we've learn How to expose a WCF Service through many endpoints and multiple bindings.  

Happy programming.....
      By Carmel Shvartzman
כתב: כרמל שוורצמן