WCF-tcp

1.创建一个空的解决方案 在里面添加如下

c#演示下载链接:
//download.csdn.net/download/varcad/12038145
在这里插入图片描述

IService:用于定义服务契约,引用System.ServiceModel程序集。

Service:用于定义服务类型。引用System.ServiceModel程序集、IService、DAL。

SopService:控制台应用作为服务宿主,引用System.ServiceModel程序集、IService、Service

WindowsFormsAPP1:winfrom窗体作为客户端,引用System.ServiceModel程序集、IService

2.创建服务契约

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.Data;
namespace SOS.IService
{
    
        //注意:using System.ServiceModel   [ServiceContract]   [OperationContract]

    [ServiceContract]
    public interface IQE
    {
    
    
        [OperationContract]
        DataSet QESelect();
        [OperationContract]
        int Login(string name, string pws);
    }
}

3.创建服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using SOS.IService;
using System.Data;
using SOS.DAL;
namespace SOS.Service
{
    
        //注意:引用 ServiceModel IService
    [ServiceBehavior]
    public class QEService : IQE
    {
    
    
        QEDA da = new QEDA();

        public int Login(string name, string pws)
        {
    
    
            return da.Login(name,pws);
        }

        public DataSet QESelect() {
    
    
            return da.QESelect();
        }
        
    }
}

4.寄宿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using SOS.Service;
using SOS.IService;
using System.ServiceModel.Description;

namespace SOS.SopService
{
    
    
    //注意:using System.ServiceModel using System.ServiceModel.Description;
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            using (ServiceHost host = new ServiceHost(typeof(QEService)))
            {
    
    
                ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                    host.Description.Behaviors.Add(new ServiceMetadataBehavior());

             
                //开启服务
                host.Open();
                Console.WriteLine("开始监听");
                Console.WriteLine("按任意键关闭");
                Console.ReadKey();
                host.Abort();
                host.Close();
            }
        }
    }
}

服务端app.config 配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
   <add name="ConnectionString" connectionString="Data Source=192.168.1.207;Initial Catalog=11111; Persist Security Info=True;User ID=sa;Password=*" />
  </connectionStrings>
  <system.serviceModel>
  <services>
    <service name="SOS.Service.QEService" behaviorConfiguration="">
      <endpoint name="QEService" 
                address="net.tcp://192.168.1.207:5898/QEService"
                binding="netTcpBinding" contract="SOS.IService.IQE" />


    </service>
    
  </services>
    </system.serviceModel>
</configuration>

5.客户端调用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;
using SOS.IService;
namespace SOS.WindowsFormsApp1
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
    
    
            using (ChannelFactory<IQE> channelFactory = new ChannelFactory<IQE>("QEService"))
            {
    
    
                IQE qE = channelFactory.CreateChannel();
               
                DataSet sas = qE.QESelect();
                DataTable dt = sas.Tables[0];
             
              
                dataGridView1.DataSource = dt;
                dataGridView1.Refresh();
            }
           

        }

客户端app.config配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 
  <system.serviceModel>
   
   
    <client>
      <endpoint name="QEService"  address="net.tcp://192.168.1.207:5898/QEService"
                   binding="netTcpBinding" bindingConfiguration="" contract="SOS.IService.IQE" />
    </client>

  </system.serviceModel>
</configuration>

DBHelper

在这里插入图片描述

DAL

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/varcad/article/details/103599011
WCF