WCF学习之旅—WCF服务部署到应用程序(十)
上接 WCF学习之旅—WCF寄宿前的准备(八)
WCF学习之旅—WCF服务部署到IIS7.5(九)
五、控制台应用程序宿主
(1) 在解决方案下新建控制台输出项目 ConsoleHosting。如下图。

(2)添加 System.ServiceModel.dll 的引用。
(3)添加 WCF 服务类库(WcfServiceLib)的项目引用。
(4)创建宿主程序,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using WcfServiceLib; namespace ConsoleHosting
{
class Program
{
static void Main(string[] args)
{
//创建宿主的基地址
Uri baseAddress = new Uri("http://localhost:8080/BookService");
//创建宿主
using (ServiceHost host = new ServiceHost(typeof(BookService), baseAddress))
{
//向宿主中添加终结点
host.AddServiceEndpoint(typeof(IBookService), new WSHttpBinding(), baseAddress);
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
{
//将HttpGetEnabled属性设置为true
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;
behavior.HttpGetUrl = baseAddress;
//将行为添加到Behaviors中
host.Description.Behaviors.Add(behavior);
//打开宿主
host.Opened += delegate
{
Console.WriteLine("BookService控制台程序寄宿已经启动,HTTP监听已启动....,按任意键终止服务!"); }; host.Open();
Console.Read();
}
}
}
} }
(5)运行宿主程序,在运行客户端进行调用之前,需要先运行宿主程序。如下图所示,则说明宿主建立成功。

建立客户端
(1) 重新建立解决方案-->Windows窗体应用程序项目。如下图。

(2)添加对服务的引用(在引用上右键-->输入我们定义的服务宿主的基地址(此处为:http://localhost:8080/BookService)-->转到-->确定),具体请看第一节。如下图1,图2。

图1

图2
(2) 在From1中添加一个文本框与一个按钮。如下图。

(3) 在From1.cs中写如下代码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WinClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnGetBook_Click(object sender, EventArgs e)
{
BookServiceReference.BookServiceClient client = new BookServiceReference.BookServiceClient();
string book = client.GetBook("");
textBox1.Text = book; } } }
(4) 测试程序如下图所示说明成功(注意:一定要先运行我们的宿主程序才行,如果宿主没有打开的话会报错:由于目标计算机积极拒绝,无法连接。)。如下图。

在这个示例中我们把Endpoint中的ABC,基地址,Behaviors等都直接写在了代码里,但实际应用过程中都是去依赖配置文件,为了对比说明我们下面的例子中会使用配置文件。
六、Windows应用程序宿主
(1) 在解决方案下新建Windows窗体应用程序项目 WinHosting。如下图。

(2)添加 System.ServiceModel.dll 的引用。
(3)添加 WCF 服务类库(WcfServiceLib)的项目引用。
(4)添加应用程序配置文件App.config,这次我们使用配置的方式进行WCF服务的公布,WCF服务配置代码如下。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:8080/BookService/metadata" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="metadataBehavior" name="WcfServiceLib.BookService">
<endpoint address="http://127.0.0.1:8080/BookService" binding="wsHttpBinding"
contract="WcfServiceLib.IBookService" />
</service>
</services>
</system.serviceModel>
</configuration>
(5)创建宿主程序Form1窗体,并修改App.config,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WcfServiceLib; namespace WinHosting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} ServiceHost host = null;
private void Form1_Load(object sender, EventArgs e)
{
try
{
host = new ServiceHost(typeof(BookService)); host.Opened += delegate
{
label1.Text="窗体宿主程序,BookService,使用配置文件!"; };
host.Open(); }
catch (Exception ex)
{
label1.Text=ex.Message;
}
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
host.Close(); } } }
(6) 运行程序如下图所示。

