zoukankan      html  css  js  c++  java
  • 简单的WCF服务

    公用类库:

    总共一个项目:Common

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Common
     8 {
     9     public class SysInfo
    10     {
    11         public string CurrentTime { get; set; }
    12     }
    13 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Runtime.Serialization.Json;
     7 using System.IO;
     8 
     9 namespace Common
    10 {
    11     public class JsonHelper
    12     {
    13 
    14         /// <summary>  
    15         /// 将对象转化为Json字符串   
    16         /// </summary>  
    17         /// <typeparam name="T">源类型</typeparam>  
    18         /// <param name="obj">源类型实例</param>  
    19         /// <returns>Json字符串</returns>  
    20         public static string GetJsonFromObj<T>(T obj)
    21         {
    22             DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(obj.GetType());
    23             using (MemoryStream ms = new MemoryStream())
    24             {
    25                 jsonSerializer.WriteObject(ms, obj);
    26                 return Encoding.UTF8.GetString(ms.ToArray());
    27             }
    28         }
    29 
    30         /// <summary>  
    31         /// 将Json字符串转化为对象  
    32         /// </summary>  
    33         /// <typeparam name="T">目标类型</typeparam>  
    34         /// <param name="strJson">Json字符串</param>  
    35         /// <returns>目标类型的一个实例</returns>  
    36         public static T GetObjFromJson<T>(string strJson)
    37         {
    38             T obj = Activator.CreateInstance<T>();
    39             using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson)))
    40             {
    41                 DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(obj.GetType());
    42                 return (T)jsonSerializer.ReadObject(ms);
    43             }
    44         }
    45 
    46     }
    47 }

    服务端:

    总共两个项目:ServiceSet、WCFServer

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.ServiceModel;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 
     8 namespace ServiceSet
     9 {
    10     [ServiceContract(Name = "LogisticsService", Namespace = "http://localhost/")]
    11     public interface ILogistics
    12     {
    13         /// <summary>
    14         /// 查询当前时间
    15         /// </summary>
    16         [OperationContract]
    17         string GetDate(string guid);
    18     }
    19 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using Common;
     7 
     8 namespace ServiceSet
     9 {
    10     public class LogisticsService : ILogistics
    11     {
    12         public string GetDate(string guid)
    13         {
    14             SysInfo info = new SysInfo();
    15             info.CurrentTime = DateTime.Now.ToString("HH:mm:ss:fff");
    16             return JsonHelper.GetJsonFromObj<SysInfo>(info);
    17         }
    18     }
    19 }
     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <configuration>
     3   <system.serviceModel>
     4     <behaviors>
     5       <serviceBehaviors>
     6         <behavior name="metadataBehavior">
     7           <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3899/LogisticsService/metadata" />
     8         </behavior>
     9       </serviceBehaviors>
    10     </behaviors>
    11     <services>
    12       <service behaviorConfiguration="metadataBehavior" name="ServiceSet.LogisticsService">
    13         <endpoint address="http://127.0.0.1:3899/LogisticsService" binding="wsHttpBinding" bindingConfiguration="NoneSecurity" contract="ServiceSet.ILogistics" />
    14       </service>
    15     </services>
    16     <bindings>
    17       <wsHttpBinding>
    18         <binding name="NoneSecurity"
    19           maxBufferPoolSize="12000000" maxReceivedMessageSize="12000000" useDefaultWebProxy="false">
    20           <readerQuotas maxStringContentLength="12000000" maxArrayLength="12000000"/>
    21           <security mode="None"/>
    22         </binding>
    23       </wsHttpBinding>
    24     </bindings>
    25   </system.serviceModel>
    26   <startup>
    27     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    28   </startup>
    29 </configuration>
     1 using ServiceSet;
     2 using System;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.ServiceModel;
     6 using System.Text;
     7 using System.Threading;
     8 using System.Threading.Tasks;
     9 
    10 namespace WCFServer
    11 {
    12     public class Program
    13     {
    14         public static void Main(string[] args)
    15         {
    16             try
    17             {
    18                 using (ServiceHost host = new ServiceHost(typeof(LogisticsService)))
    19                 {
    20                     host.Opened += delegate
    21                     {
    22                         Console.WriteLine("{0}\t服务已经启动!", DateTime.Now.ToString("HH:mm:ss:fff"));
    23                     };
    24                     host.Open();
    25                     while (true)
    26                     {
    27                         Console.WriteLine("{0}\t程序正常运行中。", DateTime.Now.ToString("HH:mm:ss:fff"));
    28                         Thread.Sleep(10000);
    29                     }
    30                 }
    31             }
    32             catch (Exception ex)
    33             {
    34                 Console.WriteLine("{0}\t启动出错:{1}", DateTime.Now.ToString("HH:mm:ss:fff"), ex.Message);
    35             }
    36         }
    37     }
    38 }

    客户端:

    总共一个项目:WCFClient

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading;
     6 using System.Threading.Tasks;
     7 using WCFClient.LogisticsService;
     8 using Common;
     9 
    10 namespace WCFClient
    11 {
    12     class Program
    13     {
    14         static void Main(string[] args)
    15         {
    16             LogisticsServiceClient client = new LogisticsServiceClient();
    17             for (int i = 1; i < 1000; i++)
    18             {
    19                 Thread.Sleep(3000);
    20                 string guid = Guid.NewGuid().ToString();
    21                 string result = client.GetDate(guid);
    22                 SysInfo info = JsonHelper.GetObjFromJson<SysInfo>(result);
    23                 Console.WriteLine("{0}\t服务器时间:{1}", DateTime.Now.ToString("HH:mm:ss:fff"), info.CurrentTime);
    24             }
    25         }
    26     }
    27 }

    注:如果要真正实现分布式,必须把127.0.0.1改成真正的IP地址。

  • 相关阅读:
    设计模式之工厂模式(Factory Pattern)用C++实现
    读书笔记之Effective C++ 1.让自己习惯C++
    LeetCode之RemoveDuplicates扩展
    一致性哈希(Consistent Hashing)原理和实现(整理)
    LeetCode之Remove Duplicates from Sorted Array
    C++小结
    HTTP、HTTP1.0、HTTP1.1、HTTP2.0——笔记
    《趣谈网络协议》——问答笔记
    技术源于生活(头脑风暴)
    解决:未能加载文件或程序集“System.Web.Http, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
  • 原文地址:https://www.cnblogs.com/ha666/p/3066366.html
Copyright © 2011-2022 走看看