IServiceUpdate
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web; namespace ServiceUpdater
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IServiceUpdate”。
[ServiceContract]
public interface IServiceUpdate
{
[OperationContract, WebInvoke(Method = "GET", UriTemplate = "SyncTool/{fileName}")]
Stream SyncTool(string fileName); [OperationContract, WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
Stream DownloadFile(PostData postData);
} public class PostData
{
public string CustomerCode { get; set; }
public string Token { get; set; }
public string Mac { get; set; }
public string Filename { get; set; }
}
}
ServiceUpdate
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using log4net; namespace ServiceUpdater
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“ServiceUpdate”。
public class ServiceUpdate : IServiceUpdate
{
public Stream SyncTool(string fileName)
{
string basePath = AppDomain.CurrentDomain.BaseDirectory + "Release\\SyncTool\\";
string downloadFileName = basePath + fileName;
if (File.Exists(downloadFileName) && WebOperationContext.Current != null)
{
var fileExt = Path.GetExtension(downloadFileName);
switch (fileExt.ToLower())
{
case ".js":
WebOperationContext.Current.OutgoingResponse.ContentType = "text/javascript";
break;
case ".css":
WebOperationContext.Current.OutgoingResponse.ContentType = "text/css";
break;
case ".html":
case ".htm":
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
break;
}
LogManager.GetLogger(this.GetType()).Info("File downloaded = " + downloadFileName);
return File.OpenRead(downloadFileName);
}
return null;
} public Stream DownloadFile(PostData postData)
{
if (postData != null
&& !string.IsNullOrEmpty(postData.CustomerCode)
&& !string.IsNullOrEmpty(postData.Token)
&& !string.IsNullOrEmpty(postData.Mac)
&& !string.IsNullOrEmpty(postData.Filename))
{
string downFilename = AppDomain.CurrentDomain.BaseDirectory + "Release\\" + postData.CustomerCode + "\\" + postData.Filename;
if (File.Exists(downFilename))
{
LogManager.GetLogger(this.GetType()).Info(
"File download = " + downFilename
+ Environment.NewLine + "CustomerCode = " + postData.CustomerCode
+ Environment.NewLine + "Token = " + postData.Token
+ Environment.NewLine + "Mac = " + postData.Mac);
return File.OpenRead(downFilename);
}
}
return null;
}
}
}