WCF client invoke services

1  使用客户端代理: 

 

 CalculatorClient proxy = new CalculatorClient();
            Console.WriteLine(proxy.Add(10, 20));
            Console.WriteLine(proxy.Multiple(10, 20));
            Console.Read();

 

2 使用ChannelFactory

View Code
ChannelFactory<ICalculator> factoryProxy = new ChannelFactory<ICalculator>();
            BasicHttpBinding bing = new BasicHttpBinding();
            EndpointAddress address = new EndpointAddress("http://localhost:38562/WCFCalculator.svc");
            ICalculator proxy1 = ChannelFactory<ICalculator>.CreateChannel(bing, address);
            using (proxy as IDisposable)
            {
                Console.WriteLine(proxy1.Add(10, 20));
            }
            Console.Read();

            ContractDescription cd = ContractDescription.GetContract(typeof(ICalculator));
            BasicHttpBinding binding = new BasicHttpBinding();
            EndpointAddress endpointAddress = new EndpointAddress("http://localhost:38562/WCFCalculator.svc");
            ServiceEndpoint se = new ServiceEndpoint(cd, binding, endpointAddress);
            ChannelFactory<ICalculator> channel = new ChannelFactory<ICalculator>(se);
            ICalculator ic = channel.CreateChannel();
            Console.WriteLine(ic.Add(10, 20));
            Console.WriteLine(ic.Multiple(10, 20));
            ICommunicationObject ico = ic as ICommunicationObject;
            if (channel != null)
                channel.Close();
            Console.Read();


 

posted @ 2012-08-16 11:01  xiaoshiyilang  阅读(583)  评论(0编辑  收藏  举报