WCF总结笔记

------------------------windowform承载服务步骤:

(1)定义契约:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace IWpf

{

    [ServiceContract]

    public interface IUser

    {

        [OperationContract]

        string GetName();

        [OperationContract]

        int GetAge();

    }

}

(2)承载契约;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using IWpf;

namespace wpf承载服务

{

  public  class wpf:IUser

    {

 

        #region IUser 成员

 

        public string GetName()

        {

            return "郭泽峰";

        }

 

        public int GetAge()

        {

            return 13;

        }

        #endregion

    }

}

appconfig:配置契约:

?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <system.serviceModel>

    <services>

      <service name="wpf承载服务.wpf" behaviorConfiguration ="behaviorone">

        <host>

          <baseAddresses>

            <add baseAddress="http://localhost:1234/wpf"/>

          </baseAddresses>

        </host>

        <endpoint address="" binding="basicHttpBinding" contract="IWpf.IUser">

        </endpoint>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="behaviorone">

          <serviceMetadata httpGetEnabled="true"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>

-------------启动服务;

using System.ServiceModel;

namespace wpf承载服务

{

    public partial class Form1 : Form

    {

        ServiceHost host;

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

        {

            host = new ServiceHost(typeof(wpf));

            host.Open();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            host.Close();

        }

    }

}

-----------------使用契约;

  ServiceReference1.UserClient u = new ServiceReference1.UserClient();

  MessageBox.Show(u.GetName());

---------------------------------------------------------------------------完成;

--------------

 

 

 

---------------通过编程来承载和使用服务;

--承载服务

 public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        ServiceHost host = null;

        private void button1_Click(object sender, EventArgs e)

        {

            host = new ServiceHost(typeof(wpf));

            //定义绑定协议:

            NetTcpBinding tcpBinding = new NetTcpBinding();

            //定义地址:

            string s = "net.tcp://localhost:3200/gg";//路径随便了;

            host.AddServiceEndpoint(typeof(IWpf.IUser), tcpBinding, s);

            host.Opened += delegate { label1.Text = "已经打开"; };

            host.Closed += delegate { label1.Text = "已经关闭"; };

            host.Open();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            if (host.State == CommunicationState.Opened)

            {

                host.Close();

            }

        }

-----------------应用客户端:

           //绑定形式:

            NetTcpBinding bind = new NetTcpBinding();

            //地址

            EndpointAddress address = new EndpointAddress("net.tcp://localhost:3200/gg");

            //通道工厂

            ChannelFactory<IWpf.IUser> factory = new ChannelFactory<IWpf.IUser>(bind, address);

            IWpf.IUser user = factory.CreateChannel();

            string s = user.GetName();

            MessageBox.Show(s);

 

-----------------通信方式:单工(无需等待方法名void,且参数部位out;ref;);请求响应(需要等待);双攻;

单工:[OperationContract(IsOneWay=true)]即可;

-------

 

---SOA:属于一种基于组件的架构模型;它可以根据需求对松散耦合的粗粒度应用组件

进行分布式部署,组合和使用。

SOA的四个特性:

1.每个服务必须有明确的服务边界;

2.服务是独立的;

3.采用标准的契约定义和通信协议;

4.服务是自解释的;

//地址:

http://dddd:5050/;

net.tcp:dec:3030/myservice;

net.msmq:localhost/mymsmq;

-----------------------------------------

soa的好处:在用户层和一万五层添加了一层服务层,将

力度比较小的互操作的对象进一步封装,形成粗粒度

组件,从而达到一种松耦合的状态;

--------------------------------------------

tcp:二进制;效率远远高于http;多用于内网传输;

http:soap;

---------------------------------------------

wcf:

1.面向服务;

2.松耦合;

3,可交互;

4.整合性;

-----------------------------------------

remoting和wevservice区别:

webservice:不限平台,http协议,基于xml;效率低;无状态;

remoting:仅限。net平台,网络传输协议(不一定是http),二进制;效率很高;有状态;

remoting是.net 中用来跨越machine, process, appdomain 进行方法调用的技术,

------------messagecontract:控制soap协议的工具;

原文地址:https://www.cnblogs.com/guozefeng/p/3209320.html