zoukankan      html  css  js  c++  java
  • WCF(客户端与服务)

     

    项目一:类库项目。定义服务,code实现服务接口的类。[如上图的Service]

    namespace CodesContract

    {

        [ServiceContract(Name = "CodeService", Namespace = "http://www.rxm.net")]

        public interface ICodeContract

        {

            [OperationContract]

            string GetName(string name);

        }

     

        public class CodeContract : ICodeContract

        {

     

            #region ICodeContract 成员

     

            public string GetName(string name)

            {

                return "My name is " + name;

            }

     

            #endregion

        }

    }

    项目二:控制台项目。服务寄宿的宿主,公布终结点。[如上图ServideHost,ABC]

    namespace CodesHost

    {

        class Program

        {

            static void Main(string[] args)

            {

                using (ServiceHost host = new ServiceHost(typeof(CodesContract.CodeContract)))

                {

                    host.AddServiceEndpoint(typeof(CodesContract.ICodeContract), new NetTcpBinding(), "net.Tcp://localhost:806/CodesName");

                    host.Open();

                    Console.Read();

                }

            }

        }

    }

    项目三:控制台项目。定义与服务端相同的服务,终结点地址要与服务终结点地址和通信协议相同。[如上图左边]

    namespace CodesClient

    {

        [ServiceContract(Name = "CodeService", Namespace = "http://www.rxm.net")]

        public interface ICodeContract

        {

            [OperationContract]

            string GetName(string name);

        }

     

        class Program

        {

            static void Main(string[] args)

            {

                ICodeContract proxy = ChannelFactory<ICodeContract>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.Tcp://localhost:806/CodesName"));

                string name = proxy.GetName("rxm");

                Console.WriteLine(name);

                Console.ReadKey();

            }

        }

    }

  • 相关阅读:
    学习日记-- 动态性。动态编译,静态方法,包装类
    第一周学习所获———ORM,数据持久化,通过注解来实现数据持久化框架
    第一周学习所获--class类1(反射和类加载过程)
    各种命名规范
    用easyui,json,纯mvc实现一个系统的数据流动过程
    js+bootstrap实现分页页码
    Echarts简单案例
    bootstrap日期控件(双日期、清空等问题解决)
    三种方法实现调用Restful接口
    Spring MVC异常处理 和 重定向传递数据
  • 原文地址:https://www.cnblogs.com/hometown/p/2829584.html
Copyright © 2011-2022 走看看