wcf Rest 服务宿主在windows服务及客户端访问
2020-12-13 04:45
标签:des style blog http color 使用 首先写好服务,代码如下: 服务实现类: 还可以定义更多的服务,如MicroblogService、UserService等,和EmployeeService一样,代码略过。 然后让它们寄宿在windows service应用程序上,如下: 新建一个项目,名称为WindowsServiceDemo,写app.config配置文件: WindowsServiceDemo1.cs代码: Program.cs文件代码: 加入组件serviceInstaller1和serviceProcessInstaller1分别设置好服务名称、显示名称、及account等 使用命令安装服务: installutil D:\WcfRestServiceDemo1\WcfRestServiceDemo1\WindowsServiceDemo\bin\Debug\WindowsServiceDemo1.exe 确保程序和服务都是管理员身份,启动服务。代码中已加入日志,在IE浏览器中输入“http://localhost:3723/EmployeesService”,若显示不出,则服务有问题,可查看日志文件找原因。 服务启动OK后,写控制台客户端代码: 在解决方案中新增一控制台项目为ClientConsoleDynamicProject,app.config文件不用配置,直接写program.cs文件,如下代码: 执行客户端程序,OK。 需要注意的: 1.json格式中日期转换的问题,代码中已处理。若不处理,默认时使用的ISO8601的日期格式,而它需要“\/Date(1234656000000)\/”形式的日期格式,否则总报“BadRequest (400)”错误。 2.客户端调用服务中Add方法时,参数是string,在post时字符串需作处理。比如:“ss”字符串,传入时需写成“\"ss\"”,也就是说引号必须传入才是它能接受的。否则也总报“BadRequest (400)”错误。 wcf Rest 服务宿主在windows服务及客户端访问,搜素材,soscw.com wcf Rest 服务宿主在windows服务及客户端访问 标签:des style blog http color 使用 原文地址:http://www.cnblogs.com/Ericacsg/p/3848491.html 1 using System;
2 using System.Collections.Generic;
3 using System.Runtime.Serialization;
4 using System.ServiceModel.Web;
5 using System.ServiceModel;
6
7
8 namespace Artech.WcfServices.Service
9 {
10 [ServiceContract(Namespace = "Artech.WcfServices.Service")]
11 public interface IEmployees
12 {
13 [OperationContract]
14 [WebGet(UriTemplate = "all", ResponseFormat = WebMessageFormat.Json)]
15 IEnumerable
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Reflection;
6 using System.Runtime.Serialization.Json;
7 using System.Text;
8 using System.ServiceModel.Web;
9 using System.Net;
10
11 namespace Artech.WcfServices.Service
12 {
13 public class EmployeesService : IEmployees
14 {
15 private log4net.ILog logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
16 private static List
1 "1.0" encoding="utf-8" ?>
2
1 using System;
2 using System.Collections.Generic;
3 using System.Configuration;
4 using System.Reflection;
5 using System.ServiceModel;
6 using System.ServiceModel.Configuration;
7 using System.ServiceProcess;
8 using log4net;
9
10 [assembly: log4net.Config.DOMConfigurator(ConfigFile = "app.config", Watch = true)]
11 namespace WindowsServiceDemo
12 {
13 public partial class WindowsServiceDemo1 : ServiceBase
14 {
15 ///
1 using System.ServiceProcess;
2
3
4 namespace WindowsServiceDemo
5 {
6 static class Program
7 {
8 ///
1 using System;
2 using System.Text;
3 using Microsoft.Http;
4 using Newtonsoft.Json.Linq;
5 using System.Runtime.Serialization.Json;
6
7 namespace ClientConsoleDynamicProject
8 {
9 class Program
10 {
11 static dynamic GetModel(string id,string name,string department,string grade,string isHire,string entryDate)
12 {
13 dynamic model=new JObject();
14 model.Id = id;
15 model.Name = name;
16 model.Department = department ;
17 model.Grade =grade ;
18 model.IsHire = isHire;
19 model.EntryDate = ConvertDateStringToJsonDate(entryDate);
20 return model;
21 }
22 private static string ConvertDateStringToJsonDate(string dateTime)
23 {
24 string result = string.Empty;
25 DateTime dt = DateTime.Parse(dateTime);
26 dt = dt.ToUniversalTime();
27 TimeSpan ts = dt - DateTime.Parse("1970-1-1");
28 result = string.Format("/Date({0}+0800)/", ts.TotalMilliseconds);
29 return result;
30 }
31
32 static void Main(string[] args)
33 {
34 var client = new HttpClient();
35 var strUrl = "http://localhost:3723/EmployeesService/{0}";
36 strUrl = string.Format(strUrl, "2");
37 var response = client.Get(strUrl);
38 response.EnsureStatusIsSuccessful();
39 var json = response.Content.ReadAsString();
40 Console.WriteLine(json);
41
42 //create
43 string jsonModel = GetModel("5","Jimmy as 张奇","文学部","G3","true","2012/09/05 12:23:08").ToString();
44 Console.WriteLine(jsonModel );
45 var data = Encoding.UTF8.GetBytes(jsonModel);
46 strUrl = "http://localhost:3723/EmployeesService/";
47 HttpContent content = HttpContent.Create( data,"application/json");
48 response = client.Post(strUrl, content);
49 response.EnsureStatusIsSuccessful();
50
51 //get all
52 strUrl = "http://localhost:3723/EmployeesService/all";
53 response = client.Get(strUrl);
54 var xml = response.Content.ReadAsString();
55 Console.WriteLine("=============新增数据后,get all=============");
56 Console.WriteLine(xml);
57 #region update,delete
58 ////update
59 //strUrl = "http://localhost:3723/EmployeesService/";
60 //dynamic model = new JObject();
61 //model.Id = 2;
62 //model.Name = "牛牛";
63 //model.Department = "开发部";
64 //model.Grade = "G2";
65 //model.EntryDate = ConvertDateStringToJsonDate("2000/01/01");
66 //jsonModel = model.ToString();
67 //data = Encoding.UTF8.GetBytes(jsonModel);
68 //content = HttpContent.Create(data, "application/json");
69 //response = client.Put(strUrl, content);
70 //response.EnsureStatusIsSuccessful();
71
72 ////delete
73 //strUrl = "http://localhost:3723/EmployeesService/{0}";
74 //strUrl = string.Format(strUrl, "7");
75 //response = client.Delete(strUrl);
76 //response.EnsureStatusIsSuccessful();
77 #endregion
78 //add
79 //jsonModel = GetModel("6", "小猫咪", "事业部", "G8", "true", "2013/07/02 22:08:52").ToString();
80 jsonModel = "\"{\\\"Department\\\":\\\"业务部\\\",\\\"EntryDate\\\":\\\"\\/Date(1372774132000+0800)\\/\\\",\\\"Grade\\\":\\\"G5\\\",\\\"Id\\\":10,\\\"IsHire\\\":true,\\\"Name\\\":\\\"张大伟\\\"}\"";
81 Console.WriteLine(jsonModel);
82 data = Encoding.UTF8.GetBytes(jsonModel);
83 strUrl = "http://localhost:3723/EmployeesService/Add";
84 content = HttpContent.Create(data, "application/json");
85 response = client.Post(strUrl, content);
86 //response = client.Delete(strUrl+jsonModel);
87 response.EnsureStatusIsSuccessful();
88
89 //get all
90 strUrl = "http://localhost:3723/EmployeesService/all";
91 response = client.Get(strUrl);
92 json = response.Content.ReadAsString();
93 Console.WriteLine("=============get all=============");
94 Console.WriteLine(json);
95 Console.Read();
96 }
97 }
98 }
下一篇:归并排序算法思想
文章标题:wcf Rest 服务宿主在windows服务及客户端访问
文章链接:http://soscw.com/essay/29872.html