WCF学习入门之服务托管

在VS2010下新建一个WCF项目的过程

一、 1、先在新建项目下新建一个WCF服务库
2、新建之后则自动生成一个IServce1的接口契约[ServiceContract],在其内部可以定义要进行业务操作的接口在定义的接口上添加一个操作契约[OperationContract]

3、添加数据契约:这些字段为要进行传送的数据实体契约为[DataContract] 针对字段的操作的契约是[DataMember]

二、到此一个WCF服务模型基本建立,接下来就应该建立服务托管,托管有两种方式一种是托管在WindowsForm上,另一种是托管在IIS服务器上,下面写出托管在WindowsForm上的一种方式

1、先建立一个窗体,窗体上放置一人按钮控件,用来启动服务。

2、在按钮单击事件中写下下列事件

//宿主服务

        ServiceHost Host;

        private void button1_Click(object sender, EventArgs e)

        {

            Uri uri = null;

            if (Uri.TryCreate("http://192.168.15.236:8089", UriKind.Absolute, out uri))

            {

                Host = new ServiceHost(typeof(HHChainSystemServiceLibrary.WCFService), uri);

                //服务启动禁用按钮

                Host.Opened += new EventHandler((s1, e1) =>

                {

                    this.button1.Enabled = false;

                }

                );

                //服务关闭开启按钮

                Host.Closed += new EventHandler((s2, e2) =>

                {

                    this.button1.Enabled = true;

                }

                );

                //打开服务

                Host.Open();

            }





        }



        private void Form1_FormClosed(object sender, FormClosedEventArgs e)

        {

            //关闭服务

            if (Host != null)

                Host.Close();

        }

在定义接口服务时的代码如下:

[ServiceContract]

    public interface IWCFService

    {

        /// <summary>

        /// 根据卡号,身份证,组织获取卡,会员,AR账户信息

        /// </summary>

        /// <param name="cardNumber">卡号</param>

        /// <param name="idCard">身份证号</param>

        /// <param name="Org">所属组织</param>

        /// <param name="token">访问授权码</param>

        /// <returns>集合</returns>

        [OperationContract, WebGet(UriTemplate = "Card_AR_User/?cardNumber={cardNumber}&idCard={idCard}&Org={Org}&token={token}", ResponseFormat = WebMessageFormat.Json)]

        IList<Card_AR_User> GetCard_AR_User(string cardNumber,string idCard,string Org,string token);

        /// <summary>

        /// 卡充值(刷卡)

        /// </summary>

        /// <param name="cardNumber">卡号</param>

        /// <param name="Org">充值组织</param>

        /// <param name="token">访问授权码</param>

        /// <returns>success/fail</returns>

        [OperationContract, WebGet(UriTemplate = "CardRecharge/?cardNumber={cardNumber}&ChargeMoney={rechargeMoney}&Org={Org}&token={token}", ResponseFormat = WebMessageFormat.Json)]

        bool CardRecharge(string cardNumber, string Org, decimal rechargeMoney, string token);

        /// <summary>

        /// 卡消费(刷卡)

        /// </summary>

        /// <param name="cardNumber">卡号</param>

        /// <param name="idCard">身份证号</param>

        /// <param name="Org">所属组织</param>

        /// <param name="token">访问授权码</param>

        /// <returns>success/fail</returns>

        [OperationContract, WebGet(UriTemplate = "ConsumeMoney/?cardNumber={cardNumber}&consumeMoney={consumeMoney}&Org={Org}&token={token}", ResponseFormat = WebMessageFormat.Json)]

        bool ConsumeMoney(string cardNumber, decimal consumeMoney, string Org, string token);



        /// <summary>

        /// 查询某会员卡的充值记录

        /// </summary>

        /// <param name="cardNumber">卡号</param>

        /// <param name="token">访问授权码</param>

        /// <returns>集合</returns>

        [OperationContract, WebGet(UriTemplate = "GetRechargeRecordBycardNumber/?cardNumber={cardNumber}&token={token}", ResponseFormat = WebMessageFormat.Json)]

        DataTable GetRechargeRecordBycardNumber(string cardNumber, string token);



        /// <summary>

        /// 查询某会员卡的消费记录

        /// </summary>

        /// <param name="cardNumber">卡号</param>

        /// <param name="token">访问授权码</param>

        /// <returns>集合</returns>

        [OperationContract, WebGet(UriTemplate = "GetConsumeRecordBycardNumber/?cardNumber={cardNumber}&token={token}", ResponseFormat = WebMessageFormat.Json)]

        DataTable GetConsumeRecordBycardNumber(string cardNumber, string token); 

    }

 

你可能感兴趣的:(WCF)