说起wcf,一直以来总是直接创建wpf的应用程序,这样默认的宿主是IIS。如果想更换宿主,那么我们首先得创建wcf类库

这个类库会自动创建一个app.config文件。到最后部署的时候,把它移到宿主的项目下。看看IService1.cs:

 using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace WcfServiceLibrary1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetDataUsingDataContract(CompositeType composite); // TODO: 在此添加您的服务操作
} // 可以将 XSD 文件添加到项目中。在生成项目后,可以通过命名空间“WcfServiceLibrary1.ContractType”直接使用其中定义的数据类型。
[DataContract]
public class CompositeType
{
[DataMember]
public string UserName{set;get;} [DataMember]
public string FullName { set; get; } [DataMember]
public string Sex { set; get; }
[DataMember]
public string Email { set; get; }
[DataMember]
public string Depart { set; get; }
/// <summary>
/// 工作性质
/// </summary>
[DataMember]
public string WorkType { set; get; }
[DataMember]
public string Phone { set; get; } [DataMember]
public string ID { set; get; } public string ToJson()
{
return JsonConvert.SerializeObject(this);
}
}
}

该接口只提供一个方法,接收一个对象,返回string,再来看它的实现:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text; namespace WcfServiceLibrary1
{
public class Service1 : IService1
{
public string GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
} return "你好:" + composite.UserName;
}
}
}

然后,我们创建宿主程序。

一、wpf寄宿

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WcfServiceLibrary1; namespace WpfInterface
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WCFInit();
} private ServiceHost Host = null;
void WCFInit()
{
if (Host == null)
{
Host = new ServiceHost(typeof(WcfServiceLibrary1.Service1)); Host.Open(); this.wcfService.Text = "wcf服务已启动";
}
} private void Window_Closed(object sender, EventArgs e)
{
if (Host != null)
{
Host.Close();
}
}
}
}
ServiceHost来自 命名空间:System.ServiceModel,WcfServiceLibrary1.Service1来自类库,所以宿主项目必须引用 System.ServiceModel和wcf类库。39-41行,能够如此简洁地实例化ServiceHost,得益于App.config:
 <?xml version="1.0" encoding="utf-8" ?>
<configuration> <appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- 部署服务库项目时,必须将配置文件的内容添加到
主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
<system.serviceModel> <services>
<service name="WcfServiceLibrary1.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8003/Service1/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
<endpoint address="" binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1">
<!--
部署时,应删除或替换下列标识元素,以反映
用来运行所部署服务的标识。删除之后,WCF 将
自动推断相应标识。
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 -->
<!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,
请在部署前将以下值设置为 false -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- 要接收故障异常详细信息以进行调试,
请将以下值设置为 true。在部署前设置为 false
以避免泄漏异常信息-->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel> </configuration>

wcf配置的三要素,简称ABC。

baseAddress:http://localhost:8003/Service1(地址)

binding:basicHttpBinding(通讯协议)

contract:WcfServiceLibrary1.IService1(契约)

针对这三要素,最复杂的和不好理解的就是bingding。binding背后隐藏了wcf的通讯机制,需要再单独研究。

二、word插件寄宿:

     private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
//创建宿主的基地址
Uri baseAddress = new Uri("http://localhost:8004/Service1"); //创建宿主
host = new ServiceHost(typeof(WcfServiceLibrary1.Service1), baseAddress); host.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), new WSHttpBinding(), ""); //将HttpGetEnabled属性设置为true
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true; //将行为添加到Behaviors中
host.Description.Behaviors.Add(smb); //打开宿主
host.Open();
Console.WriteLine("WCF中的HTTP监听已启动....");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("word插件已启动");
}
}

由于word插件中不能使用配置文件,所以WCF的ABC三要素硬编码实现。

注意:wcf宿主程序启动时,必须以管理员的权限启动,否则会报错。


												

