MSDN上面的WCF入门教程练习

这篇博客详细介绍了如何通过MSDN上的教程学习Windows Communication Foundation(WCF),包括服务的创建、异常处理、N2类库的使用,以及字符串操作等关键知识点。
摘要由CSDN通过智能技术生成

http://msdn.microsoft.com/zh-cn/library/ms734712.aspx


照着说明测试了一下:
一、创建解决方案WCFtest
二、创建服务端
1、在WCFtest下添加一个“控制台应用程序”,命名为Service
2、修改默认的命名空间为Microsoft.ServiceModel.Samples
3、添加引用System.ServiceModel.dll
4、编写Program.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Microsoft.ServiceModel.Samples
{
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator
    {
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }
    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            double result = n1 + n2;
            Console.WriteLine("Received Add({0},{1})", n1, n2);
            // Code added to write output to the console window. 
            Console.WriteLine("Return: {0}", result); return result;
        }
        public double Subtract(double n1, double n2)
        {
            double result = n1 - n2;
            Console.WriteLine("Received Subtract({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result); return result;
        }
        public double Multiply(double n1, double n2)
        {
            double result = n1 * n2;
            Console.WriteLine("Received Multiply({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }
        public double Divide(double n1, double n2)
        {
            double result = n1 / n2;
            Console.WriteLine("Received Divide({0},{1})", n1, n2);
            Console.WriteLine("Return: {0}", result);
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            // Step 1 of the address configuration procedure: Create a URI to serve as the base address. 
            Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
            // Step 2 of the hosting procedure: Create ServiceHost 
            ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
            try
            {
                // Step 3 of the hosting procedure: Add a service endpoint. 
                selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");
                // Step 4 of the hosting procedure: Enable metadata exchange. 
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);
                // Step 5 of the hosting procedure: Start (and then stop) the service. 
                selfHost.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();
                // Close the ServiceHostBase to shutdown the service. 
                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }
}
验证服务是否正常运行:
1、按F5启动或者在项目目录下的bin/Debug目录下打开service.exe
2、打开浏览器并输入 http://localhost:8000/ServiceModelSamples/Service
三、创建客户端
1、在WCFtest下添加一个“控制台应用程序”,命名为Client
2、添加引用System.ServiceModel.dll
3、打开“Visual Studio 2010 命令提示”,运行ServiceModel 元数据实用工具 (Svcutil.exe) ,在项目Client目录下创建客户端代码和配置文件:
C:/Documents and Settings/lc/My Documents/Visual Studio 2010/Projects/WCFte
st/Client>svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config htt
p://localhost:8000/ServiceModelSamples/service
4、在解决方案资源管理器中点击“显示所有文件”,把隐藏的app.config、generatedProxy.cs右键包括在项目Client中
5、编写Program.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //Step 1: Create an endpoint address and an instance of the WCF Client. 
            CalculatorClient client = new CalculatorClient();
            // Step 2: Call the service operations. 
            // Call the Add service operation. 
            double value1 = 100.00D;
            double value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
            //Step 3: Closing the client gracefully closes the connection and cleans up resources. 
            client.Close();
            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
}
最后,先打开Server/bin/Debug下的Service.exe运行服务端,再打开Client/bin/Debug下的Client.exe运行客户端。
显示如下:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值