WCF实现客户端自动更新

IServiceUpdate

 1 using System.IO;
 2 using System.ServiceModel;
 3 using System.ServiceModel.Web;
 4 
 5 namespace ServiceUpdater
 6 {
 7     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IServiceUpdate”。
 8     [ServiceContract]
 9     public interface IServiceUpdate
10     {
11         [OperationContract, WebInvoke(Method = "GET", UriTemplate = "SyncTool/{fileName}")]
12         Stream SyncTool(string fileName);
13 
14         [OperationContract, WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
15         Stream DownloadFile(PostData postData);
16     }
17 
18     public class PostData
19     {
20         public string CustomerCode { get; set; }
21         public string Token { get; set; }
22         public string Mac { get; set; }
23         public string Filename { get; set; }
24     }
25 }

ServiceUpdate

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Runtime.Serialization;
 6 using System.ServiceModel;
 7 using System.ServiceModel.Web;
 8 using System.Text;
 9 using log4net;
10 
11 namespace ServiceUpdater
12 {
13     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“ServiceUpdate”。
14     public class ServiceUpdate : IServiceUpdate
15     {
16         public Stream SyncTool(string fileName)
17         {
18             string basePath = AppDomain.CurrentDomain.BaseDirectory + "Release\SyncTool\";
19             string downloadFileName = basePath + fileName;
20             if (File.Exists(downloadFileName) && WebOperationContext.Current != null)
21             {
22                 var fileExt = Path.GetExtension(downloadFileName);
23                 switch (fileExt.ToLower())
24                 {
25                     case ".js":
26                         WebOperationContext.Current.OutgoingResponse.ContentType = "text/javascript";
27                         break;
28                     case ".css":
29                         WebOperationContext.Current.OutgoingResponse.ContentType = "text/css";
30                         break;
31                     case ".html":
32                     case ".htm":
33                         WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
34                         break;
35                 }
36                 LogManager.GetLogger(this.GetType()).Info("File downloaded = " + downloadFileName);
37                 return File.OpenRead(downloadFileName);
38             }
39             return null;
40         }
41 
42         public Stream DownloadFile(PostData postData)
43         {
44             if (postData != null
45                 && !string.IsNullOrEmpty(postData.CustomerCode)
46                 && !string.IsNullOrEmpty(postData.Token)
47                 && !string.IsNullOrEmpty(postData.Mac)
48                 && !string.IsNullOrEmpty(postData.Filename))
49             {
50                 string downFilename = AppDomain.CurrentDomain.BaseDirectory + "Release\" + postData.CustomerCode + "\" + postData.Filename;
51                 if (File.Exists(downFilename))
52                 {
53                     LogManager.GetLogger(this.GetType()).Info(
54                         "File download = " + downFilename
55                         + Environment.NewLine + "CustomerCode = " + postData.CustomerCode
56                         + Environment.NewLine + "Token = " + postData.Token
57                         + Environment.NewLine + "Mac = " + postData.Mac);
58                     return File.OpenRead(downFilename);
59                 }
60             }
61             return null;
62         }
63     }
64 }
原文地址:https://www.cnblogs.com/jonney-wang/p/5724184.html