Showing posts from March, 2012

WCF P2P example

Create empty c# Console project and add reference to System.ServiceModel Message interface and class [ServiceContract(CallbackContract = typeof (IMessage))] public interface IMessage { [OperationContract(IsOneWay = true )] void Send( string sender, string message); } class MessageImpl : IMessage { public string Name { get ; set ; } public MessageImpl( string name) { Name = name; } public void Send( string sender, string message) { if (!Name.Equals(sender)) { Console.WriteLine(" {0}: {1} ", sender, message); } } } Messenger public class Messenger { public string Name { get ; private set ; } public IMessage Channel; public IMessage Host; private DuplexChannelFactory<IMessage> _factory; private readonly AutoResetEvent _stopped = new AutoResetEvent( false ); private readonly AutoResetEvent _started = new AutoResetEvent( false ); pu

MVC3 Restful JSON example

This is a Restful API for the MVC3 example from the previous post . Get: GET /Api/Product/{id} Create: POST /Api/Product Update: PUT /Api/Product Delete: DELETE /Api/Product/{id} Create ApiController public class ApiController : Controller { DatabaseEntities entities = new DatabaseEntities(); [HttpGet] [ActionName(" Product ")] public JsonResult Get(Guid id) { return Json(entities.Products.FirstOrDefault(x => x.Id == id), JsonRequestBehavior.AllowGet); } [HttpPost] [ActionName(" Product ")] public JsonResult Create(Product product) { product.Id = Guid.NewGuid(); entities.Products.AddObject(product); entities.SaveChanges(); return Json(product); } [HttpPut] [ActionName(" Product ")] public JsonResult Update(Product product) { var current = entities.Products.FirstOrDefault(x => x.Id == product.Id); current

Adding SQL Server CE 4.0 (Compact Edition) support to VS2010

Install VS2010 SP1 and SQL CE Tools for Visual Studio Now you can add SQL Server Compact 4.0 Local Database to your project and connect to that database with ADO.NET Entity Data Model (Entity Framework 4). More info on ScottGu’s Blog

How to use MVC3 with AJAX

To enable AJAX calls add this line to Views/Shared/_Layout.cshtml <script src=" @Url.Content( "~/Scripts/jquery.unobtrusive-ajax.min.js" ) " type=" text/javascript "></script> Add new Products tab to menu in Views/Shared/_Layout.cshtml <li>@Html.ActionLink(" Product ", " Index ", " Product ")</li> Create new controller Controllers/ProductController.cs DatabaseEntities is the Entity Framework 4 model with single table Product with 2 columns (Id uniqueidentity, Name nvarchar(100)) public class ProductController : Controller { DatabaseEntities entities = new DatabaseEntities(); public ActionResult Index() { return View(entities.Products); } [HttpPost] public ActionResult AddProduct( string name) { var product = new Product(); product.Id = Guid.NewGuid(); product.Name = name; entities.Products.AddObject(product);