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();

            }

        }

    }

  • 相关阅读:
    nacos 命名空间
    Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences
    gitee
    maven引入junit 4.12,项目import org.junit.Test 还是报错.
    gitflow
    202011
    idea 忽略显示不需要的文件
    服务熔断 & 降级区别
    各种微服务框架对比
    zookeeper not connected
  • 原文地址:https://www.cnblogs.com/hometown/p/2829584.html
Copyright © 2011-2022 走看看