• WCF系列之一个简单的Demo


    一、前言

        前面的几个章节介绍了很多理论基础,如:什么是WCF、WCF中的A、B、C。WCF的传输模式。本文从零开始和大家一起写一个小的WCF应用程序Demo。

        大多框架的学习都是从增、删、改、查开始来学习的,我们学习WCF也是一样的。从简单来看(不包括安全、优化等相关问题),WCF的增删改查和WebForm相差无几。WCF只是把具体“实现”写在“Service端”,而“调用”放在了“Client端”。觉得有帮助别忘了点个赞哈,谢谢哦~

    二、Demo说明

    1)Demo的 “Service端”以本机IIS为宿主,“Client端”以WebForm项目为例。

    2)Demo的“Service端”提取数据采用初学者比较容易接受的分层结构进行搭建,分别分为服务层、实体层、数据层。

    引用关系如下图所示:

    3)Demo以数据库为SqlServer,表User为例(sql语句在下载的压缩包中Init.sql),表结构如下所示:

    image

    三、创建Service端宿主

    1)创建WCF应用程序命名为:WCF.Demo.Service,如下图:

    2)删除默认文件IService.cs与Service.svc。并分别创建增、删、改、查”Add.svc”、“Save.svc”、“Remove.svc”、“Get.svc,Search.svc”,分别对应4个功能的服务应用程序WCF服务应用程序,并创建数据操作层和数据实体层,如下图:

    3)增加实体层和数据操作层代码,如下所示:

    //用户实体
        [DataContract]
        public class User
        {
            [DataMember]
            public int UserID { get; set; }
            [DataMember]
            public string UserName { get; set; }
            [DataMember]
            public string Password { get; set; }
            [DataMember]
            public string Discribe { get; set; }
            [DataMember]
            public DateTime SubmitTime { get; set; }
        }
        //数据操作,调用SqlHeler
        public class User
        {
            private static readonly string connectionString = "server=.;database=wcfDemo;uid=sa;pwd=123456;";
    
            //添加
            public static bool Add(Model.User user)
            {
                string sql = string.Format("INSERT INTO [dbo].[User]([UserName],[Password],[Discribe],[SubmitTime]) VALUES('{0}','{1}','{2}','{3}')", user.UserName, user.Password, user.Discribe, user.SubmitTime);
                int result = SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql);
                if (result > 0)
                    return true;
                else
                    return false;
            }
    
            //修改
            public static bool Save(Model.User user)
            {
                string sql = string.Format("UPDATE [dbo].[User] SET [UserName] = '{0}',[Discribe] = '{2}',[SubmitTime] = '{3}' WHERE UserID = {4}", user.UserName, user.Password, user.Discribe, user.SubmitTime, user.UserID);
                int result = SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql);
                if (result > 0)
                    return true;
                else
                    return false;
            }
    
            //删除
            public static bool Remove(int UserID)
            {
                string sql = string.Format("DELETE FROM [dbo].[User] WHERE UserID = {0}", UserID);
                int result = SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql);
                if (result > 0)
                    return true;
                else
                    return false;
            }
    
            //获取用户
            public static Model.User Get(int UserID)
            {
                Model.User user = new Model.User();
                string sql = string.Format("SELECT * FROM [dbo].[User] WHERE UserID = {0}", UserID);
                DataSet ds = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        user.UserID = Convert.ToInt32(dr["UserID"]);
                        user.UserName = dr["UserName"].ToString();
                        user.Password = dr["Password"].ToString();
                        user.Discribe = dr["Discribe"].ToString();
                        user.SubmitTime = Convert.ToDateTime(dr["SubmitTime"]);
                    }
                }
                return user;
            }
    
            //获取用户列表
            public static List<Model.User> GetUsers()
            {
                List<Model.User> Users = new List<Model.User>();
                string sql = string.Format("SELECT * FROM [dbo].[User]");
                DataSet ds = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, sql);
                if (ds != null && ds.Tables.Count > 0)
                {
                    foreach (DataTable dt in ds.Tables)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            Model.User user = new Model.User();
                            user.UserID = Convert.ToInt32(dr["UserID"]);
                            user.UserName = dr["UserName"].ToString();
                            user.Password = dr["Password"].ToString();
                            user.Discribe = dr["Discribe"].ToString();
                            user.SubmitTime = Convert.ToDateTime(dr["SubmitTime"]);
                            Users.Add(user);
                        }
                    }
                }
                return Users;
            }
        }

    4)修改Add接口的代码和实现,如下所示:

    [ServiceContract]
        public interface IAdd
        {
            [OperationContract]
            bool DoWork(Model.User user);
        }
    
        public class Add : IAdd
        {
            public bool DoWork(Model.User user)
            {
                return DAL.User.Add(user);
            }
        }

    5)修改Save接口的代码和实现,如下所示:

    [ServiceContract]
        public interface ISave
        {
            [OperationContract]
            bool DoWork(Model.User user);
        }
    
        public class Save : ISave
        {
            public bool DoWork(Model.User user)
            {
                return DAL.User.Save(user);
            }
        }

    6)修改Remove接口的代码和实现,如下所示:

    [ServiceContract]
        public interface IRemove
        {
            [OperationContract]
            bool DoWork(int UserID);
        }
        public class Remove : IRemove
        {
            public bool DoWork(int UserID)
            {
                return DAL.User.Remove(UserID);
            }
        }

    7)修改Search接口的代码和实现,如下所示:

    [ServiceContract]
        public interface ISearch
        {
            [OperationContract]
            List<Model.User> DoWork();
        }
    
        public class Search : ISearch
        {
            List<Model.User> ISearch.DoWork()
            {
                return DAL.User.GetUsers();
            }
        }

    8)修改Get接口的代码和实现,如下所示:

    [ServiceContract]
        public interface IGet
        {
            [OperationContract]
            Model.User DoWork(int UserID);
        }
    
        public class Get : IGet
        {
            public Model.User DoWork(int UserID)
            {
                return DAL.User.Get(UserID);
            }
        }

    四、部署服务端程序

    1)将程序发布,并部署到IIS上,设置端口:8080,如下图所示:

    2)以Add.svc服务应用程序为目标,测试部署是否成功,成功后如下图所示:

    五、创建Client端Web应用程序

    新建WebForm项目WCF.Demo.Client,并创建增删改查文件,Add.aspx,Save.aspx,SearchAndRemove.aspx。如下图所示:

    六、使用SvcUtil.exe生成客户端代码和配置

    SvcUtil.exe是一个VS命令行工具,该工具位于:C:Program FilesMicrosoft  SDKsWindowsv7.0Ain 或 C:Program Files (x86)Microsoft SDKsWindowsv7.0Ain一般情况下我们将SvcUtil.exe添加到VS开发工具中方便以后的运用(也可直接使用该命令行工具)。

    1)在VS中的 Tools菜单---选择External Tools,打开管理窗口如下图所示:

    在Title中输入SvcUtil,Command中选择SvcUtil.exe全路径,Initial  directory栏选择生成的客户端代码和配置文件所放的目录(此处为解决方案所在目录),选上Prompt for arguments,不选上Close on  exit。点击OK.

    2)添加完成后,在VS的工具下会出现这个菜单。如下图所示:

    3)在Client端添加对服务的引用。打开SvUtil工具,在Arguments里填写服务的地址,如下图:

    点击OK后出现下图,说明代码类和配置文件生成成功(在解决方案目标下),如下图所示:

    此时代理类和配置文件被下载到解决方案的物理目录中,如下图所示:

    七、在Client端使用代理类与配置

    将代理类从服务端的物理目录拷贝出来,放到Client端,并适当的修改代码,加入自己需要的名称空间,如下图所示:

    使用代码如下所示:

    //增加
        public partial class Add : System.Web.UI.Page
        {
            Service.AddClient addClient = new Service.AddClient();
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            //提交
            protected void btnSubmit_Click(object sender, EventArgs e)
            {
                Model.User user = new Model.User();
                user.UserName = this.txtUserName.Text;
                user.Password = this.txtPassword.Text;
                user.Discribe = this.txtDiscribe.Text;
                user.SubmitTime = System.DateTime.Now;
                addClient.DoWork(user);
                Response.Write("添加成功!");
            }
        }
        //修改
        public partial class Save : System.Web.UI.Page
        {
            Service.SaveClient saveClient = new Service.SaveClient();
            Service.GetClient getClient = new Service.GetClient();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack && !string.IsNullOrEmpty(Request.QueryString["UserID"]))
                {
                    GetUser();
                }
            }
    
            protected void GetUser()
            {
                int UserID = Convert.ToInt32(Request.QueryString["UserID"]);
                Model.User user = getClient.DoWork(UserID);
                this.txtUserName.Text = user.UserName;
                this.txtDiscribe.Text = user.Discribe;
            }
    
            //提交
            protected void btnSubmit_Click(object sender, EventArgs e)
            {
                int UserID = Convert.ToInt32(Request.QueryString["UserID"]);
                Model.User user = getClient.DoWork(UserID);
                user.UserName = this.txtUserName.Text;
                user.Discribe = this.txtDiscribe.Text;
                saveClient.DoWork(user);
                Response.Write("修改成功!");
            }
        }
        //列表及删除
        public partial class SearchAndRemove : System.Web.UI.Page
        {
            Service.SearchClient searchClient = new Service.SearchClient();
            Service.RemoveClient removeClient = new Service.RemoveClient();
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    GetUsers();
                }
            }
    
            protected void GetUsers()
            {
                this.repUsers.DataSource = searchClient.DoWork();
                this.repUsers.DataBind();
            }
    
            protected void lbtnRemoveCommand(object sender, CommandEventArgs e)
            {
                int UserID = Convert.ToInt32(e.CommandName);
                removeClient.DoWork(UserID);
                Response.Write("删除成功~");
                GetUsers();
            }
        }

    将生成的配置文件中的 <system.serviceModel>复制到Client的Web.config中,代码如下:

    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IAdd" closeTimeout="00:01:00"
                openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
              <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                  maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None"
                    realm="" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
            </binding>
            <binding name="BasicHttpBinding_IRemove" closeTimeout="00:01:00"
                       openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                       allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                       maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                       messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                       useDefaultWebProxy="true">
              <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                  maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None"
                    realm="" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
            </binding>
            <binding name="BasicHttpBinding_ISearch" closeTimeout="00:01:00"
               openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
               allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
               maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
               messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
               useDefaultWebProxy="true">
              <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                  maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None"
                    realm="" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
            </binding>
            <binding name="BasicHttpBinding_ISave" closeTimeout="00:01:00"
                       openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                       allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                       maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                       messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                       useDefaultWebProxy="true">
              <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                  maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None"
                    realm="" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
            </binding>
            <binding name="BasicHttpBinding_IGet" closeTimeout="00:01:00"
                       openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                       allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                       maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                       messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                       useDefaultWebProxy="true">
              <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                  maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <security mode="None">
                <transport clientCredentialType="None" proxyCredentialType="None"
                    realm="" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
              </security>
            </binding>
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://localhost:8080/Add.svc" binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IAdd" contract="IAdd"
              name="BasicHttpBinding_IAdd" />
          <endpoint address="http://localhost:8080/Remove.svc" binding="basicHttpBinding"
                  bindingConfiguration="BasicHttpBinding_IRemove" contract="IRemove"
                  name="BasicHttpBinding_IRemove" />
          <endpoint address="http://localhost:8080/Search.svc" binding="basicHttpBinding"
                 bindingConfiguration="BasicHttpBinding_ISearch" contract="ISearch"
                 name="BasicHttpBinding_ISearch" />
          <endpoint address="http://localhost:8080/Save.svc" binding="basicHttpBinding"
                   bindingConfiguration="BasicHttpBinding_ISave" contract="ISave"
                   name="BasicHttpBinding_ISave" />
          <endpoint address="http://localhost:8080/Get.svc" binding="basicHttpBinding"
                   bindingConfiguration="BasicHttpBinding_IGet" contract="IGet"
                   name="BasicHttpBinding_IGet" />
        </client>
      </system.serviceModel>

    -------------------------最终效果-----------------------

    添加:

    修改:

    列表及删除:

    八、源码下载

    点我下载本文章Demo

    版权:http://www.cnblogs.com/iamlilinfeng
  • 相关阅读:
    使用Strust2框架写HelloWorld
    MyEclipse10搭建Strust2开发环境
    MyEclipse使用总结——在MyEclipse中设置jsp页面为默认utf-8编码
    HTML一些标签注意事项
    Spring常用注解
    VB.NET中Module的概念
    vs2008发布项目失败的解决方法
    Java开发中的一些小技巧
    DNS服务器
    关于在Struts2的Action中使用domain模型接收参数的问题
  • 原文地址:https://www.cnblogs.com/cmhunter/p/4245462.html
Copyright © 2020-2023  润新知