简单的WCF实例

在解决方案中新建两个项目,一个做服务端,一个做客户端,

在服务端的项目中添加新项,添加一个WCF服务,名为XServer ,会添加两个文件IXService.cs 和Xserver  如下

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

namespace wcf_t2
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IXService”。
    [ServiceContract]
    public interface IXService
    {
        [OperationContract]
        void DoWork();

        [OperationContract]
        string Test();
    }
}

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

namespace wcf_t2
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“XService”。
    public class XService : IXService
    {
        public void DoWork()
        {
        }
        public string Test()
        {
            string temp =  "Hellow World!";

             return temp;
        }
    }
}

在客户端的项目中加入IXService.cs文件,内容与服务端相同,下面在程序中通过代码即可在客户端实现对服务端XService.cs中方法的调用

服务端需要先开启服务

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.Net;
using System.Threading;

namespace wcf_t2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

          }

          private ServiceHost _host;
          private void simpleButton1_Click(object sender, EventArgs e)
        {
            OpenSerice();
        }
        private void OpenSerice()
        {
            try
            {
               
                    IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
                    string srtAddress = string.Format(@"net.tcp://{0}:11230", ip[0]);
                    _host = new ServiceHost(typeof(XService));
                    _host.AddServiceEndpoint(typeof(IXService), new NetTcpBinding(), srtAddress);
                    _host.Open();

                labelControl1.Text = "服务已开启"             

           }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
          
           
        }

    }
}

 

客户端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using wcf_t2;
using System.ServiceModel;
using System.Threading;
namespace wcf_client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static IXService XService;
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(Show);
            thread.Start();

        }
        private void Show()
        {
            try
            {
                InitService();

                while (true)
                {
                  
                    System.Threading.Thread.Sleep(100);
                  BeginInvoke(new MethodInvoker(() =>
                  labelControl1.Text =   XService.Test()+count.ToString()
                        ));
                    count++;

                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }

        }
        private int count = 0;
        private bool InitService()
        {
            try
            {
                IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
                string srtAddress = string.Format(@"net.tcp://{0}:11230", ip[0]);
                ChannelFactory<IXService> factory = new ChannelFactory<IXService>
                    (new NetTcpBinding(), new EndpointAddress(srtAddress));
                XService = factory.CreateChannel();
            }
            catch
            {
                return false;
            }
            return true;
        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值