wcf类库及宿主
说起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类库及宿主的更多相关文章
- WCF之Windows宿主
WCF之Windows宿主(可安装成服务自动并启动) WCF之Windows宿主(可安装成服务自动并启动) 创建解决方案WCFServiceDemo 创建WCF服务库(类库或WCF服务库)WCFSer ...
- WCF之Windows宿主(可安装成服务自动并启动)
WCF之Windows宿主(可安装成服务自动并启动) 创建解决方案WCFServiceDemo 创建WCF服务库(类库或WCF服务库)WCFService ,添加引用System.ServiceMo ...
- WCF之Host宿主
Self_hosting自托管宿主. 过程:手动创建Host实例,把服务端点添加到Host实例上,把服务接口与Host关联. 一个Host只能指定一个服务类型,但是可以添加多个服务端点,也可以打开多个 ...
- wcf自身作为宿主的一个小案例
第一步:创建整个解决方案 service.interface:用于定义服务的契约(所有的类的接口)引用了wcf的核心程序集system.ServiceModel.dll service:用于定义服务类 ...
- 使用Winform程序作为WCF服务的宿主
如果我们自己新建一个WCF服务库,生成了dll文件.那我们需要创建一个宿主程序,在本例中我们新建一个Winform程序作为WCF的宿主程序. 在网上很多教程里对创建过程写的很模糊,错误也很多.本文是作 ...
- [WCF] Restful 自定义宿主
IPersonRetriever: /* * 由SharpDevelop创建. * 用户: Administrator * 日期: 2017/6/2 * 时间: 22:13 * * 要改变这种模板请点 ...
- WCF 基于 WinForm 宿主 发布
ServiceHost Host = new ServiceHost(typeof(ServiceHTTP)); //绑定 System.ServiceModel.Channels.Binding h ...
- WCF学习笔记一
Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windows通讯的 . ...
- WCF深入浅出学习1
1.本文主要对WCF的基本使用做简单化介绍,对于初学WCF的来说,初期对于配置文件的理解,比较烦躁吧,相信你看完了该文,能够达到深入浅出的感觉. 关于WCF的概念 和 应用场景,在此处不做详细介绍,可 ...
随机推荐
- 4、flask之分页插件的使用、添加后保留原url搜索条件、单例模式
本篇导航: flask实现分页 添加后保留原url搜索条件 单例模式 一.flask实现分页 1.django项目中写过的分页组件 from urllib.parse import urlencode ...
- 自制PHP高防防盗链(不是一般的高)(思路)
原理:根据IP,资源ID,时间戳,一次性Access_Token,APPKEY(暴露在前台)和APPSERECT(后台)来生成参数,具体见下面: 浏览器请求页面=>后台引用防盗链代码=>生 ...
- Ansible playbook循环实践总结<一>
1.标准Loops 标准loops可以直接减少task的次数,如下: [root@zero01 playbook]# vi loops.yaml --- - hosts: all gather_fac ...
- Java基础系列--final关键字
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/8482909.html 一.概述 final是Java关键字中最常见之一,表示"最 ...
- 变态的IE
1.IE7及更早版本, unshift()方法总是返回undefined而不是数组的新长度.2.IE8及之前版本, 在catch语句中捕获的错误对象会被添加到执行环境的变量对象, 而不是catch语句 ...
- 重绘(redraw或repaint),重排(reflow)
浏览器运行机制图: 浏览器的运行机制:layout:布局: 1.构建DOM树(parse):渲染引擎解析HTML文档,首先将标签转换成DOM树中的DOM node(包括js生成的标签)生成内容树(Co ...
- python实现三级菜单
一.要求: 1.一开始打印出所有省份和提示 2.用户输入省份以此查询城市 3.在按照输出的城市名提示用户输入,最后输出用户所查询的区县名 4.随时输入"back"可以返回上一级菜单 ...
- 阿里巴巴分布式服务框架 Dubbo 介绍
Dubbo是阿里巴巴内部的SOA服务化治理方案的核心框架,每天为2000+ 个服务提供3,000,000,000+ 次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点.Dubbo自2011年开源后, ...
- MySQL架构篇(一)
MySQL复制解决了什么问题? 1.实现在不同服务器上的数据分布 2.利用二进制日志增量进行 3.不需要太多的带宽 4.但是使用基于行的复制在进行大批量的更改时会对带宽带来一定的压力,特别是跨IDC环 ...
- 快速排序(QuickSort)
1.算法思想 快速排序是一种划分交换排序.它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod). (1) 分治法的基本思想 分治法的基本思想是:将原 ...