【转载】MSDN上发现了一篇很好的WCF入门教程

转载地址:

http://www.cnblogs.com/xblues/archive/2008/07/11/1240843.html

看了园子里很多学习WCF的例子,感觉受益匪浅,但是由于每个人学习的侧重点不同,一些很详细的细节例如每一个属性都是用来干什么的,建立不同的项目类型对创建的服务有什么区别等等,都不得而知。终于,在MSDN上发现了一篇入门教程。讲解的十分基本,十分详细,想进到每一个细节,然我彻底了解入门的每一个细节,整个教程结构清晰,代码简洁,讲解细致,值得推荐。

地址: http://msdn.microsoft.com/en-us/library/ms734712.aspx

做这分5部来讲解创建一个最基本的基于B/S构架的WCF应用。服务是根据输入的两个数字,返回这两个数字的加减乘除运算结果。

第一步:定义WCF服务契约(创建项目,加入引用,定义Interface)
第二部:引入WCF服务契约(添加具体服务函数)
第三部:构架WCF服务,运行WCF服务(添加Uri,定义服务对象地址,运行服务)
第四部:利用工具访问服务,自动生成WCF服务代理的代码文件
第五部:配置一个简单的WCF客户端(用客户端引入服务代理,通过服务代理来访问服务)
第六部:运行程序

How to: Define a Windows Communication Foundation Service Contract
How to: Implement a Windows Communication Foundation Service Contract
How to: Host and Run a Basic Windows Communication Foundation Service
How to: Create a Windows Communication Foundation Client
How to: Configure a Basic Windows Communication Foundation Client
How to: Use a Windows Communication Foundation Client

先建立一个解决方案。
在这个解决方案下面建立一个叫做Server的控制台应用项目,再建立一个叫做Client的控制台应用项目。
分别给每一个项目添加引用到System.ServiceModel
编辑每个项目下面的Program.cs

ContractedBlock.gif ExpandedBlockStart.gif 客户端Program.cs代码
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gifusing System.ServiceModel;
 5None.gif
 6None.gifnamespace ServiceModelSamples
 7ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 8InBlock.gif
 9InBlock.gif    class Client
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
11InBlock.gif        static void Main()
12ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
13InBlock.gif            //Step 1: Create an endpoint address and an instance of the WCF Client.
14InBlock.gif            EndpointAddress epAddress = new EndpointAddress("http://localhost:8000/ServiceModelSamples/Service/CalculatorService");
15InBlock.gif            CalculatorClient client = new CalculatorClient(new WSHttpBinding(), epAddress);
16InBlock.gif
17InBlock.gif
18InBlock.gif            // Step 2: Call the service operations.
19InBlock.gif            // Call the Add service operation.
20InBlock.gif            double value1 = 100.00D;
21InBlock.gif            double value2 = 15.99D;
22InBlock.gif            double result = client.Add(value1, value2);
23InBlock.gif            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
24InBlock.gif
25InBlock.gif            // Call the Subtract service operation.
26InBlock.gif            value1 = 145.00D;
27InBlock.gif            value2 = 76.54D;
28InBlock.gif            result = client.Subtract(value1, value2);
29InBlock.gif            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
30InBlock.gif
31InBlock.gif            // Call the Multiply service operation.
32InBlock.gif            value1 = 9.00D;
33InBlock.gif            value2 = 81.25D;
34InBlock.gif            result = client.Multiply(value1, value2);
35InBlock.gif            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
36InBlock.gif
37InBlock.gif            // Call the Divide service operation.
38InBlock.gif            value1 = 22.00D;
39InBlock.gif            value2 = 7.00D;
40InBlock.gif            result = client.Divide(value1, value2);
41InBlock.gif            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
42InBlock.gif
43InBlock.gif            //Step 3: Closing the client gracefully closes the connection and cleans up resources.
44InBlock.gif            client.Close();
45InBlock.gif
46InBlock.gif            Console.WriteLine();
47InBlock.gif            Console.WriteLine("Press <ENTER> to terminate client.");
48InBlock.gif            Console.ReadLine();
49InBlock.gif
50InBlock.gif
51ExpandedSubBlockEnd.gif        }

52ExpandedSubBlockEnd.gif    }

53ExpandedBlockEnd.gif}

54None.gif


服务端创建好了以后,就可以试运行了。
这时候可以用微软提供的命令行工具访问这个服务,生成服务代理 app.config 和 generatedProxy.cs两个文件。

svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config
http://localhost:8000/ServiceModelSamples/service

把这两个文件添加到客户端项目里去。
现在就可以编辑客户端代码了。

