zoukankan      html  css  js  c++  java
  • (5)数据契约

     wcf传递类和结构

    一、使用 [Serializable] 特性 序列化 

    服务端

     IService1.cs

        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
             Animal GetData(Animal value);
        }

     Service1.cs

        public class Service1 : IService1
        {
            public Animal GetData()
            {
                Animal A = new Animal();
                A.name = "cat";
                return A;
            }
        }

     公用的类库

    Animal.cs

        [Serializable]
        class Animal
        {
            string name;
        }

    Program.cs

        class Program
        {
            static void Main(string[] args)
            {
                Uri httpBaseAddress = new Uri("http://localhost:9001/");
                Uri tcpBaseAddress = new Uri("net.tcp://localhost:9002/");
    
                //ServiceHost host = new ServiceHost(typeof(Service1), httpBaseAddress, tcpBaseAddress);
                ServiceHost host = new ServiceHost(typeof(Service1), httpBaseAddress, tcpBaseAddress);
                ////启动行为
                ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                {
                    smb = new ServiceMetadataBehavior();
                }
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);
                host.Open();
                Console.WriteLine("Service已经启动,按任意键终止服务!");
                Console.Read();
                host.Close();
            }
        }

    客户端

    proxy.cs

    使用工具生成代理类

    Animal.cs

    和服务端共享

    App.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IService1" />
          </basicHttpBinding>
          <netTcpBinding>
            <binding name="NetTcpBinding_IService1" />
          </netTcpBinding>
        </bindings>
        <client>
          <endpoint address="http://localhost:9001/" binding="basicHttpBinding"
              bindingConfiguration="BasicHttpBinding_IService1" contract="IService1"
              name="BasicHttpBinding_IService1" />
          <endpoint address="net.tcp://localhost:9002/" binding="netTcpBinding"
              bindingConfiguration="NetTcpBinding_IService1" contract="IService1"
              name="NetTcpBinding_IService1">
            <identity>
              <userPrincipalName value="XTZ-01805141702Administrator" />
            </identity>
          </endpoint>
        </client>
      </system.serviceModel>
    </configuration>

    Program.cs

        class Program
        {
            static void Main(string[] args)
            {      
                Service1Client proxy = new Service1Client("BasicHttpBinding_IService1");//传入客户端终结点名称
                Animal b = proxy.GetData();
                Console.Write(b.name);
                Console.ReadKey();
            }
        }
  • 相关阅读:
    Java语言
    包名规范
    带参数的方法
    成员变量和局部变量
    Java数据类型
    java反射机制
    JDK安装
    注释
    二维数组
    数组的经典排序
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/10027055.html
Copyright © 2011-2022 走看看