WCF客户端与服务端通信简单入门教程

服务端

1.新建空白解决方案,然后再空白解决方案中新建:WCF服务应用程序。建完后如图:

2.删掉自动生成的IService1.cs和Service.svc并添加WCF服务文件StudentService.svc,VS会自动生成IStudentService.cs 在添加一个Student类,如图:

Student.cs:

 1  /// <summary>
 2     /// DataContract数据约定,保证student类在WCF调用中被序列化
 3     /// DataMember 在被序列化的成员变量中必须加 [DataMember]标签
 4     /// </summary>
 5     [DataContract]
 6     public class Student
 7     {
 8         [DataMember]
 9         public string StudentId { get; set; }
10         [DataMember]
11         public string StudentName { get; set; }
12     }

IStudentService.cs:

 1      /// <summary>
 2     ///  ServiceContract:服务约定,代表我们所能操作的接口集合,提    供功能点。 
 3     ///  OperationContract: 每个对外需要发布的方法都需要加上此标签
 4     /// </summary>
 5     [ServiceContract]
 6     public interface IStudentService
 7     {
 8         [OperationContract]
 9         List<Student> RemoveStudent(string id);
10     }

StudentService.svc:

 1  public class StudentService : IStudentService
 2     {
 3 
 4         public List<Student> RemoveStudent(string id)
 5         {
 6             var students = new List<Student>() { 
 7                 new Student {StudentId="1",StudentName="学生1" }, 
 8                 new Student {StudentId="2",StudentName="学生2" } 
 9             };
10 
11             var student = students.Find(m => m.StudentId == id);
12 
13             students.Remove(student);
14 
15             return students;
16         }
17     }

到现在为止我们WCF服务端程序建好了,我们把StudentService.svc设为起始页,F5运行一下,会弹出来WCF测试客户端,如图

双击左侧的RemoveStudent(),在右侧输入值然后点击调用,如图:

结果如我们预料的那样,StudentId为1的数据被删掉了。

接下来我们把它部署到IIS上, 在默认文档里添加StudentService.svc,然后浏览,如图:

客户端

1.新建空白解决方案,新建ASP.NET WEB应用程序,名称为WCFClient,添加服务引用,服务引用地址为上图地址中的

http://localhost:88/StudentService.svc?wsdl 

,如图:

WCFTest.aspx:

 1 <form id="form1" runat="server">
 2         <div>
 3             <table>
 4                 <tr>
 5                     <td> <asp:TextBox ID="txtStudentId" runat="server"></asp:TextBox></td>
 6                      <td><asp:Button ID="btnSubmint" runat="server"  Text="删除" OnClick="btnSubmint_OnClick"/></td>
 7                 </tr>
 8             </table>
 9            
10         </div>
11     </form>

WCFTest.aspx.cs:

 1         protected void btnSubmint_OnClick(object sender, EventArgs e)
 2         {
 3             var wcfService = new WCFService.StudentServiceClient();
 4 
 5             var str = string.Empty;
 6 
 7             wcfService.RemoveStudent(this.txtStudentId.Text.Trim())
 8                 .ToList()
 9                 .ForEach(m =>str += m.StudentId + ":" + m.StudentName);
10 
11             Response.Write(str);
12         }

运行下,看下效果:

转载于:https://www.cnblogs.com/qiuyan/p/3265176.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值