ContractedBlock.gif ExpandedBlockStart.gif 服务端Program.cs代码
  1None.gifusing System;
  2None.gifusing System.ServiceModel;
  3None.gifusing System.ServiceModel.Description;
  4None.gif
  5None.gifnamespace Microsoft.ServiceModel.Samples
  6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  7InBlock.gif    // Define a service contract.
  8InBlock.gif    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
  9InBlock.gif    public interface ICalculator
 10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 11InBlock.gif        [OperationContract]
 12InBlock.gif        double Add(double n1, double n2);
 13InBlock.gif        [OperationContract]
 14InBlock.gif        double Subtract(double n1, double n2);
 15InBlock.gif        [OperationContract]
 16InBlock.gif        double Multiply(double n1, double n2);
 17InBlock.gif        [OperationContract]
 18InBlock.gif        double Divide(double n1, double n2);
 19ExpandedSubBlockEnd.gif    }

 20InBlock.gif
 21InBlock.gif    // Service class that implements the service contract.
 22InBlock.gif    // Added code to write output to the console window.
 23InBlock.gif    public class CalculatorService : ICalculator
 24ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 25InBlock.gif        public double Add(double n1, double n2)
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 27InBlock.gif            double result = n1 + n2;
 28InBlock.gif            Console.WriteLine("Received Add({0},{1})", n1, n2);
 29InBlock.gif            Console.WriteLine("Return: {0}", result);
 30InBlock.gif            return result;
 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif
 33InBlock.gif        public double Subtract(double n1, double n2)
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 35InBlock.gif            double result = n1 - n2;
 36InBlock.gif            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
 37InBlock.gif            Console.WriteLine("Return: {0}", result);
 38InBlock.gif            return result;
 39ExpandedSubBlockEnd.gif        }

 40InBlock.gif
 41InBlock.gif        public double Multiply(double n1, double n2)
 42ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 43InBlock.gif            double result = n1 * n2;
 44InBlock.gif            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
 45InBlock.gif            Console.WriteLine("Return: {0}", result);
 46InBlock.gif            return result;
 47ExpandedSubBlockEnd.gif        }

 48InBlock.gif
 49InBlock.gif        public double Divide(double n1, double n2)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 51InBlock.gif            double result = n1 / n2;
 52InBlock.gif            Console.WriteLine("Received Divide({0},{1})", n1, n2);
 53InBlock.gif            Console.WriteLine("Return: {0}", result);
 54InBlock.gif            return result;
 55ExpandedSubBlockEnd.gif        }

 56ExpandedSubBlockEnd.gif    }

 57InBlock.gif
 58InBlock.gif
 59InBlock.gif    class Program
 60ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 61InBlock.gif        static void Main(string[] args)
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63InBlock.gif
 64InBlock.gif            // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
 65InBlock.gif            Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
 66InBlock.gif
 67InBlock.gif            // Step 1 of the hosting procedure: Create ServiceHost
 68InBlock.gif            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
 69InBlock.gif            try
 70ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 71InBlock.gif
 72InBlock.gif
 73InBlock.gif                // Step 3 of the hosting procedure: Add a service endpoint.
 74InBlock.gif                selfHost.AddServiceEndpoint(
 75InBlock.gif                    typeof(ICalculator),
 76InBlock.gif                    new WSHttpBinding(),
 77InBlock.gif                    "CalculatorService");
 78InBlock.gif
 79InBlock.gif
 80InBlock.gif                // Step 4 of the hosting procedure: Enable metadata exchange.
 81InBlock.gif                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
 82InBlock.gif                smb.HttpGetEnabled = true;
 83InBlock.gif                selfHost.Description.Behaviors.Add(smb);
 84InBlock.gif
 85InBlock.gif                // Step 5 of the hosting procedure: Start (and then stop) the service.
 86InBlock.gif                selfHost.Open();
 87InBlock.gif                Console.WriteLine("The service is ready.");
 88InBlock.gif                Console.WriteLine("Press <ENTER> to terminate service.");
 89InBlock.gif                Console.WriteLine();
 90InBlock.gif                Console.ReadLine();
 91InBlock.gif
 92InBlock.gif                // Close the ServiceHostBase to shutdown the service.
 93InBlock.gif                selfHost.Close();
 94ExpandedSubBlockEnd.gif            }

 95InBlock.gif            catch (CommunicationException ce)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 97InBlock.gif                Console.WriteLine("An exception occurred: {0}", ce.Message);
 98InBlock.gif                selfHost.Abort();
 99ExpandedSubBlockEnd.gif            }

100ExpandedSubBlockEnd.gif        }

101ExpandedSubBlockEnd.gif    }

102ExpandedBlockEnd.gif}

103None.gif


每一个细节都包含在上面的这两个Program.cs文件中了,你大概看一下就会懂。比院子里大多数教材说得都清晰,特别适合像我一样爱刨根问底的初学者。:)
最后编译程序,试运行。(两个都是命令行程序,直接到那个编译好的目录里去找那个exe文件运行,先运行服务,再运行客户端)。

转载于:https://www.cnblogs.com/OceanChen/archive/2009/03/03/1402273.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值