本人是做.net方向的,对于移动互联网比较感兴趣,业余时间研究研究Android,希望大家多指教。
最近帮朋友做一个项目,需要一个服务端,其他语言都不太会,就准备用WCF来做服务端程序。
本文是基于ksoap2来调用WCF服务的。
下面是WCF程序:(如果你也是.net方向,请略过此篇)
源码地址:http://download.csdn.net/detail/cnryc/7694983
1、项目解决方案:
这是一个非常简单的WCF程序,分了3个项目:
WCFLibrary.dll是WCF服务的具体内容。
Test.exe是WCF服务的宿主程序。
ClientTest.exe是一个.net Console程序,用于测试WCF是否能正常使用。
2、WCFLibrary.dll项目:
首先新建一个类库项目,然后引用System.ServiceModel。
然后添加一个ITestService接口文件,里面就一个返回string的方法。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.ServiceModel; 7 8 namespace WCFLibrary 9 { 10 [ServiceContract] 11 public interface ITestService 12 { 13 [OperationContract] 14 string GetString(string value); 15 } 16 }
之后添加一个TestService类继承上面的接口,实现一个非常简单的功能。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace WCFLibrary 8 { 9 public class TestService : ITestService 10 { 11 public string GetString(string value) 12 { 13 return "Hello " + value; 14 } 15 } 16 }
3、Test.exe项目
这个是WCF服务的宿主程序。
新建一个控制台程序,然后引用System.ServiceModel,再上面的WCFLibrary.dll。
然后在Program类里添加代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.ServiceModel; 5 using System.Text; 6 using System.Threading.Tasks; 7 using WCFLibrary; 8 9 namespace Test 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 using (ServiceHost host = new ServiceHost(typeof(TestService))) 16 { 17 host.Opened += delegate 18 { 19 Console.WriteLine("Service已经启动,按任意键终止服务!"); 20 }; 21 22 host.Open(); 23 Console.Read(); 24 } 25 } 26 } 27 }
之后在App.config里添加WCF的配置信息:
1 <system.serviceModel> 2 <services> 3 <service name="WCFLibrary.TestService"> 4 <host> 5 <baseAddresses> 6 <add baseAddress = "http://localhost:8733/WCFLibrary/TestService/" /> 7 </baseAddresses> 8 </host> 9 <endpoint address="" binding="basicHttpBinding" contract="WCFLibrary.ITestService"/> 10 </service> 11 </services> 12 <behaviors> 13 <serviceBehaviors> 14 <behavior> 15 <!-- 为避免泄漏元数据信息, 16 请在部署前将以下值设置为 false --> 17 <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/> 18 <!-- 要接收故障异常详细信息以进行调试, 19 请将以下值设置为 true。在部署前设置为 false 20 以避免泄漏异常信息--> 21 <serviceDebug includeExceptionDetailInFaults="False" /> 22 </behavior> 23 </serviceBehaviors> 24 </behaviors> 25 </system.serviceModel>
到此,简单WCF服务就完成了。
用管理员身份启动VS或者启动Test.exe,启动程序。
在浏览器中打开上面配置文件中的地址,出现如下结果,服务已成功启动:
4、ClientTest.exe项目
建立一个客户端程序来测试服务是否正常。
新建一个控制台程序,然后引用System.ServiceModel,再添加一个服务引用,地址就是上面App.config文件里的那个地址。
添加服务引用之后,自动回在App.config中生成配置文件内容。
然后再Program类里写上服务端调用的测试代码:
using ClientTest.ServiceReference; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClientTest { class Program { static void Main(string[] args) { TestServiceClient client = new TestServiceClient(); string value = "imoonstal"; string result = client.GetString(value); Console.WriteLine(result); client.Close(); Console.ReadLine(); } } }
运行程序,调用服务成功,结果如下:
到此,WCF的服务端就做完了。
源代码已上传至csdn下载,希望能对初学者有所帮助。