教程:实现 Windows Communication Foundation 服务协定
本教程介绍创建基本 Windows Communication Foundation (WCF) 应用程序所要完成的五个任务中的第二个任务。 有关教程概述,请参阅教程:Windows Communication Foundation 应用程序入门。
创建 WCF 应用程序的下一步是添加代码,以实现在上一步中创建的 WCF 服务接口。 在此步骤中,将创建名为 CalculatorService
的类,用于实现用户定义的 ICalculator
接口。 以下代码中的每个方法都将调用计算器操作,并将文本写入到控制台进行测试。
本教程介绍如何执行下列操作:
- 添加代码以实现 WCF 服务协定。
- 生成解决方案。
添加代码以实现 WCF 服务协定
打开 GettingStartedLib 中的 Service1.cs 或 Service1.vb 文件,并将其代码替换为以下代码:
using System;
using System.ServiceModel;
namespace GettingStartedLib
{
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;
}
}
}
Imports System.ServiceModel
Namespace GettingStartedLib
Public Class CalculatorService
Implements ICalculator
Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Add
Dim result As Double = n1 + n2
' Code added to write output to the console window.
Console.WriteLine("Received Add({0},{1})", n1, n2)
Console.WriteLine("Return: {0}", result)
Return result
End Function
Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
Dim result As Double = n1 - n2
Console.WriteLine("Received Subtract({0},{1})", n1, n2)
Console.WriteLine("Return: {0}", result)
Return result
End Function
Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
Dim result As Double = n1 * n2
Console.WriteLine("Received Multiply({0},{1})", n1, n2)
Console.WriteLine("Return: {0}", result)
Return result
End Function
Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
Dim result As Double = n1 / n2
Console.WriteLine("Received Divide({0},{1})", n1, n2)
Console.WriteLine("Return: {0}", result)
Return result
End Function
End Class
End Namespace
编辑 App.config
编辑 GettingStartedLib 中的 App.config,以体现你对代码进行的更改。
对于 Visual C# 项目:
- 将第 14 行更改为
<service name="GettingStartedLib.CalculatorService">
- 将第 17 行更改为
<add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService" />
- 将第 22 行更改为
<endpoint address="" binding="wsHttpBinding" contract="GettingStartedLib.ICalculator">
- 将第 14 行更改为
对于 Visual Basic 项目:
- 将第 14 行更改为
<service name="GettingStartedLib.GettingStartedLib.CalculatorService">
- 将第 17 行更改为
<add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService" />
- 将第 22 行更改为
<endpoint address="" binding="wsHttpBinding" contract="GettingStartedLib.GettingStartedLib.ICalculator">
- 将第 14 行更改为
编译代码
生成解决方案,以验证是否没有任何编译错误。 如果使用的是 Visual Studio,请在“生成”菜单上选择“生成解决方案”(或按 Ctrl+Shift+B)。
后续步骤
在本教程中,你了解了如何执行以下操作:
- 添加代码以实现 WCF 服务协定。
- 生成解决方案。
请继续阅读下一篇教程,了解如何运行 WCF 服务。