首页 > 代码库 > Xamarin.Android 入门实例(2)之实现WCF 寄宿于IIS 的Web服务提供

Xamarin.Android 入门实例(2)之实现WCF 寄宿于IIS 的Web服务提供

1.WCF 契约 ICalculator.cs

技术分享
 1 using System.ServiceModel; 2 namespace Contracts 3 { 4     [ServiceContract] 5     public interface ICalculator 6     { 7         /// <summary> 8         /// 加法 9         /// </summary>10         /// <param name="x"></param>11         /// <param name="y"></param>12         /// <returns></returns>13         [OperationContract]14         [DataContractFormat(Style = OperationFormatStyle.Rpc)]15         double AddDouble(double x, double y);16 17         /// <summary>18         /// 加法19         /// </summary>20         /// <param name="num1">第一个数</param>21         /// <param name="num2">第二个数</param>22         /// <param name="num">结果</param>23         /// <returns></returns>24         [OperationContract]25         [DataContractFormat(Style = OperationFormatStyle.Rpc)]26         string Add(double num1, double num2, ref double num);27 28 29         /// <summary>30         /// 减法31         /// </summary>32         /// <param name="num1">第一个数</param>33         /// <param name="num2">第二个数</param>34         /// <param name="num">结果</param>35         /// <returns></returns>36         [OperationContract]37         [DataContractFormat(Style = OperationFormatStyle.Rpc)]38         string Reduce(double num1, double num2, ref double num);39 40 41 42         /// <summary>43         /// 乘法44         /// </summary>45         /// <param name="num1">第一个数</param>46         /// <param name="num2">第二个数</param>47         /// <param name="num">结果</param>48         /// <returns></returns>49         [OperationContract]50         [DataContractFormat(Style = OperationFormatStyle.Rpc)]51         string Multiplication(double num1, double num2, ref double num);52 53 54         /// <summary>55         /// 除法56         /// </summary>57         /// <param name="num1">第一个数</param>58         /// <param name="num2">第二个数</param>59         /// <param name="num">结果</param>60         /// <returns></returns>61         [OperationContract]62         [DataContractFormat(Style = OperationFormatStyle.Rpc)]63         string Division(double num1, double num2, ref double num);64 65     }66 67 68 }
View Code

注意:

在客户端添加Web引用时为避免额外的布尔值参数使用,确保代理客户端强制执行时使用的RPC风格的编码,这是SOAP 1.1规范的一部分。

请参考随笔:

Web Reference for a WCF Service has Extra “IdSpecified” Parameter ?

http://www.cnblogs.com/mschen/p/4262090.html

 

2. 服务实现 CalculatorService.cs

技术分享
  1 using System;  2 using System.Collections.Generic;  3 using System.Text;  4 using Contracts;  5   6 namespace Services  7 {  8     public class CalculatorService : ICalculator  9     { 10         /// <summary> 11         /// 加法 12         /// </summary> 13         /// <param name="x"></param> 14         /// <param name="y"></param> 15         /// <returns></returns> 16         public double AddDouble(double x, double y) 17         { 18             return x + y; 19         } 20  21         /// <summary> 22         /// 加法 23         /// </summary> 24         /// <param name="num1">第一个数</param> 25         /// <param name="num2">第二个数</param> 26         /// <param name="num">结果</param> 27         /// <returns></returns> 28         ///  29         public string Add(double num1, double num2, ref double num) 30         { 31             try 32             { 33                 num = num1 + num2; 34                 return "1"; 35             } 36             catch (Exception ex) 37             { 38                 return ex.ToString(); 39             } 40         } 41  42         /// <summary> 43         /// 减法 44         /// </summary> 45         /// <param name="num1">第一个数</param> 46         /// <param name="num2">第二个数</param> 47         /// <param name="num">结果</param> 48         /// <returns></returns> 49         public string Reduce(double num1, double num2, ref double num) 50         { 51             try 52             { 53                 num = num1 - num2; 54                 return "1"; 55             } 56             catch (Exception ex) 57             { 58                 return ex.ToString(); 59             } 60         } 61  62  63         /// <summary> 64         /// 乘法 65         /// </summary> 66         /// <param name="num1">第一个数</param> 67         /// <param name="num2">第二个数</param> 68         /// <param name="num">结果</param> 69         /// <returns></returns> 70         public string Multiplication(double num1, double num2, ref double num) 71         { 72             try 73             { 74                 num = num1 * num2; 75                 return "1"; 76             } 77             catch (Exception ex) 78             { 79                 return ex.ToString(); 80             } 81         } 82  83         /// <summary> 84         /// 除法 85         /// </summary> 86         /// <param name="num1">第一个数</param> 87         /// <param name="num2">第二个数</param> 88         /// <param name="num">结果</param> 89         /// <returns></returns> 90         public string Division(double num1, double num2, ref double num) 91         { 92             try 93             { 94                 num = num1 / num2; 95                 return "1"; 96             } 97             catch (Exception ex) 98             { 99                 return ex.ToString();100             }101         }102 103     }104 }
View Code

3.IIS 创建文件CalculatorService.svc

 1 <%@ServiceHost Service="Services.CalculatorService"%> 

4.IIS 寄宿服务配置,基于basicHttpBinding绑定 Web.config

 1 <?xml version="1.0"?> 2 <configuration> 3   <system.serviceModel> 4     <behaviors> 5       <serviceBehaviors> 6         <behavior name="CalculatorBehavior"> 7           <serviceMetadata httpGetEnabled="true"/> 8         </behavior> 9       </serviceBehaviors>10     </behaviors>11     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />12     <services>13       <service behaviorConfiguration="CalculatorBehavior" name="Services.CalculatorService">14         <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""15           contract="Contracts.ICalculator" />16         <host>17           <baseAddresses>18             <add baseAddress="http://192.168.1.2:8080/CalculatorService.svc" />19           </baseAddresses>20         </host>21       </service>22     </services>23   </system.serviceModel>24   <system.web>25     <compilation debug="true"/>26   </system.web>27 </configuration>

注意:为了保证 soap:address location 和wsdl:import location 显示和url里面的ip或者域名是一致请使用:<serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> ,multipleSiteBindingsEnabled 是.net framework 4里面支持的特性,如果版本低于4,例如3.5 那么就要根据IIS binding来设置baseAddress.

5.Main.axml 视图

技术分享

技术分享
 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent"> 5     <EditText 6         android:id="@+id/txtPhoneNo" 7         android:layout_width="304.0dp" 8         android:phoneNumber="true" 9         android:hint="请输入第一个数"10         android:layout_height="wrap_content"11         android:layout_marginTop="5px"12         android:layout_marginLeft="5px" />13     <EditText14         android:id="@+id/txtSMS"15         android:layout_width="313.3dp"16         android:layout_height="40.0dp"17         android:phoneNumber="true"18         android:gravity="top"19         android:hint="请输入第二个数"20         android:layout_below="@id/txtPhoneNo" />21     <Button22         android:id="@+id/btnadd"23         android:text="加"24         android:layout_width="69dp"25         android:layout_height="wrap_content"26         android:layout_below="@id/txtSMS" />27     <Button28         android:id="@+id/btnjian"29         android:text="减"30         android:layout_width="69dp"31         android:layout_height="wrap_content"32         android:layout_below="@id/txtSMS"33         android:layout_toRightOf="@id/btnadd" />34     <Button35         android:id="@+id/btncheng"36         android:text="乘"37         android:layout_width="69dp"38         android:layout_height="wrap_content"39         android:layout_below="@id/txtSMS"40         android:layout_toRightOf="@id/btnjian" />41     <Button42         android:id="@+id/btnchu"43         android:text="除"44         android:layout_width="69dp"45         android:layout_height="wrap_content"46         android:layout_below="@id/txtSMS"47         android:layout_toRightOf="@id/btncheng" />48     <EditText49         android:id="@+id/txtjieguo"50         android:layout_width="fill_parent"51         android:layout_height="40.7dp"52         android:gravity="top"53         android:hint=""54         android:layout_below="@id/btnchu" />55 </RelativeLayout>
View Code

6.Activity1.cs

技术分享
using System;using Android.App;using Android.Content;using Android.Runtime;using Android.Views;using Android.Widget;using Android.OS;namespace Databasedemo{    [Activity(Label = "Databasedemo", MainLauncher = true, Icon = "@drawable/icon")]    public class Activity1 : Activity    {        EditText tvjg;        EditText num1;        EditText num2;        double num = 0;        protected override void OnCreate(Bundle bundle)        {            base.OnCreate(bundle);            // Set our view from the "main" layout resource            SetContentView(Resource.Layout.Main);            //            Button btnadd = FindViewById<Button>(Resource.Id.btnadd);            //            Button btnjian = FindViewById<Button>(Resource.Id.btnjian);            //            Button btncheng = FindViewById<Button>(Resource.Id.btncheng);            //            Button btnchu = FindViewById<Button>(Resource.Id.btnchu);            //第一个数            num1 = FindViewById<EditText>(Resource.Id.txtPhoneNo);            //第二个数            num2 = FindViewById<EditText>(Resource.Id.txtSMS);            //结果            tvjg = FindViewById<EditText>(Resource.Id.txtjieguo);            var ca = new WebReference.CalculatorService();             btnadd.Click += delegate             {                                  double num1s = Convert.ToDouble(num1.Text.ToString());                 double num2s = Convert.ToDouble(num2.Text.ToString());                 string flag = ca.Add(num1s, num2s, ref num);                 tvjg.Text = num1s.ToString() + "+" + num2s.ToString() + "=" + num.ToString();             };             btnjian.Click += delegate             {                 double num1s = Convert.ToDouble(num1.Text.ToString());                 double num2s = Convert.ToDouble(num2.Text.ToString());                 string flag = ca.Reduce(num1s, num2s, ref num);                 tvjg.Text = num1s.ToString() + "-" + num2s.ToString() + "=" + num.ToString();             };             btncheng.Click += delegate             {                 double num1s = Convert.ToDouble(num1.Text.ToString());                 double num2s = Convert.ToDouble(num2.Text.ToString());                 string flag = ca.Division(num1s, num2s, ref num);                 tvjg.Text = num1s.ToString() + "*" + num2s.ToString() + "=" + num.ToString();             };             btnchu.Click += delegate             {                 double num1s = Convert.ToDouble(num1.Text.ToString());                 double num2s = Convert.ToDouble(num2.Text.ToString());                 string flag = ca.Multiplication(num1s, num2s, ref num);                 tvjg.Text = num1s.ToString() + "/" + num2s.ToString() + "=" + num.ToString();             };        }          }}
View Code

模拟器

技术分享

运行效果

技术分享

 

源码下载:

WCFforIIS.zip

Databasedemo.zip

 

Xamarin.Android 入门实例(2)之实现WCF 寄宿于IIS 的Web服务提供