Using ServiceHost and ChannelFactory

The ServiceHost class gives you access to the WCF hosting infrastructure on the server side, whereas the
ChannelFactory class gives you access to the WCF hosting infrastructure on the client side.

 

ServiceHost Sample:

using System;
using System.ServiceModel;
using QuickReturns.StockTrading.ExchangeService;
using QuickReturns.StockTrading.ExchangeService.Contracts;

namespace QuickReturns.StockTrading.ExchangeService.Hosts
{
     public  class ExchangeServiceHost
    {
         static  void Main( string[] args)
        {
            Uri baseAddress =
             new Uri( " http://localhost:8080/QuickReturns ");
            CustomServiceHost host =
             new CustomServiceHost( typeof(TradeService), baseAddress);
            host.Open();
            Console.WriteLine( " Service started: Press Return to exit ");
            Console.ReadLine();
        }
    }
    
     public  class CustomServiceHost : ServiceHost
    {
         public CustomServiceHost(Type serviceType,  params Uri[] baseAddresses)
        :  base(serviceType, baseAddresses)
        {
        }
        
         protected  override  void OnInitialize()
        {
            BasicHttpBinding binding =  new BasicHttpBinding();
            AddServiceEndpoint( typeof(ITradeService), binding,  " Exchange ");
        }
    }
}

 

 

ChannelFactory:

 

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
namespace QuickReturns.StockTrading.ExchangeService.Clients
{
    [ServiceContract(Namespace =  " http://QuickReturns ")]
     interface ITradeService
    {
        [OperationContract()]
        Quote GetQuote( string ticker);
        
        [OperationContract()]
         void PublishQuote(Quote quote);
    }
    
    [DataContract(Namespace =  " http://QuickReturns ", Name =  " Quote ")]
     public  class Quote
    {
        [DataMember(Name =  " Ticker ")]
         public  string Ticker;
        
        [DataMember(Name =  " Bid ")]
         public  decimal Bid;
        
        [DataMember(Name =  " Ask ")]
         public  decimal Ask;

        [DataMember(Name =  " Publisher ")]
         public  string Publisher;

        [DataMember(Name =  " UpdateDateTime ")]
         private DateTime UpdateDateTime;
    }
    
     class ExchangeServiceSimpleClient
    {
         static  void Main( string[] args)
        {
            EndpointAddress address =
                 new EndpointAddress( " http://localhost:8080/QuickReturns/Exchange ");
            BasicHttpBinding binding =  new BasicHttpBinding();
            IChannelFactory<ITradeService> channelFactory =
                 new ChannelFactory<ITradeService>(binding);
            ITradeService proxy = channelFactory.CreateChannel(address);
            Quote msftQuote =  new Quote();
            msftQuote.Ticker =  " MSFT ";
            msftQuote.Bid =  30.25M;
            msftQuote.Ask =  32.00M;
            msftQuote.Publisher =  " PracticalWCF ";
            Quote ibmQuote =  new Quote();
            ibmQuote.Ticker =  " IBM ";
            ibmQuote.Bid =  80.50M;
            ibmQuote.Ask =  81.00M;
            ibmQuote.Publisher =  " PracticalWCF ";
            proxy.PublishQuote(msftQuote);
            proxy.PublishQuote(ibmQuote);
            Quote result =  null;
            result = proxy.GetQuote( " MSFT ");
            Console.WriteLine( " Ticker: {0} Ask: {1} Bid: {2} ",
            result.Ticker, result.Ask, result.Bid);
            result = proxy.GetQuote( " IBM ");
            Console.WriteLine( " Ticker: {0} Ask: {1} Bid: {2} ",
            result.Ticker, result.Ask, result.Bid);
             try
            {
                result = proxy.GetQuote( " ATT ");
            }
             catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
             if (result ==  null)
            {
                Console.WriteLine( " Ticker ATT not found! ");
            }
            Console.WriteLine( " Done! Press return to exit ");
            Console.ReadLine();
        }
    }
}

 

App.config file for the Client Code

 

<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
     < system.serviceModel >
         < client >
             < endpoint  address ="http://localhost:8080/QuickReturns/Exchange"
                binding
="basicHttpBinding"
                contract
="QuickReturns.StockTrading.ExchangeServiceClient. ITradeService" >
             </ endpoint >
         </ client >
     </ system.serviceModel >
</ configuration >

 

 

 

转载于:https://www.cnblogs.com/davidgu/archive/2011/11/28/2265911.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值