服务应用之WEB与WCF使用之见

APP_Code

WEB:

WebService.cs
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Services;
6
7 /// <summary>
8 ///WebService 的摘要说明
9 /// </summary>
10 [WebService(Namespace = "http://tempuri.org/")]
11 //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
12 [WebServiceBinding(ConformsTo = WsiProfiles.None)]
13 //若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
14 // [System.Web.Script.Services.ScriptService]
15 public class WebService : System.Web.Services.WebService {
16
17 public WebService () {
18
19 //如果使用设计的组件,请取消注释以下行
20 //InitializeComponent();
21 }
22
23 [WebMethod(MessageName ="auto")]
24 public string HelloWorld() {
25 return "Hello World";
26 }
27 [WebMethod(MessageName = "HelloWorldByName")]
28 public string HelloWorld(string name) {
29 return "Hello World " + name;
30 }
31
32 }

WCF

IService.cs
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Runtime.Serialization;
5 using System.ServiceModel;
6 using System.Text;
7
8 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService”。
9 [ServiceContract]
10 public interface IService
11 {
12 [OperationContract]
13 int Add(int a, int b);
14 [OperationContract]
15 int Subtract(int a, int b);
16 }
Service.cs
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Runtime.Serialization;
5 using System.ServiceModel;
6 using System.Text;
7
8 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service”。
9 public class Service : IService
10 {
11 public int Add(int a, int b)
12 {
13 return a + b;
14 }
15 public int Subtract(int a, int b)
16 {
17 return (a-b);
18 }
19 }

启动文件:

Service.svc
1 <%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
WebService.asmx
1 <%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>

测试:

test
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7
8 public partial class _Default : System.Web.UI.Page
9 {
10 protected void Page_Load(object sender, EventArgs e)
11 {
12
13 }
14 protected void Button1_Click(object sender, EventArgs e)
15 {
16 AspService.WebService test = new AspService.WebService();
17 Label3.Text = test.HelloWorld();
18 }
19 protected void Button2_Click(object sender, EventArgs e)
20 {
21 AspService.WebService test1 = new AspService.WebService();
22 Label4.Text = test1.HelloWorld(TextBox1 .Text);
23 }
24 protected void Button3_Click(object sender, EventArgs e)
25 {
26 int a = int.Parse(TextBox2.Text);
27 int b = int.Parse(TextBox3 .Text );
28 wcfService.ServiceClient test = new wcfService.ServiceClient();
29 Label5 .Text = test.Add(a, b).ToString ();
30 test.Close();
31 }
32 protected void Button4_Click(object sender, EventArgs e)
33 {
34 int a = int.Parse(TextBox2.Text);
35 int b = int.Parse(TextBox3.Text);
36 wcfService.ServiceClient test1 = new wcfService.ServiceClient();
37 Label5.Text = test1.Subtract(a, b).ToString ();
38 test1.Close();
39 }
40 }








原文地址:https://www.cnblogs.com/lvfeilong/p/example.html