在这个练习中,我们将定义并实现用来计算衍生产品的价格的WCF服务契约。
我们会在一个单独的.NET类库中定义和实现这个服务。虽然这使我们的实验变得简单,但一般来说,更好的方法是在一个程序集中定义服务契约并在另一个程序集中用一个实体类来实现该契约。这是契约优先开发的核心理念。
1.创建Derivatives Calculator Service项目
在这个任务中,我们将创建必需的Visual Studio项目,并引用WCF运行库。
(1)用Administrator帐户登录到虚拟机上,登录密码是P2ssw0rd。
(2)选择Start | All Programs | Microsoft Visual Studio 2008| Microsoft Visual Studio 2008。
(3)在Visual Studio中,选择File | New Project。
(4)在New Project对话框中的Project Types部分,选择Visual C# | Windows | Class Library,如下面所示。
(5)按图7所示在New Project对话框中输入:
NameDerivativesCalculatorService
LocationC:\Labs\WCF-Intro\CSharp\before
Solution NameDerivativesCalculator
Create directory for solutionChecked
图1 New Project对话框
(6)单击OK来创建新的解决方案。
添加对WCF的引用
(7)在Solution Explorer的References文件夹上按鼠标右键并选择Add Reference菜单项。
(8)在.NET选项卡中选择System.ServiceModel,如图2所示。
图2 添加对一个程序集的引用
(9)单击OK来添加对该程序集引用。
2.定义服务契约
(1)将Class1.cs更名为IDerivativesCalculator.cs
具体步骤:
·在Solution Explorer的DerivativesCalculatorService项目中,鼠标右键单击Class1.cs并选择Rename菜单项。
·输入IDerivativesCalculator.cs。
·当Visual Studio弹出确认窗口时选择Yes。
(2)打开IDerivativesCalculator.cs代码文件。它的内容应该像下面这样:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DerivativesCalculatorService
{
public class IDerivativesCalculator
{
}
}
(3)删除文件中的所有代码。
(4)在空白文件中插入下面的代码段。这等于是对代码做以下修改:
·添加了导入System.ServiceModel名字空间的using语句。
·将类声明为公有的。
·将IDerivativesCalculator从类改为接口。
·在接口上使用ServiceContract attribute。
这样做是为了告诉WCF运行库,该接口是一个WCF服务契约。这是定义WCF服务的关键步骤。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> SnippetWCF-Intro-1
using System;
using System.ServiceModel;
namespace DerivativesCalculatorService
{
[ServiceContract]
public interface IDerivativesCalculator
{
}
}
(5)在接口中加入下面的方法:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> SnippetWCF-Intro-2
[ServiceContract]
public interface IDerivativesCalculator
{
Decimal CalculateDerivative(int days, string[] symbols, string[] functions);
}
虽然我们已经定义了接口中必须实现的一个方法,但除非在方法上使用相应的attribute,否则WCF运行库是认不出这个方法的。
(6) 在接口的方法定义上使用OperationContract attribute。
这样做是为了告诉WCF运行库,这个方法定义了一个操作,在这个服务契约的任何实现中都可以使用。
(7)现在的代码看起来如下所示。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace DerivativesCalculatorService
{
[ServiceContract]
public interface IDerivativesCalculator
{
[OperationContract]
Decimal CalculateDerivative(int days, string[] symbols,
string[] functions);
}
}
现在我们已经定义了一个简单的WCF契约。
就这么简单!
3.实现服务契约
我们已经有了服务契约,现在是创建真正的服务的时候了。这很简单,只要实现服务契约的接口就可以了。
添加实体类
(1)在项目中添加一个名为Calculator.cs的新文件。
具体步骤:
·选择Project | Add Class菜单项。
·在Add New Item对话框中,将Name设为Calculator.cs。
·单击Add按钮。
(2)打开新添加的类文件。
(3)加入一行using语句来导入System.ServiceModel名字空间。
(4)给类加上public 修饰符。
(5)类文件现在看起来应该像下面这样:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace DerivativesCalculatorService
{
public class Calculator
{
}
}
实现服务契约的接口
(6)在类定义的后面加上: IDerivativesCalculator。
(7)按快捷键Shift + ALT + F10,这会弹出一个菜单来帮助我们实现接口,如图所示。
图3 实现一个接口
(8)选择Implement interface ‘IDerivitivesCalculator’菜单项,Visual Studio会为我们添加代码来实现接口。
(9)类的代码现在看起来应该像下面这样:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public class Calculator : IDerivativesCalculator
{
#region IDerivativesCalculator Members
public decimal CalculateDerivative(int days, string[] symbols,
string[] functions)
{
throw new NotImplementedException();
}
#endregion
}
因为这个实验的重点并不是实现具体的业务逻辑,所以在这个简单的实验中,我们只是从函数中返回一个值。
(10)用下面的代码替换掉NotImplementedException。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> SnippetWCF-Intro-3
public decimal CalculateDerivative(int days, string[] symbols, string[] functions)
{
return (decimal) (System.DateTime.Now.Millisecond);
}
(11)保存类文件(选择File | Save All菜单项或按快捷键CTRL + S)
这就是前面定义的服务契约的完整实现。
构建项目
(12)选择Build | Build Solution菜单项。
(13)如果项目在构建时没有产生错误,那么我们就完成了Derivatives Calculator服务的实现。