進(jìn)行WCF Web Service創(chuàng)建與編寫(xiě)
今天上導(dǎo)師的課,在里課堂里提到WCF Web Service創(chuàng)建者在設(shè)計(jì)此項(xiàng)技術(shù)時(shí)的一個(gè)基本原則:“服務(wù)具有自主性:服務(wù)及其客戶端同意它們之間的接口,但相互獨(dú)立。它們可以采用不同的語(yǔ)言編寫(xiě)。#t#
可以使用不同的運(yùn)行時(shí)環(huán)境(如 CLR 和 Java 虛擬機(jī)),可以運(yùn)行在不同操作系統(tǒng)上,還可以存在其他方面的不同?!?nbsp; 我想這就說(shuō)明 Indigo 的 Service 可以發(fā)布成一個(gè)標(biāo)準(zhǔn)的WCF Web Service ,客戶端可以是任意語(yǔ)言編寫(xiě)的程序,只要它能通過(guò)WCF Web Service 的方式與其他程序交互。
我做了以下嘗試:
1. 用 WCF 寫(xiě)一個(gè) 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. 運(yùn)行這個(gè)程序啟動(dòng) WCF 服務(wù)
- 3. 用 WSDL 工具生成一個(gè)代理類(lèi)
- 在控制臺(tái)中輸入:wsdl http://localhost:8000/MathService?wsdl
- 4. 創(chuàng)建一個(gè)客戶端工程,把生成的代理加入這個(gè)工程中:
- 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 寫(xiě)的程序與WCF 技術(shù)實(shí)現(xiàn)的Service進(jìn)行交互,要想讓 Java 的程序和.Net的程序交互,我想最好的方法就是WCF Web Service 。我寫(xiě)的那些代碼就是想先試一下C# 的代碼能否用WCF Web Service 的方式與WCF技術(shù)實(shí)現(xiàn)的Service 進(jìn)行交互,但現(xiàn)在的問(wèn)題是我沒(méi)法精確控制這個(gè)WCF Web Service 的接口。