WCF入门

契约:

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

namespace ClassLibrary1
{
    [ServiceContract]
    public interface myInterface
    {
        [OperationContract]
        string GetCurrentTime();
        [OperationContract]
        DataTable GetAlldata();
    }
}

宿主:

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

namespace winserver
{
    class myClass:ClassLibrary1.myInterface
    {

        public string GetCurrentTime()
        {
            return DateTime.Now.ToString();
        }

        public System.Data.DataTable GetAlldata()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("学号");
            dt.Columns.Add("姓名");
            dt.Columns.Add("年龄");
            DataRow dr = dt.NewRow();
            dr["学号"] = "200784251040";
            dr["姓名"]="wang";
            dr["年龄"]="30";
            dt.Rows.Add(dr);
            return dt;
        }

    }
}

配置文件实现:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>

      <service name="winserver.myClass" behaviorConfiguration="mybehavior">

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8002/test"/>
           
          </baseAddresses>
         
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="ClassLibrary1.myInterface">

        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mybehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
       
      </serviceBehaviors>
    </behaviors>
   
  </system.serviceModel>
</configuration>

代码方式实现:

using System;
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;

namespace winserver2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ServiceHost host = null;
        private void button1_Click(object sender, EventArgs e)
        {
            host=new ServiceHost(typeof(winserver2.myClass));
            NetTcpBinding tcpBinding=new NetTcpBinding();
            string address="net.tcp://localhost:3200/test";
            host.AddServiceEndpoint(typeof(ClassLibrary1.myInterface), tcpBinding, address);
            host.Opened += delegate { label1.Text = "服务已启动"; };
            host.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (host.State != CommunicationState.Closed)
            {
                host.Close();
            }
            label1.Text = "服务已停止";
        }
    }
}

客户端:

using System;
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;

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

        private void button1_Click(object sender, EventArgs e)
        {
            NetTcpBinding tcpBinding = new NetTcpBinding();
            string address = "net.tcp://localhost:3200/test";
            ChannelFactory<ClassLibrary1.myInterface> cfty = new ChannelFactory<ClassLibrary1.myInterface>(tcpBinding, address);
            ClassLibrary1.myInterface myobject=cfty.CreateChannel();
            label1.Text = myobject.GetCurrentTime();
        }
    }
}
BasicHttpBinding实现:

 Server端:


            servicehost=new ServiceHost(typeof(ClassLibrary2.myClass));
            servicehost.Opened += delegate { label1.Text = "服务器已启动!"; };
            servicehost.Open();

client端:

           EndpointAddress address=new EndpointAddress("http://localhost:3200/test");
            BasicHttpBinding httpBinding=new BasicHttpBinding();
            ChannelFactory<ClassLibrary1.myInterface> mychannel = new ChannelFactory<ClassLibrary1.myInterface>(httpBinding, address);
            ClassLibrary1.myInterface obj = mychannel.CreateChannel();
            label1.Text = obj.GetCurrentTime();

配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="ClassLibrary2.myClass" behaviorConfiguration="testBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:3200/test"></add>
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="ClassLibrary1.myInterface"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="testBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

原文地址:https://www.cnblogs.com/wangyhua/p/4050641.html