Monday, April 9, 2012

How to use ChannelFactory class in WCF client side code

ChannelFactory class is used in the way that create new channel with server side endpoints.
Msdn reference is here. http://msdn.microsoft.com/en-us/library/ms576132.aspx


The sample code is also here.
 static void Main(string[] args)  
 {  
   ChannelFactory<IEvalService> cf =   
     new ChannelFactory<IEvalService>("NetTcpBinding_IEvalService");  
   IEvalService channel = cf.CreateChannel();  

   Eval eval = new Eval();  
   eval.Submitter = "Shingo";  
   eval.Timesent = DateTime.Now;  
   eval.Comments = "I love WCF";  

   channel.SubmitEval(eval);  
   channel.SubmitEval(eval);  

   List<Eval> evals = channel.GetEvals();  

   Console.WriteLine("Number of evals : {0}", evals.Count);  
   ((IClientChannel)channel).Close();  

   Console.ReadLine();  
 }  

Simply, we can also write like this. EvalServiceClient class was generated when consuming  service reference.
 static void Main(string[] args)  
 {  
   EvalServiceClient channel = new EvalServiceClient("NetTcpBinding_IEvalService");  

   Eval eval = new Eval();  
   eval.Submitter = "Shingo";  
   eval.Timesent = DateTime.Now;  
   eval.Comments = "I love WCF";  

   channel.SubmitEval(eval);  
   channel.SubmitEval(eval);  

   List<Eval> evals = channel.GetEvals();  

   Console.WriteLine("Number of evals : {0}", evals.Count);  
   ((IClientChannel)channel).Close();  

   Console.ReadLine();  
 }  


In the code, I get the results of GetEvals method as List<>. You can set the results of collection type easily. The step is like this.

















You can choose here.


Friday, April 6, 2012

Running WCF with Console Application and introducing useful app.config editor tool

Usually, WCF runs on IIS Web server. But I studied that WCF can also run client application like console application, WPF application or more.

I also learned we can write service model element in app.config from code. (But we don't use usually.)

I'm gonna show you sample code.

 class Program  
 {  
   static void Main(string[] args)  
   {  
     ServiceHost host = new ServiceHost(typeof(EvalService));  
     host.AddServiceEndpoint(typeof(IEvalService),  
       new BasicHttpBinding(),  
       "http://localhost:8080/evals/basic");  
     host.AddServiceEndpoint(typeof(IEvalService),  
       new WSHttpBinding(),  
       "http://localhost:8080/evals/ws");  
     host.AddServiceEndpoint(typeof(IEvalService),  
       new NetTcpBinding(),  
       "net.tcp://localhost:8081/evals");  
     try  
     {  
       host.Open();  
       PrintServiceInfo(host);  
       Console.ReadLine();  
       host.Close();  
     }  
     catch (Exception ex)  
     {  
       Console.WriteLine(ex);  
       host.Abort();  
       Console.ReadLine();  
     }  
   }  
   static void PrintServiceInfo(ServiceHost host)  
   {  
     Console.WriteLine("{0} is up and running with these endpoints:",  
       host.Description.ServiceType);  
     foreach (ServiceEndpoint se in host.Description.Endpoints)  
       Console.WriteLine(se.Address);  
   }  
 }  

The result is like this.



So remarkably, you need run visual studio as Administrator.



  • WCF app.config Editor
You may feel pain to write app.config for WCF. There is excellent configuration tool for you.



You can easily edit app.config from GUI.


Thursday, April 5, 2012

Entry of Windows Communication Service (WCF)

I'm studying WCF. It's so nice and useful framework so that I'm going to log my understanding into the blog.


The simple WCF code is here.
 [DataContract]  
 public class Eval  
 {  
   [DataMember]  
   public string Submitter;  
   [DataMember]  
   public DateTime Timesent;  
   [DataMember]  
   public string Comments;  
 }  
 [ServiceContract]  
 public interface IEvalService  
 {  
   [OperationContract]  
   void SubmitEval(Eval eval);  
   [OperationContract]  
   List<Eval> GetEvals();  
 }  
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, 
    ConcurrencyMode=ConcurrencyMode.Multiple)]
 public class EvalService : IEvalService  
 {  
   List<Eval> evals = new List<Eval>();  
   public void SubmitEval(Eval eval)  
   {  
     evals.Add(eval);  
   }  
   public List<Eval> GetEvals()  
   {  
     return evals;  
   }  
 }  

So, I studied new things about ServiceBehavior attribute.
ServiceBehavior has two enum parameters mainly that are InstanceContextMode and ConcurrencyMode.
InstanceContextMode has Single, Percall, and Persession that are about instanciation. Also ConcurrencyMode has Single, Multiple, and Reentrant that are about threading.


In detail, here is msdn documents.
http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.aspx
http://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode.aspx
http://msdn.microsoft.com/en-us/library/system.servicemodel.concurrencymode.aspx


I'm going to continue this series depend on my study progress.
Thanks.