WCF通訊簡單樣本

來源:互聯網
上載者:User

   今天寫了一個WCF demo,給大家分享,說明如下

     服務介面類庫WCFService.dll

     服務實作類別庫WCFServicImp.dll

     服務宿主專案WCFHost

     用戶端調用項目:WCFTest

    大家下載完DEMO啟動並執行時候,先運行伺服器端WCFHost,然後再運行用戶端WCFTest進行調用,關於資料庫伺服器的地址請自己配置

    請大家注意下伺服器端和用戶端的app.config檔案,關於WCF服務的所有配置,都在這兩個檔案中進行配置,

    用戶端調用服務的時候,要引用介面類庫 WCFService.dll

  代碼

   伺服器端宿主

   

public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        static List<ServiceHost> _hosts = new List<ServiceHost>();        private void Form1_Load(object sender, EventArgs e)        {            //啟動伺服器端          Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);            ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");            foreach (ServiceElement el in svcmod.Services.Services)            {                Type svcType = Type.GetType(el.Name + "," + el.Name.Substring(0, el.Name.LastIndexOf('.')));                if (svcType == null)                {                    throw new Exception("設定檔中的服務 [" + el.Name + "] 無效.");                }                ServiceHost hst = new ServiceHost(svcType);                _hosts.Add(hst);                hst.Open();            }        }    }


    用戶端調用代碼

   

    private void button1_Click(object sender, EventArgs e)        {            using (ChannelFactory<WCFService.ICity> channelFactory = new ChannelFactory<WCFService.ICity>("CityImp"))            {                WCFService.ICity proxy = channelFactory.CreateChannel();                this.dataGridView1.DataSource = proxy.GetAllCity().Tables[0];            }        }



     

   

 demo下載

 http://download.csdn.net/detail/zx13525079024/3739270

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.