[WCF Step by Step 读书笔记] Chapter02 寄宿一个WCF服务
2011-02-24 10:52
369 查看
如何使用WPF寄宿一个WCF的Service?
1. 首先新建一个WCF的Class Library,包括IProductService.cs和ProductService.cs两个文件,内容与上一章相同。同时要有一个与上一章相同的数据库访问的project
2. 新建一个WPF的工程,其后台CS文件内容为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.ServiceModel;
using Products;
namespace ProductsServiceHost
{
/// <summary>
/// Interaction logic for HostController.xaml
/// </summary>
public partial class HostController : Window
{
private ServiceHost productsServiceHost;
public HostController()
{
InitializeComponent();
}
private void handleException(Exception ex)
{
MessageBox.Show(ex.Message, "Exception",
MessageBoxButton.OK, MessageBoxImage.Error);
}
private void start_Click(object sender, RoutedEventArgs e)
{
try
{
productsServiceHost = new ServiceHost(typeof(ProductsServiceImpl));
productsServiceHost.Open();
stop.IsEnabled = true;
start.IsEnabled = false;
status.Text = "Service Running";
}
catch (Exception ex)
{
handleException(ex);
}
}
private void stop_Click(object sender, EventArgs e)
{
try
{
productsServiceHost.Close();
stop.IsEnabled = false;
start.IsEnabled = true;
status.Text = "Service Stopped";
}
catch (Exception ex)
{
handleException(ex);
}
}
}
}
这里工作的原理是:在类内部有一个ServiceHost类型的member,每次揿开始按钮,会为其实例化一个ServiceHost,而揿关闭按钮时会执行Close方法。
注意这里实例化的时候传入的参数是typeof(ProductsServiceImpl),这里typeof将返回一个Type类型的对象,这样ServiceHost的构造函数就可以利用它获得ProductsServiceImpl的各种member和method,具体可参考:
http://msdn.microsoft.com/zh-cn/library/58918ffs(v=vs.80).aspx
3. 在书中的ProductsClient里并没有提到添加Service Reference,实际上也不可以添加,原因在于书中并没有提供该Service的元数据发布的Endpoint。实际上可以通过如下方法进行配置:
1)在ProductsServiceHost这个project里的App.config,通过configuration wizard进行配置的时候,首先在Services=>Host这个节点下添加BaseAddress为net.tcp://localhost:8080/TcpService
2)在Endpoints里添加一个发布给Service的Endpoint,其Binding为mexTcpBinding,发布用的Contract是ProductsServices.IProductService。这里地址栏使用空白,表明其Service的发布地址继承为net.tcp://localhost:8080/TcpService
3)在Endpoints里添加一个所提供的Service的Metadata的Endpoint,Binding为mexTcpBinding,发布用的Contract是IMetadataExchange,地址狼为Mex,表明其Service发布的地址为net.tcp://localhost:8080/TcpService
在经历过以上几步之后,启动Host,启动服务,就可以在Client端添加Add Service Reference了。如果缺少了发布Metadata的第三步,那么则不能添加Service Reference,但是在添加完成之后,将第三步设置的发布metadata的endpoint删除,并不会影响client端的工作。
1. 首先新建一个WCF的Class Library,包括IProductService.cs和ProductService.cs两个文件,内容与上一章相同。同时要有一个与上一章相同的数据库访问的project
2. 新建一个WPF的工程,其后台CS文件内容为:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.ServiceModel;
using Products;
namespace ProductsServiceHost
{
/// <summary>
/// Interaction logic for HostController.xaml
/// </summary>
public partial class HostController : Window
{
private ServiceHost productsServiceHost;
public HostController()
{
InitializeComponent();
}
private void handleException(Exception ex)
{
MessageBox.Show(ex.Message, "Exception",
MessageBoxButton.OK, MessageBoxImage.Error);
}
private void start_Click(object sender, RoutedEventArgs e)
{
try
{
productsServiceHost = new ServiceHost(typeof(ProductsServiceImpl));
productsServiceHost.Open();
stop.IsEnabled = true;
start.IsEnabled = false;
status.Text = "Service Running";
}
catch (Exception ex)
{
handleException(ex);
}
}
private void stop_Click(object sender, EventArgs e)
{
try
{
productsServiceHost.Close();
stop.IsEnabled = false;
start.IsEnabled = true;
status.Text = "Service Stopped";
}
catch (Exception ex)
{
handleException(ex);
}
}
}
}
这里工作的原理是:在类内部有一个ServiceHost类型的member,每次揿开始按钮,会为其实例化一个ServiceHost,而揿关闭按钮时会执行Close方法。
注意这里实例化的时候传入的参数是typeof(ProductsServiceImpl),这里typeof将返回一个Type类型的对象,这样ServiceHost的构造函数就可以利用它获得ProductsServiceImpl的各种member和method,具体可参考:
http://msdn.microsoft.com/zh-cn/library/58918ffs(v=vs.80).aspx
3. 在书中的ProductsClient里并没有提到添加Service Reference,实际上也不可以添加,原因在于书中并没有提供该Service的元数据发布的Endpoint。实际上可以通过如下方法进行配置:
1)在ProductsServiceHost这个project里的App.config,通过configuration wizard进行配置的时候,首先在Services=>Host这个节点下添加BaseAddress为net.tcp://localhost:8080/TcpService
2)在Endpoints里添加一个发布给Service的Endpoint,其Binding为mexTcpBinding,发布用的Contract是ProductsServices.IProductService。这里地址栏使用空白,表明其Service的发布地址继承为net.tcp://localhost:8080/TcpService
3)在Endpoints里添加一个所提供的Service的Metadata的Endpoint,Binding为mexTcpBinding,发布用的Contract是IMetadataExchange,地址狼为Mex,表明其Service发布的地址为net.tcp://localhost:8080/TcpService
在经历过以上几步之后,启动Host,启动服务,就可以在Client端添加Add Service Reference了。如果缺少了发布Metadata的第三步,那么则不能添加Service Reference,但是在添加完成之后,将第三步设置的发布metadata的endpoint删除,并不会影响client端的工作。
相关文章推荐
- [WCF Step by Step 读书笔记] Chapter03 构造更强健的应用和服务
- WCF 寄宿于Windows服务 ,可能会报的错:在系统启动时至少有一个服务或驱动程序产生错误。详细信息,请使用事件查看器查看事件日志。
- 重温WCF之构建一个简单的WCF(一)(1)通过控制台和IIS寄宿服务
- WCF 第七章 寄宿 在一个IIS寄宿服务中开启ASMX特性
- WCF 第七章 寄宿 在一个进程中寄宿多个服务
- 重温WCF之构建一个简单的WCF(一)(2)通过Windows Service寄宿服务和WCF中实现操作重载
- [ASP.NET 设计模式] 用Visual Studio2010搭建一个简单的分层结构示例Step by Step —— 03 服务层
- WCF 第七章 寄宿 在一个IIS寄宿服务中开启ASMX特性
- WCF 第七章 寄宿 在一个进程中寄宿多个服务
- [WCF Step by Step 读书笔记] Chapter01 WCF 介绍
- Visual.C#.2010从入门到精通].(Visual.C#.2010.Step.By.Step)读书笔记 ADO.NET(十九)
- WCF 第一章 基础 在IIS中寄宿服务
- 在Windows Azure中实现和调试一个WCF服务(下)
- WCF服务寄宿IIS与Windows服务
- WCF自定义行为 Step by Step (三)behavior模型
- MDX Step by Step 读书笔记(八) - Navigating Hierarchies 层次结构导航
- Silverlight与WCF之间的通信(4)silverlight以net.tcp方式调用console上寄宿的wcf服务
- WCF 第五章 行为 为服务终结点行为实现一个消息检测器
- 寄宿于CS程序的WCF服务
- WCF 第十一章 工作流服务 从WF暴露一个服务(中)