如何:实现 Windows Communication Foundation 服务协定
这是创建基本 Windows Communication Foundation (WCF) 服务和可以调用该服务的客户端所需的六项任务中的第二项任务。有关全部六项任务的概述,请参见入门教程主题。
创建 WCF 服务要求首先创建使用接口定义的协定。有关创建该接口的更多信息,请参见如何:定义 Windows Communication Foundation 服务协定。本示例中显示的下一步骤是实现接口。这涉及到创建一个名为 CalculatorService 的类,该类实现用户定义的 ICalculator 接口。在操作过程后面的示例中提供了用于此任务的代码。
实现 WCF 服务协定
在定义了
ICalculator
接口的同一文件中创建一个名为CalculatorService
的新类。CalculatorService
实现ICalculator
接口。Public Class CalculatorService Implements ICalculator
public class CalculatorService : ICalculator
在
CalculatorService
类中实现在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
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
Imports System.ServiceModel
Namespace Microsoft.ServiceModel.Samples
' Define a service contract.
<ServiceContract([Namespace] := "http://Microsoft.ServiceModel.Samples")> _
Public Interface ICalculator
<OperationContract()> _
Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
<OperationContract()> _
Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
<OperationContract()> _
Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
<OperationContract()> _
Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
End Interface
' Step 1: Create service class that implements the service contract.
Public Class CalculatorService
Implements ICalculator
' Step 2: Implement functionality for the service operations.
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
using System;
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples
{
// Define a service contract.
[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);
}
// Step 1: Create service class that implements the service contract.
public class CalculatorService : ICalculator
{
// Step 2: Implement functionality for the service operations.
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;
}
}
}
现在已创建并实现了服务协定。生成该解决方案以确保不存在编译错误,然后按照如何:承载和运行基本的 Windows Communication Foundation 服务中的说明运行服务。有关疑难解答信息,请参见入门教程疑难解答。
编译代码
如果使用的是命令行编译器,则必须引用 System.ServiceModel 程序集。
如果使用的是 Visual Studio,请在“生成”菜单上单击“生成解决方案”(或按 Ctrl+Shift+B)。