TCPActivation 範例示範裝載使用 Windows Process Activation Services (WAS) 的服務來啟用透過 net.tcp 通訊協定進行通訊的服務。 此範例是以 用戶入門為基礎。
備註
此範例的安裝程式和建置指示位於本主題結尾。
此範例包含客戶端控制台程式(.exe)和服務庫(.dll),裝載於 WAS 所啟動的背景工作程序。 主控台視窗中會顯示客戶端活動。
服務會實作定義要求-回復通訊模式的合約。 合約是由 ICalculator 介面所定義,其會公開數學運算(Add、Subtract、Multiply 和 Divide),如下列範例程式代碼所示:
[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);
}
服務實作會計算並傳回適當的結果:
// Service class that implements the service contract.
public class CalculatorService : ICalculator
{
public double Add(double n1, double n2)
{
return n1 + n2;
}
public double Subtract(double n1, double n2)
{
return n1 - n2;
}
public double Multiply(double n1, double n2)
{
return n1 * n2;
}
public double Divide(double n1, double n2)
{
return n1 / n2;
}
}
此範例會使用 net.tcp 系結的變體,並啟用 TCP 連接埠共用並關閉安全性。 如果您想要使用安全的 TCP 系結,請將伺服器的安全性模式變更為所需的設定,並在用戶端上重新執行 Svcutil.exe,以產生更新用戶端組態檔。
下列範例顯示服務的組態:
<system.serviceModel>
<services>
<service name="Microsoft.ServiceModel.Samples.CalculatorService"
behaviorConfiguration="CalculatorServiceBehavior">
<!-- This endpoint is exposed at the base address provided by host: net.tcp://localhost/servicemodelsamples/service.svc -->
<endpoint binding="netTcpBinding" bindingConfiguration="PortSharingBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
<!-- the mex endpoint is exposed at net.tcp://localhost/servicemodelsamples/service.svc/mex -->
<endpoint address="mex"
binding="mexTcpBinding"
contract="IMetadataExchange" />
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="PortSharingBinding" portSharingEnabled="true">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
用戶端的端點已設定為如下列範例程式代碼所示:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_ICalculator">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost/servicemodelsamples/service.svc"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ICalculator"
contract="Microsoft.ServiceModel.Samples.ICalculator" name="NetTcpBinding_ICalculator" />
</client>
</system.serviceModel>
當您執行範例時,作業要求和回應會顯示在用戶端控制台視窗中。 在客戶端視窗中按 ENTER 鍵以關閉用戶端。
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Press <ENTER> to terminate client.
要設定、建置和執行範例,請執行以下步驟:
確定已安裝 IIS 7.0。 需要 IIS 7.0 才能啟用 WAS。
請確定您已針對 Windows Communication Foundation 範例執行One-Time 安裝程式。
此外,您必須安裝 WCF 非 HTTP 啟用元件:
從 [ 開始] 功能表中,選擇 [控制面板]。
選取 [程式和功能]。
按兩下 [開啟或關閉 Windows 元件]。
展開 Microsoft .NET Framework 3.0 節點,並檢查 Windows Communication Foundation 非 HTTP 啟用 功能。
設定 WAS 以支援 TCP 啟用。
為了方便起見,下列兩個步驟會在位於範例目錄中的批處理檔(名稱為 "AddNetTcpSiteBinding.cmd")中實施。
若要支持 net.tcp 啟用,默認網站必須先系結至 net.tcp 連接埠。 這可以使用 Appcmd.exe來完成,這是隨 Internet Information Services 7.0 (IIS) 管理工具組一起安裝的。 從系統管理員層級命令提示字元中,執行下列命令:
%windir%\system32\inetsrv\appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.tcp',bindingInformation='808:*']小提示
此命令是單行文字。 此命令會將 net.tcp 網站系結新增至默認網站,以任何主機名接聽 TCP 連接埠 808。
雖然網站內的所有應用程式都會共用一般 net.tcp 系結,但每個應用程式都可以個別啟用 net.tcp 支援。 若要啟用 /servicemodelsamples 應用程式的 net.tcp,請從系統管理員層級命令提示字元執行下列命令:
%windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/servicemodelsamples" /enabledProtocols:http,net.tcp備註
此命令是單行文字。 此命令可讓 /servicemodelsamples 應用程式同時使用
http://localhost/servicemodelsamples和net.tcp://localhost/servicemodelsamples來存取。
若要建置解決方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例 中中的指示。
若要在單一或跨計算機組態中執行範例,請遵循 執行 Windows Communication Foundation 範例中的指示。
拿掉您為此範例新增的 net.tcp 網站系結。
為了方便起見,下列兩個步驟會在範例目錄中稱為 RemoveNetTcpSiteBinding.cmd的批處理檔中實作。
從系統管理員層級命令提示字元執行下列命令,從啟用的通訊協定清單中移除 net.tcp:
%windir%\system32\inetsrv\appcmd.exe set app "Default Web Site/servicemodelsamples" /enabledProtocols:http備註
此命令必須輸入為單行文字。
從系統管理員層級命令提示字元執行下列命令,以移除 net.tcp 網站系結:
%windir%\system32\inetsrv\appcmd.exe set site "Default Web Site" --bindings.[protocol='net.tcp',bindingInformation='808:*']備註
此命令必須輸入為單行文字。