進行WCF Web Service創(chuàng)建與編寫
今天上導師的課,在里課堂里提到WCF Web Service創(chuàng)建者在設計此項技術(shù)時的一個基本原則:“服務具有自主性:服務及其客戶端同意它們之間的接口,但相互獨立。它們可以采用不同的語言編寫。#t#
可以使用不同的運行時環(huán)境(如 CLR 和 Java 虛擬機),可以運行在不同操作系統(tǒng)上,還可以存在其他方面的不同。” 我想這就說明 Indigo 的 Service 可以發(fā)布成一個標準的WCF Web Service ,客戶端可以是任意語言編寫的程序,只要它能通過WCF Web Service 的方式與其他程序交互。
我做了以下嘗試:
1. 用 WCF 寫一個 Service。
- <configuration
- xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
- <system.serviceModel>
- <services>
- <service name="WcfServer.MathService"
- behaviorConfiguration="MathServiceBehavior">
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:8000/MathService"/>
- baseAddresses>
- host>
- <endpoint
- address=""
- binding="basicHttpBinding"
- contract="WcfServer.IMath"/>
- service>
- services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="MathServiceBehavior">
- <serviceMetadata httpGetEnabled="True"/>
- <serviceDebug includeExceptionDetailInFaults="False" />
- behavior>
- serviceBehaviors>
- behaviors>
- system.serviceModel>
- configuration>
- 2. 運行這個程序啟動 WCF 服務
- 3. 用 WSDL 工具生成一個代理類
- 在控制臺中輸入:wsdl http://localhost:8000/MathService?wsdl
- 4. 創(chuàng)建一個客戶端工程,把生成的代理加入這個工程中:
- namespace UsingWebServiceMode
- {
- class Program
- {
- static void Main(string[] args)
- {
- MathService proxy = new MathService();
- int result;
- bool resultSpecified;
- proxy.Add(4, true, 5, true, out result, out resultSpecified);
- Console.WriteLine(result);
- Console.WriteLine(resultSpecified);
- Console.ReadLine();
- }
- }
- }
我的最終目的是要讓 Java 寫的程序與WCF 技術(shù)實現(xiàn)的Service進行交互,要想讓 Java 的程序和.Net的程序交互,我想最好的方法就是WCF Web Service 。我寫的那些代碼就是想先試一下C# 的代碼能否用WCF Web Service 的方式與WCF技術(shù)實現(xiàn)的Service 進行交互,但現(xiàn)在的問題是我沒法精確控制這個WCF Web Service 的接口。