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();
            }
        }
  • 相关阅读:
    2020-11-07:已知一个正整数数组,两个数相加等于N并且一定存在,如何找到两个数相乘最小的两个数?
    2020-11-06:go中,谈一下调度器。
    Python2和Python3中urllib库中urlencode的使用注意事项
    python爬虫基础
    python自动抢票
    Movist for Mac 1.4.2 破解版 | Mac上好用的视频播放器有哪些?7 款本地视频播放器实测对比
    前端自动化及优化
    移动端库
    jQuery
    JavaScript
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/10027055.html
Copyright © 2011-2022 走看看