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的概念 和 应用场景,在此处不做详细介绍,可 ...
随机推荐
- 洛谷P2756飞行员配对方案问题 P2055假期的宿舍【二分图匹配】题解+代码
洛谷 P2756飞行员配对方案问题 P2055假期的宿舍[二分图匹配] 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架 ...
- 我的2017年终总结(PF项目框架设计心得分享 1.0rc new)
一晃眼又过去了一年,在这一年里尽管有许多不如意的事,却阻挡不了我前进的脚步.先用一句话来总结去年一年的状态,那就是“无休无止的忙碌”.而这样的忙碌状态对我来说是不可取的,因为匮乏的忙碌只能让头脑处于一 ...
- 【考试】java基础知识测试,看你能得多少分?
1 前言 共有5道java基础知识的单项选择题,每道20分,共计100分.解析和答案在最后. 2 试题 2.1 如下程序运行结果是什么? class Parent { public Parent(St ...
- mysql数据库外部无法访问
有以下两种情况: 1.mysql未分配访问权限 格式:grant 权限 on 数据库名.表名 用户@登录主机 identified by "用户密码"; grant select, ...
- 关于 JS 拖拽功能的冲突问题及解决方法
前言 我在之前写过关于 JS 拖拽的文章,实现方式和网上能搜到的方法大致相同,别无二致,但是在一次偶然的测试中发现,这种绑定事件的方式可能会和其它的拖拽事件产生冲突,由此产生了对于事件绑定的思考.本文 ...
- 如何通过以太坊智能合约来进行众筹(ICO)
前面我们有两遍文章写了如何发行代币,今天我们讲一下如何使用代币来公开募资,即编写一个募资合约. 写在前面 本文所讲的代币是使用以太坊智能合约创建,阅读本文前,你应该对以太坊.智能合约有所了解,如果你还 ...
- 第二十章 Django数据库实战
第二十章 Django数据库实战 第一课 获取单表单数据的三种方式: urls.py中的路由代码: path('busniess',views.busniess), views.py中代码: def ...
- nyoj888 取石子(九) 反Nimm博弈
这题就是反Nimm博弈--分析见反Nimm博弈 AC代码 #include <cstdio> #include <cmath> #include <algorithm&g ...
- nyoj1204 魔法少女 线性DP
d[i][0]表示到达第i层,且在第i层没有使用魔法的最少时间 d[i][1]表示到达第i层,且在第i层使用魔法通过一层 d[i][2]表示到达第i层,且在第i层使用魔法通过两层 状态转移方程: d[ ...
- C# 之三类文件的读写( .XML,.INI 和 .TXT 文件)
笔记之用,关于三类.xml, .ini, .txt 文件的 C# 读写,请多多指教! 1,第一类:.xml 文件的读写 先贴上xml文件,下面对这个文件进行操作: <?xml version=& ...