• 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>

  • 相关阅读:
    skydriver 工具 SDExplorer
    微软出手了:Live Office与Google 文档大PK
    4§7 二次曲面的直纹性
    5§4 二次曲线的直径
    5§5 二次曲线的主直径
    AI第四章 计算智能(1)
    矩阵理论 第五讲 对角化与Jordan标准形
    矩阵理论第 四讲 矩阵的对角化
    5§7 二次曲线方程的化简与分类
    矩阵论 ppt
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050641.html
Copyright © 2020-2023  润新知