教學課程:實作 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 服務。