Posts

Showing posts from March, 2012

WCF P2P example

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

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 ApiControllerpublicclass 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.Name = product.Name; entities.SaveChanges()…

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

Install VS2010 SP1 and SQL CE Tools for Visual StudioNow 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

Image
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.csDatabaseEntities is the Entity Framework 4 model with single table Product with 2 columns (Id uniqueidentity, Name nvarchar(100))publicclass 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); entities.SaveChanges(); return PartialView("Produ…