建立客户端
使用我们在Console寄宿程序编写的客户端,去访问Windows窗体宿主程序的WCF服务。
WCF学习之旅—WCF服务部署到应用程序(十)的更多相关文章
- WCF学习之旅—WCF服务部署到IIS7.5(九)
上接 WCF学习之旅—WCF寄宿前的准备(八) 四.WCF服务部署到IIS7.5 我们把WCF寄宿在IIS之上,在IIS中宿主一个服务的主要优点是在发生客户端请求时宿主进程会被自动启动,并且你可以 ...
- WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...
- WCF学习之旅—WCF服务的WAS寄宿(十二)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) 八.WAS宿主 IIS ...
- WCF学习之旅—WCF服务的批量寄宿(十三)
上接 WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) WCF学习之旅—WCF ...
- WCF学习之旅—基于Fault Contract 的异常处理(十八)
WCF学习之旅—WCF中传统的异常处理(十六) WCF学习之旅—基于ServiceDebug的异常处理(十七) 三.基于Fault Contract 的异常处理 第二个示例是通过定制Servic ...
- WCF学习之旅—WCF第二个示例(五)
二.WCF服务端应用程序 第一步,创建WCF服务应用程序项目 打开Visual Studio 2015,在菜单上点击文件—>新建—>项目—>WCF服务应用程序.在弹出界面的“名称”对 ...
- WCF学习之旅—WCF第二个示例(七)
三.创建客户端应用程序 若要创建客户端应用程序,你将另外添加一个项目,添加对该项目的服务引用,配置数据源,并创建一个用户界面以显示服务中的数据. 在第一个步骤中,你将 Windows 窗体项目添加到解 ...
- WCF学习之旅—WCF第二个示例(六)
第五步,创建数据服务 在“解决方案资源管理器”中,使用鼠标左键选中“SCF.WcfService”项目,然后在菜单栏上,依次选择“项目”.“添加新项”. 在“添加新项”对话框中,选择“Web”节点,然 ...
- WCF学习之旅—WCF寄宿前的准备(八)
一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类协定的定 ...
随机推荐
- Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数
上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...
- Android UI体验之全屏沉浸式透明状态栏效果
前言: Android 4.4之后谷歌提供了沉浸式全屏体验, 在沉浸式全屏模式下, 状态栏. 虚拟按键动态隐藏, 应用可以使用完整的屏幕空间, 按照 Google 的说法, 给用户一种 身临其境 的体 ...
- Socket聊天程序——初始设计
写在前面: 可能是临近期末了,各种课程设计接踵而来,最近在csdn上看到2个一样问答(问题A,问题B),那就是编写一个基于socket的聊天程序,正好最近刚用socket做了一些事,出于兴趣,自己抽了 ...
- Android之常见问题集锦Ⅱ
Android问题集锦Ⅰ:http://www.cnblogs.com/AndroidJotting/p/4608025.html EditText输入内容改变事件监听 _edit.addTextCh ...
- shiro权限管理框架与springmvc整合
shiro是apache下的一个项目,和spring security类似,用于用户权限的管理‘ 但从易用性和学习成本上考虑,shiro更具优势,同时shiro支持和很多接口集成 用户及权限管理是众多 ...
- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform
eclipse maven clean install 报错 1. 修改properties-->resource-->utf-8仍然报错 2.修改项目pom.xml文件,增加: < ...
- vue入门学习(基础篇)
vue入门学习总结: vue的一个组件包括三部分:template.style.script. vue的数据在data中定义使用. 数据渲染指令:v-text.v-html.{{}}. 隐藏未编译的标 ...
- 面向对象相关知识点xmind
- JavaScript学习笔记(二)——闭包、IIFE、apply、函数与对象
一.闭包(Closure) 1.1.闭包相关的问题 请在页面中放10个div,每个div中放入字母a-j,当点击每一个div时显示索引号,如第1个div显示0,第10个显示9:方法:找到所有的div, ...
- 【原】无脑操作:express + MySQL 实现CRUD
基于node.js的web开发框架express简单方便,很多项目中都在使用.这里结合MySQL数据库,实现最简单的CRUD操作. 开发环境: IDE:WebStorm DB:MySQL ------ ...