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


    服务端

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

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

    Student.cs:

    /// <summary>
        /// DataContract数据约定,保证student类在WCF调用中被序列化
        /// DataMember 在被序列化的成员变量中必须加 [DataMember]标签
        /// </summary>
        [DataContract]
        public class Student
        {
            [DataMember]
            public string StudentId { get; set; }
            [DataMember]
            public string StudentName { get; set; }
        }
    View Code

    IStudentService.cs:

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

    StudentService.svc:

    public class StudentService : IStudentService
        {
    
            public List<Student> RemoveStudent(string id)
            {
                var students = new List<Student>() { 
                    new Student {StudentId="1",StudentName="学生1" }, 
                    new Student {StudentId="2",StudentName="学生2" } 
                };
    
                var student = students.Find(m => m.StudentId == id);
    
                students.Remove(student);
    
                return students;
            }
        }
    View Code

    vs2012-->Visual Studio Tools-->VS2012 x86 本机工具命令提示   -->输入 wcfhtestclient     可以出现下图!   客户端直接测试。

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

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

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

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

    客户端

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

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

    如图:

    WCFTest.aspx:

    <form id="form1" runat="server">
            <div>
                <table>
                    <tr>
                        <td> <asp:TextBox ID="txtStudentId" runat="server"></asp:TextBox></td>
                         <td><asp:Button ID="btnSubmint" runat="server"  Text="删除" OnClick="btnSubmint_OnClick"/></td>
                    </tr>
                </table>
               
            </div>
        </form>
    View Code

    WCFTest.aspx.cs:

    protected void btnSubmint_OnClick(object sender, EventArgs e)
            {
                var wcfService = new WCFService.StudentServiceClient();
    
                var str = string.Empty;
    
                wcfService.RemoveStudent(this.txtStudentId.Text.Trim())
                    .ToList()
                    .ForEach(m =>str += m.StudentId + ":" + m.StudentName);
    
                Response.Write(str);
            }
    View Code

    运行下,看下效果:

     
     
     
  • 相关阅读:
    SQL 判断字符包含在记录中出现了多少次
    JS 数据类型判断
    JS object转日期
    JS 日期转字符串
    Linux系统优化03-centos7网卡配置
    Linux系统安装02-centos7系统安装-分区及基本设置
    Linux系统安装01-centos7系统安装
    解决winserver2012R2安装VMware15(pro)问题
    Tomcat 如何设置Tomcat的标题,运行Tomcat显示为自己程序的命名
    IntelliJ Idea 常用快捷键列表
  • 原文地址:https://www.cnblogs.com/weixing/p/4415231.html
Copyright © 2020-2023  润新知