最近突发奇想,在PPT运行发布一个WebApi,这样通过手机或者浏览器就能控制此PPT播放。于是尝试了下用 WCF 里的 WebServiceHost 寄宿在VSTO的对象上。
因此有了下面这个类:
SlideShowServiceHost.cs
ThisAddIn.cs
因此有了下面这个类:
SlideShowServiceHost.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.Net;
namespace SlideShow
{
public class SlideShowServiceHost
{
private static WebServiceHost _host;
public static void Open()
{
var ipAddressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
var ipFirst = ipAddressList.First(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString();
_host = new WebServiceHost(typeof(SlideShowService), new Uri(string.Format("http://{0}:81", ipFirst)));
_host.Open();
}
public static void Close()
{
if (_host != null)
{
_host.Close();
((IDisposable)_host).Dispose();
}
}
}
}
这个 SlideShowServiceHost 在VSTO启动Application时加载,运行PPT时Open:
ThisAddIn.cs
void Application_SlideShowBegin(PowerPoint.SlideShowWindow Wn)
{
var ret = MessageBox.Show("Begin SlideShowServer?", "SlideShow", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (ret != DialogResult.OK)
return;
SlideShowServiceHost.Open();
...
}
void Application_SlideShowEnd(PowerPoint.Presentation Pres)
{
MessageBox.Show("SlideShow Server ShutDown.");
SlideShowServiceHost.Close();
}
OK, 很简单吧。