Par Nicolas,
jeudi 15 décembre 2011 à 05:05 ::geek stuff
Define a service to expose logic.
Expose the interface to this logic at a certain endpoint, certain channel (in our case BasicHTTPBinding).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using DM.PetShop;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// service relies on an implementation
ServiceHost host = new ServiceHost(typeof(OrderService));
// service is exposed through the intreface (i.e. contract)
host.AddServiceEndpoint(typeof(IOrderContract), new BasicHttpBinding(), "http://localhost:9000/PetShop");
// open the service ("fire and forget" call)
host.Open();
// make sure the service doesn't die and print status
Console.Title = String.Format("{0} is running ...", host.Description.Endpoints[0].Address);
Console.Write("Server started. Press enter to stop...");
Console.ReadLine();
// close after receiving a keystroke
host.Close();
Console.WriteLine("Server stoppped. Press enter to quit...");
Console.ReadLine();
}
}
}
Here is the contract (a.k.a. interface) that will be exposed to the world
Here the implementation (note the annotations,most important part)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace DM.PetShop
{
[ServiceBehavior(Name = "DM.PetShop.OrderService", Namespace = Constants.SERVICE_NAMESPACE)]
public class OrderService : IOrderContract
{
public void PlaceOrder(string orderData)
{
Console.WriteLine("Diagnostic message !");
}
}
}
Then write a client :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using IOrderContractChannelFactory = System.ServiceModel.ChannelFactory;
using DM.PetShop;
namespace Project_Client1
{
class Client1App
{
static void Main(string[] args)
{
IOrderContractChannelFactory factory = new IOrderContractChannelFactory(new BasicHttpBinding());
IOrderContract channel = factory.CreateChannel(new EndpointAddress("http://localhost:9000/PetShop/"));
channel.PlaceOrder("1 parrot");
IClientChannel clientChannel = (IClientChannel)channel;
try
{
if (clientChannel.State != CommunicationState.Closed)
clientChannel.Close();
}
catch
{
clientChannel.Abort();
}
}
}
}
Par Nicolas,
mercredi 14 décembre 2011 à 10:53 ::geek stuff
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
///
/// Summary description for JokeServer
///
public static class MOTDServer
{
static Queue MessageQueue = new Queue();
static Random rng = new Random();
static Thread ProducerThread;
static string[] Messages =
{
"A bird in the hand is worth two in the bush",
"A rolling stone gathers no moss",
"A penny saved is a penny earned",
"I'd rather have a bottle in front of me than a frontal lobotomy",
"When life hands you lemons, make lemonade"
};
private static void Init()
{
ProducerThread = new Thread(ProduceMessages);
ProducerThread.Priority = ThreadPriority.Lowest;
ProducerThread.Start();
}
public static string GetNextMessage()
{
if (ProducerThread == null)
Init();
string retval = null;
Monitor.Enter(MessageQueue);
while(MessageQueue.Count == 0)
Monitor.Wait(MessageQueue);
retval = MessageQueue.Dequeue();
Monitor.Exit(MessageQueue);
return retval;
}
static void ProduceMessages()
{
for (; ; )
{
if (MessageQueue.Count == 0)
{
Monitor.Enter(MessageQueue);
MessageQueue.Enqueue(Messages[rng.Next(Messages.Length)]);
Monitor.Pulse(MessageQueue);
Monitor.Exit(MessageQueue);
}
Thread.Sleep(rng.Next(5000) + 2000);
Debug.WriteLine("finished waiting");
}
}
}
Par Nicolas,
mercredi 14 décembre 2011 à 05:19 ::geek stuff
// define the delegate needed for async calls below
delegate List GetQuoteDelegate(DateTime dt);
// Use Async invocation to request the quotes in parallel
List[] GetQuotes(DateTime dt)
{
ReservationServices reservationService = new ReservationServices();
List[] retval = new List[3];
List getQuoteDelegates = new List();
getQuoteDelegates.Add(reservationService.GetAirlineQuotes);
getQuoteDelegates.Add(reservationService.GetCarRentalQuotes);
getQuoteDelegates.Add(reservationService.GetHotelQuotes);
CountdownEvent cde = new CountdownEvent(3);
for(int i=0; i<3; i++)
{
int localI = i;
GetQuoteDelegate getQuoteDelegate = getQuoteDelegates[i];
getQuoteDelegate.BeginInvoke(dt,
iar => {
retval[localI] = (List)getQuoteDelegate.EndInvoke(iar);
cde.Signal();
},
null
);
}
// wait for all to be finished
cde.Wait();
return retval;
}
Par Nicolas,
lundi 12 décembre 2011 à 08:48 ::geek stuff
Material : tinyurl.com/89 sj qvn
Iterators and anonymous methods
- yield keyword
- internal and external iterations
- anonymous methods : for throwaway methods ...
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
foreach (int i in DivisibleBy(0, 50, 5))
{
Console.WriteLine(i);
}
}
static IEnumerable DivisibleBy(int min, int max,int divisor) {
for (int i = min; i < max; i++)
{
if (i % divisor == 0) {
yield return i;
}
}
}
}
class X_Enumerable : IEnumerable {
private int min;
private int max;
private int divisor;
public X_Enumerable(int min, int max, int divisor)
{
this.min = min;
this.max = max;
this.divisor = divisor;
}
public IEnumerator GetEnumerator() {
return new X_Enumerator(min, max, divisor);
}
}
class X_Enumerator : IEnumerator
{
int min;
int max;
int divisor;
int current;
public X_Enumerator(int min, int max,int divisor)
{
this.min=min;
this.max = max;
this.divisor = divisor;
}
public Object Current
{
get { return current; }
}
public bool MoveNext()
{
this.current++;
while (current % divisor != 0) {
this.current++;
if (current == max) {
return false;
}
}
return true;
}
public void Reset()
{
this.current=min;
}
public void Dispose()
{
throw new NotImplementedException();
}
}
}
LINQ to Objects
- anonymous types :
var p = new { Name = "Andy", Age = 21 };@@
using System;
using System.Collections.Generic;
using System.Text;
namespace LINQBase
{
class Program
{
static void Main(string[] args)
{
var people = new List();
people.Add(new Person { Name = "Nick", Height = 1.30m });
people.Add(new Person { Name = "Jay", Height = 1.80m });
people.Add(new Person { Name = "Bob", Height = 2.30m });
foreach (var littlePerson in people.Where(p => p.Height < 2))
{
Console.WriteLine(littlePerson.Name);
}
}
}
public class Person
{
public decimal Height { get; set; }
public string Name { get; set; }
}
public static class Utils {
public static IEnumerable Where(this IEnumerable items, Predicate condition)
{
foreach (T item in items)
{
if (condition(item))
yield return item;
}
}
}
}
LINQ to XML
CLR via C#, Second EditionJeffrey Richter.Net 2.0.Net General
http://www.amazon.com/gp/product/0735621632/sr=8-1/qid=1141842947/ref=pd_bbs_1/103-3403410-7338268?_encoding=UTF8
35.99Creating Workflow Services and Durable Services (MSDN)Anonymous.Net 3.5Service Composition
http://msdn2.microsoft.com/en-us/library/bb412181.aspx
0.0Data Binding with Windows Forms 2.0Brian Noyes.Net 2.0Windows Forms
http://www.amazon.com/Data-Binding-Windows-Forms-2-0/dp/032126892X/ref=pd_bxgy_b_img_b
32.99Essential ASP.NET 2.0Fritz OnionKeith Brown.Net 2.0Web Apps
http://www.amazon.com/Essential-ASP-NET-2-0-Fritz-Onion/dp/0321237706/ref=pd_bbs_sr_1/103-2749730-3599818?ie=UTF8&s=books&qid=1176902837&sr=8-1
36.99Essential ASP.NET With Examples in C#Fritz Onion.Net 2.0Web Apps
http://www.amazon.com/Essential-ASP-NET-Examples-Fritz-Onion/dp/0201760401/ref=pd_bbs_sr_2/103-2749730-3599818?ie=UTF8&s=books&qid=1176902837&sr=8-2
31.49Essential C# 2.0Mark Michaelis.Net 2.0C# Language
http://www.amazon.com/Essential-2-0-Microsoft-NET-Development/dp/0321150775/ref=pd_bxgy_b_text_b/102-4792916-8428135?ie=UTF8&qid=1174210886&sr=1-2
37.79Essential Windows Presentation FoundationChris Anderson.Net 3.0Presentation Foundation
http://www.amazon.com/Essential-Presentation-Foundation-Microsoft-Development/dp/0321374479/ref=sr_1_1/002-9650384-5136050?ie=UTF8&s=books&qid=1188296520&sr=8-1
31.49Essential Windows Workflow FoundationDharma ShuklaBob Schmidt.Net 3.0Workflow Foundation
http://www.amazon.com/Essential-Workflow-Foundation-Microsoft-Development/dp/0321399838/ref=pd_bbs_sr_1/102-1611469-3153757?ie=UTF8&s=books&qid=1192436658&sr=8-1
31.49Learning WCFMichele Bustamante.Net 3.0Communication Foundation
http://www.amazon.com/Learning-WCF-Hands-Michele-Bustamante/dp/0596101627/ref=pd_bbs_sr_1/103-2749730-3599818?ie=UTF8&s=books&qid=1176904112&sr=8-1
29.69LINQ in ActionFabrice MarguerieSteve EichertJim Wooley.Net 3.5LINQ
http://www.amazon.com/LINQ-Action-Fabrice-Marguerie/dp/1933988169/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1202326210&sr=1-1
29.69Programming .NET Components, 2nd EditionJuval Lowy.Net 2.0.Net General
http://www.amazon.com/gp/product/0596102070/ref=pd_kar_gw_1/103-3403410-7338268?_encoding=UTF8&v=glance&n=283155
29.67Programming WCF ServicesJuval Lowy.Net 3.0Communication Foundation
http://www.amazon.com/Programming-WCF-Services-Juval-Lowy/dp/0596526997/ref=pd_bbs_sr_1/102-1611469-3153757?ie=UTF8&s=books&qid=1192436749&sr=8-1
29.69Programming Windows Workflow FoundationK. Scott Allen.Net 3.0Workflow Foundation
http://www.amazon.com/Programming-Windows-Workflow-Foundation-Techniques/dp/1904811213/ref=sr_1_1/102-1611469-3153757?ie=UTF8&s=books&qid=1192436585&sr=8-1
44.99Smart Client Deployment with ClickOnceBrian Noyes.Net 2.0Windows Forms
http://www.amazon.com/Smart-Client-Deployment-ClickOnce-Applications/dp/0321197690/ref=pd_bbs_sr_1/102-5394843-6848112?ie=UTF8&s=books&qid=1175345601&sr=8-1
29.69Understanding Windows CardSpaceVittorio BertocciGarrett SerackCaleb Baker.Net 3.0CardSpace
http://www.amazon.com/Understanding-Windows-CardSpace-Introduction-Independent/dp/0321496841/ref=sr_1_1/102-1611469-3153757?ie=UTF8&s=books&qid=1192436821&sr=8-1
32.84Windows Forms 2.0 Programming (2nd Edition)Chris SellsMichael Weinhardt.Net 2.0Windows Forms
http://www.amazon.com/gp/product/0321267966/qid=1141845579/sr=1-1/ref=sr_1_1/103-3403410-7338268?s=books&v=glance&n=283155
32.39Windows Presentation Foundation UnleashedAdam Nathan.Net 3.0Presentation Foundation
http://www.amazon.com/Windows-Presentation-Foundation-Unleashed-WPF/dp/0672328917/ref=pd_bbs_sr_1/103-2749730-3599818?ie=UTF8&s=books&qid=1176903814&sr=8-1
34.49
public class Book
{
// Book properties
public string Category { get; set; }
public string Topic { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public string[] Authors { get; set; }
public string WebSite { get; set; }
// Book description
public override string ToString()
{
string output = string.Format("{0} by {1}",
Title, string.Join(", ", this.Authors));
output += string.Format(" {0} {1} {2}",
Category, Topic, Price.ToString("c"));
return output;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace LinqToXmlLab
{
static class RssFeed
{
public static XElement GetBooksXml(string url)
{
XElement xml = XElement.Load(url);
return xml;
}
public static List GetBooks(XElement xml)
{
XNamespace ns = "http://develop.com";
IEnumerable booksQuery = from b in xml.Elements(ns + "book") select
new Book { Title = (string)b.Element(ns + "title"),
Category = (string)b.Element(ns + "category"),
Topic = (string)b.Element(ns+"topic"),
Price = (decimal)b.Element(ns + "price"),
WebSite = (string)b.Element(ns + "link"),
Authors = (from a in b.Element(ns + "authors").Elements(ns + "author") select (string)a).ToArray()
};
return booksQuery.ToList();
}
public static XElement GetFeedXml(List books) {
XElement xml = new XElement("rss",
new XAttribute("version", "2.0"),
new XElement("channel",
new XElement("title", "Essential .Net Reading"),
new XElement("link", "http://localhost:12345/books/"),
new XElement("description", ".Net developer reading"),
from b in books orderby b.Category, b.Topic, b.Title select new XElement(
"item",
new XElement("title", b.Title),
new XElement("link", b.WebSite),
new XElement("description", b))));
return xml;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Linq;
namespace LinqToXmlLab
{
class Program
{
static void Main(string[] args)
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:12345/books/");
listener.Start();
Console.WriteLine("Rss Feed Service is running");
Console.WriteLine("Press Ctrl+C to terminate ...");
while (true)
{
HttpListenerContext ctx = listener.GetContext();
using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream))
{
// Load xml from file
string url = @"..\..\books.xml";
XElement booksXml = RssFeed.GetBooksXml(url);
// Get books list
List books = RssFeed.GetBooks(booksXml);
// Convert books list to xml
XElement feedXml = RssFeed.GetFeedXml(books);
// Send xml to the reponse stream
writer.Write(feedXml);
}
}
}
}
}