Thursday, June 27, 2013

Durable Service in wcf

Durable services are WCF services that persist service state information even after service host is restarted or Client. It means that durable services have the capability to restore their own state when they are recycled. It can use data store like SQL database for maintain instance state. It is new feature in .Net 3.5
You might think that we can also maintain session using WCF sessions, but content in the session environment is not persisted by default. If the service is shut down or client closes the proxy, data will be lost. But in case of Durable service it is still maintained.

Working:

When Durable service is created with database as data store, it will maintain all its state information in the table.
When a client make a request to the service, instance of the service is serialized, a new GUID is generated. This serialized instance xml and key will be saved in the database. We will call this GUID as instanceID. Service will send the instanceID to the client, so later it can use this id to get the instance state back. Even when client is shut down, instanceId will be saved at the client side. So when ever client opening the proxy, it can get back the previous state.

Defining the Durable Service

Durable service can be implemented using [DurableService()] attribute. It takes 'CanCreateInstance' and 'CompletesInstance' property to mention on which operation instance state has to be saved and destroyed.
  • CanCreateInstance = true: Calling this operation results in creating the serialization and inserting it into the datastore.
  • CompletesInstance = true: Calling this operation results in deleting the persisted instance from the datastore.
    [Serializable]
    [DurableService()]
    public class MyService :IMyservice
    {
        [DurableOperation(CanCreateInstance = true)]
        public int StartPersistance()
        {
            //Do Something
        }

     [DurableOperation(CompletesInstance = true)]
        public void EndPersistence()
        {
            //Do Something
        }
    }

Wednesday, June 26, 2013

How to use contains ,Between and List generic in Linq with MVC?

public class CustomerController : Controller

{

List<Customer> Customers = new List<Customer>();

public CustomerController()

{

string []str = {"1","2","12"};



var dataContext = new CustDBDataContext();

Customers = (from m in dataContext.tbl1s where str.Contains(Convert.ToString(m.Id)) && m.Id>0 && m.Id<12

select new Customer { Id = (int)m.Id, Name = m.Name, Amount = (double)m.Amount }

).ToList();

}

public ViewResult DisplayCustomer(int id)

{

List<Customer> objCustomer1 = Customers.FindAll(delegate(Customer cst) { return cst.Id == id; });

Customer objCustomer = objCustomer1[0];

return View("DisplayCustomer", objCustomer);

}

public ViewResult Details()

{

return View(Customers);

}

}

How to display and view details in MVC?

//Controller
 //Controller/CustomerController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyFirstMVC.Models;
namespace MyFirstMVC.Controllers
{
public class CustomerController : Controller
{
List<Customer> Customers = new List<Customer>();
public CustomerController()
{
var dataContext = new CustDBDataContext();
var Customers1 = (from m in dataContext.tbl1s
select m).ToList();
foreach (var c in Customers1)
{
Customer obj1 = new Customer();
obj1.Id = (int)c.Id;
obj1.Name = c.Name;
obj1.Amount = (double)c.Amount;
Customers.Add(obj1);
}
}
public ViewResult DisplayCustomer(int id)
{
List<Customer> objCustomer1 = Customers.FindAll(delegate(Customer cst) { return cst.Id == id; });
Customer objCustomer = objCustomer1[0];
return View("DisplayCustomer", objCustomer);
}
public ViewResult Details()
{

return View(Customers);
}
}
}



//Model
 //Model/Customer.cs
using System;

using System.Collections.Generic;

using System.Web;

using System.Linq;

using System.ComponentModel;

namespace MyFirstMVC.Models



{

public class Customer

{

private int _Id;

private string _Name;

private double _Amount;

public int Id

{

set

{

_Id = value;

}

get

{

return _Id;

}

}

public string Name

{

get

{

return _Name;

}

set

{

_Name = value;

}

}

public double Amount

{

set

{

_Amount = value;

}

get

{

return _Amount;

}

}



}



}


// View Display//
//Views/Customer/Details.aspx<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyFirstMVC.Models.Customer>>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Details</title>

</head>

<body>

<h2><%: ViewData["Message"] %></h2>



<table>

<tr>

<th>


CustomerId
</th>

<th>


CustomerName
</th>

<th>


CustomerAddress
</th>

<th>


CustomerMobileNo
</th>

<th>


CustomerCountry
</th>

<th></th>

</tr>

<% using(Html.BeginForm()) { %>

<% foreach (var item in Model)

{%>

<tr>

<td>

<%=item.Id%>





</td>

<td>

<%=item.Name%>



</td>

<td>

<%=item.Amount%>



</td>

<td>

<%=Html.ActionLink("Details", "DisplayCustomer", new { id = item.Id })%>





</td>



</tr>

<%}

%>

<% }

%>



</table>



</body>

</html>
//Details//
////Views/Customer//DisplayCustomer.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MyFirstMVC.Models.Customer>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>DisplayCustomer</title>

</head>

<body>

<div>

The customer id is <%= Model.Id %> <br />

The customer Code is <%= Model.Name %> <br />

<% if (Model.Amount > 100) {%>



This is a priveleged customer
 
<% } else{ %>



This is a normal customer

<%} %>
 
</div>

</body>

</html>


Enter Url//../Customer/Details