My WCF Note(1): A Simple WCF App with Self-Hosting

1. Solution Structure

image

Add Reference to project Contract and Service

image


 

2.Code

 (1)Contracts.ICalculator
using System.ServiceModel; 
namespace Leon.WcfServices.Contracts 
{ 
    [ServiceContract(Name = "CalculatorService", Namespace = "http://www.leon.com")] 
    public interface ICalculator 
    { 
        [OperationContract] 
        double Add(double x, double y); 
        [OperationContract] 
        double Subtract(double x, double y); 
        [OperationContract] 
        double Multiply(double x, double y); 
        [OperationContract] 
        double Divide(double x, double y); 
    } 
} 
(2)Services.CalculatorService
using Leon.WcfServices.Contracts; 
namespace Leon.WcfServices.Services 
{ 
    public class CalculatorService : ICalculator 
    { 
        #region ICalculator Members 
        public double Add(double x, double y) { return x + y; } 
        public double Subtract(double x, double y) { return x - y; } 
        public double Multiply(double x, double y) { return x * y; } 
        public double Divide(double x, double y) { return x / y; } 
        #endregion 
    } 
} 
(3)Using ServiceHost to create Self-Hosting
using System; 
using System.ServiceModel; 
using System.ServiceModel.Description; 
using Leon.WcfServices.Contracts; 
using Leon.WcfServices.Services; 

namespace Leon.WcfServices.Hosting 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            using (ServiceHost host = new ServiceHost(typeof(CalculatorService))) 
            { 
                host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice"); 
                if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null) 
                { 
                    var behavior = new ServiceMetadataBehavior(); 
                    behavior.HttpGetEnabled = true; 
                    behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata"); 
                    host.Description.Behaviors.Add(behavior); 
                } 
                host.Opened += delegate { Console.WriteLine("CalculatorService has been started, press any key to stop the service!"); }; 
                host.Open(); 
                Console.Read(); 
            } 
        } 
    } 
} 

(4)Run the Host and test the service:

Run the Host
image


Service has been started
image
Test the Service:
image

(5)Create consuming Client

(5.1) Using the proxy which is automatically created by Add Service Reference
image image

  

image image

  

 Client.cs

using System; 
using System.ServiceModel; 
using Client.CalculationServices; 
using Leon.WcfServices.Contracts;

namespace Leon.WcfServices.Client 
{ 
    class Program 
    { 
        static void Main(string[] args) 
       { 
           //using the proxy [CalculatorServiceClient] which is automatically created by "Add Service Reference" 
            using (CalculatorServiceClient proxy = new CalculatorServiceClient()) 
            { 
                Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2)); 
                Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2)); 
                Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2)); 
                Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2)); 
                Console.ReadLine(); 
            } 
        } 
    } 
} 
(5.2) Create the proxy by myself using ChannelFactory
            using (var channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(),"http://127.0.0.1:9999/Calculatorservice"))
            {
                var proxy = channelFactory.CreateChannel();
                Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, proxy.Add(1, 2));
                Console.WriteLine("x - y = {2} when x = {0} and y = {1}", 1, 2, proxy.Subtract(1, 2));
                Console.WriteLine("x * y = {2} when x = {0} and y = {1}", 1, 2, proxy.Multiply(1, 2));
                Console.WriteLine("x / y = {2} when x = {0} and y = {1}", 1, 2, proxy.Divide(1, 2));
                Console.ReadLine();
            }
(5.3) Test the Client

image

posted @ 2012-04-11 03:39  Jian, Li  Views(251)  Comments(0)    收藏  举报