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.


No comments:

Post a Comment