WCF应用示例(2)

    技术2022-05-12  24

     本实例较之前示例区别在于,充分利用app.config对项目设置

    *******************The Client:***************************

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.ServiceModel.Channels;using IHelloEmployeeInterface;namespace EmployeeClient{    class Program    {        static void Main(string[] args)        {            ChannelFactory<IHelloEmployeeInterface.IHelloEmployee> channel = new ChannelFactory<IHelloEmployee>("httpEmployeeClient");            IHelloEmployee proxy = channel.CreateChannel();            Console.WriteLine("please input a employee's id...");            string empid = Console.ReadLine();            Console.WriteLine(proxy.GetEmployeeByEmpID(empid));            string upEmpid=Console.ReadLine();            IList<DataSchema.EmployeeInfo> lst = proxy.GetEmployeeLstByEmpID(upEmpid, empid);            for (int i = 0; i < lst.Count; i++)            {                DataSchema.EmployeeInfo em = lst[i];                Console.WriteLine("the EmpID IS:" + em.EmpID + "   and name is :" + em.Name);            }            Console.Read();

            }    }}

     

    **********************Client app.config:**********************

    <?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <client>      <endpoint name="httpEmployeeClient" address="http://localhost:8731/Employee" binding="basicHttpBinding" contract="IHelloEmployeeInterface.IHelloEmployee"></endpoint>      <endpoint name="tcpEmployeeClient" address="net.tcp://localhost:8732/Employee" binding="netTcpBinding" contract="IHelloEmployeeInterface.IHelloEmployee"></endpoint>    </client>  </system.serviceModel></configuration>

    *******************************数据契约********************************

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.Runtime.Serialization;namespace DataSchema{    [DataContract]    public class EmployeeInfo    {        private string _Name;        private string _EmpID;        [DataMember]        public string Name        {            get { return _Name; }            set { _Name = value; }        }        [DataMember]        public string EmpID        {            get { return _EmpID; }            set { _EmpID = value; }        }    }}******************************service contract******************************

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.Runtime.Serialization;

    namespace IHelloEmployeeInterface{    [ServiceContract]    public interface IHelloEmployee    {        [OperationContract]        string GetEmployeeByEmpID(string EmpID);        [OperationContract]        IList<DataSchema.EmployeeInfo> GetEmployeeLstByEmpID(string UpEmpID, string LowerEmpID);    }}

    ******************************Service contract Implemented*************************

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using IHelloEmployeeInterface;using System.Data.SqlClient;using System.Data;namespace HelloEmployee{    public class Employee:IHelloEmployeeInterface.IHelloEmployee    {       

     

            #region IHelloEmployee 成员

            string IHelloEmployee.GetEmployeeByEmpID(string EmpID)        {            string rStr = string.Format("Receive message at{0}:{1}", DateTime.Now.ToString(), EmpID);            Console.WriteLine(rStr);            string conStr = "user=sa;password=oadev2004;server=10.200.6.103;database=hroa";            SqlConnection con = new SqlConnection(conStr);            SqlCommand cmd = new SqlCommand("select * from Hr_EmployeeInfo where EmpID=" + EmpID, con);            if (con.State == ConnectionState.Closed)                con.Open();            SqlDataReader Reader = cmd.ExecuteReader();            while (Reader.Read())            {                rStr = "返回处理结果为:用户名是--" + Reader["Name"].ToString();            }            Console.WriteLine(rStr);            Reader.Close();            Reader.Dispose();            con.Close();            cmd.Dispose();

                return rStr;        }

            IList<DataSchema.EmployeeInfo> IHelloEmployee.GetEmployeeLstByEmpID(string UpEmpID, string LowerEmpID)        {            IList<DataSchema.EmployeeInfo> LST = new List<DataSchema.EmployeeInfo>();            string conStr = "user=sa;password=oadev2004;server=10.200.6.103;database=hroa";            SqlConnection con = new SqlConnection(conStr);            SqlCommand cmd = new SqlCommand("select * from hr_EmployeeInfo where Empid>=" + LowerEmpID + " and empid<=" + UpEmpID, con);            SqlDataAdapter ap = new SqlDataAdapter(cmd);            DataSet ds = new DataSet();            ap.Fill(ds);            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)            {                DataRow dr = ds.Tables[0].Rows[j];                DataSchema.EmployeeInfo em = new DataSchema.EmployeeInfo();                em.Name = dr["Name"].ToString();                em.EmpID = dr["EmpID"].ToString();                LST.Add(em);            }            return LST;        }

            #endregion    }}

    ***********************************service host*******************************

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;

    namespace EmployeeServiceHost{    class Program    {        static void Main(string[] args)        {            using (ServiceHost host = new ServiceHost(typeof(HelloEmployee.Employee)))            {                host.Opened += new EventHandler(host_Opened);                host.Open();                Console.WriteLine("Please enter any key to exit");                Console.ReadLine();            }        }

            static void host_Opened(object sender, EventArgs e)        {            Console.WriteLine("the employeeservice is running...");        }    }}

    ***********************************service app.config configuration**********************

    <?xml version="1.0" encoding="utf-8" ?><configuration>  <system.serviceModel>    <services>      <service name="HelloEmployee.Employee" behaviorConfiguration="BehaviorOfHelloEmployee">        <endpoint address="" binding="basicHttpBinding" name="httpEndPoint" contract="IHelloEmployeeInterface.IHelloEmployee"></endpoint>        <endpoint address="" binding="netTcpBinding" name="TcpEndPoint"  contract="IHelloEmployeeInterface.IHelloEmployee"></endpoint>        <host>          <baseAddresses>            <add baseAddress="http://localhost:8731/Employee"/>            <add baseAddress="net.tcp://localhost:8732/Employee"/>          </baseAddresses>        </host>      </service>          </services>    <behaviors>      <serviceBehaviors>        <behavior name="BehaviorOfHelloEmployee">          <serviceMetadata httpGetEnabled="true"></serviceMetadata>        </behavior>      </serviceBehaviors>    </behaviors>  </system.serviceModel></configuration>


    最新回复(0)