【WCF】VSTO Host WCF Web Api

最近突发奇想,在PPT运行发布一个WebApi,这样通过手机或者浏览器就能控制此PPT播放。于是尝试了下用 WCF 里的 WebServiceHost 寄宿在VSTO的对象上。
因此有了下面这个类:
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, 很简单吧。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值