wcf类库及宿主的更多相关文章

  1. WCF之Windows宿主

    WCF之Windows宿主(可安装成服务自动并启动) WCF之Windows宿主(可安装成服务自动并启动) 创建解决方案WCFServiceDemo 创建WCF服务库(类库或WCF服务库)WCFSer ...

  2. WCF之Windows宿主(可安装成服务自动并启动)

    WCF之Windows宿主(可安装成服务自动并启动) 创建解决方案WCFServiceDemo 创建WCF服务库(类库或WCF服务库)WCFService  ,添加引用System.ServiceMo ...

  3. WCF之Host宿主

    Self_hosting自托管宿主. 过程:手动创建Host实例,把服务端点添加到Host实例上,把服务接口与Host关联. 一个Host只能指定一个服务类型,但是可以添加多个服务端点,也可以打开多个 ...

  4. wcf自身作为宿主的一个小案例

    第一步:创建整个解决方案 service.interface:用于定义服务的契约(所有的类的接口)引用了wcf的核心程序集system.ServiceModel.dll service:用于定义服务类 ...

  5. 使用Winform程序作为WCF服务的宿主

    如果我们自己新建一个WCF服务库,生成了dll文件.那我们需要创建一个宿主程序,在本例中我们新建一个Winform程序作为WCF的宿主程序. 在网上很多教程里对创建过程写的很模糊,错误也很多.本文是作 ...

  6. [WCF] Restful 自定义宿主

    IPersonRetriever: /* * 由SharpDevelop创建. * 用户: Administrator * 日期: 2017/6/2 * 时间: 22:13 * * 要改变这种模板请点 ...

  7. WCF 基于 WinForm 宿主 发布

    ServiceHost Host = new ServiceHost(typeof(ServiceHTTP)); //绑定 System.ServiceModel.Channels.Binding h ...

  8. WCF学习笔记一

    Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windows通讯的 . ...

  9. WCF深入浅出学习1

    1.本文主要对WCF的基本使用做简单化介绍,对于初学WCF的来说,初期对于配置文件的理解,比较烦躁吧,相信你看完了该文,能够达到深入浅出的感觉. 关于WCF的概念 和 应用场景,在此处不做详细介绍,可 ...

随机推荐

  1. 图论算法-最小费用最大流模板【EK;Dinic】

    图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...

  2. c中const定义的问题

    /* 这题有个疑问: const double BASE1=BREAK1*RATE1; //第二个分界点前总共要缴的税收 const double BASE2=BASE1+(BREAK2-BREAK1 ...

  3. linux下安装jdk,tomcat以及mysql

    环境:centOS6.8.jdk1.8,tomcat-8.5.15,mysql-5.7.18 1.  安装JDK 注意:rpm与软件相关命令 相当于window下的软件助手 管理软件 步骤: 1)查看 ...

  4. maven中的传递依赖和传递依赖的解除

    例如创建三个maven工程A B C pom文件分别为 A <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns: ...

  5. the c programing language 学习过程8

    glean 捡拾落穗; glean insight 深入了解 modeled模型化 peripheral外围的 himogeneous匀称的 intents 意图  excerpt摘录 intende ...

  6. CodeForces - 796C Bank Hacking

    思路:共有n-1条边连接n个点,即形成一棵树.一开始需要选择一个点hack--将这个点视为根结点,与它相邻的点防御值加1,与它相隔一个在线点的点的防御也加1.当根节点被hack,即这个点被删除,又变成 ...

  7. hdu2846 Repository 字典树(好题)

    把每个字符串的所有子串都加入字典树,但在加入时应该注意同一个字符串的相同子串只加一次,因此可以给字典树的每个节点做个记号flag--表示最后这个前缀是属于那个字符串,如果当前加入的串与它相同,且二者属 ...

  8. 如何在模拟器里体验微软HoloLens

    众所周知,微软的HoloLens以及MR设备售价都比较高,这让不少感兴趣的朋友们望而却步,本篇教程将向大家介绍如何在模拟器里体验传说中的HoloLens. 1.需要准备的硬件: 智能手机一台(WP.A ...

  9. 深度拾遗(06) - 1X1卷积/global average pooling

    什么是1X1卷积 11的卷积就是对上一层的多个feature channels线性叠加,channel加权平均. 只不过这个组合系数恰好可以看成是一个11的卷积.这种表示的好处是,完全可以回到模型中其 ...

  10. WPF自学入门(一)WPF-XAML基本知识

    一.基本概念 1.XAML是派生自XML的可扩展应用程序标记语言(Extensible Application Markup Language)由微软创造应用在WPF,Silverlight等开发技术 ...