WCF小实例以及三种宿主
WCF小实例以及三种宿主
最近一直在学习WCF相关知识,下面将通过一个小实例对所学的知识进行简单的回顾;本实例是一个简单三层操作数据库,并且也简单实现的三种宿主(控制台宿主,IIS宿主以及Windows服务宿主)的练习;还包含一个双工的功能,下图为程序所创建分层结构图;

首先了解为这个实例所创建的两张简单表;
![]() |
![]() USE [TestDb] ![]() ![]() USE [TestDb] ![]() |
接下来简单了解每一层的作用以及一些注意事项;源代码后面直接提供下载;
1:Service.DAL 是针对数据库操作的一些类;
2:Service.DbHelp 为数据库操作提供助手
3:Service.HostTest 为服务提供自宿主的测试;一般在编码时会用到;此层还用到批量寄存的知识;
4:Service.Interface 为契约层,定义的相关开放接口;
5:Service.Model为实体层也是数据契约层;
6:Service.ServerDemo 为实现契约的服务内容;若有逻辑都放在这一层;
7:WcfServiceForIIs 为宿主在IIS内容;
8:WebClient 为客户端为Web形式
9:Windows_HostService为宿主要Windows服务里的内容;
10:WindowsClient、WindowsFormsForHostService、WindowsFormsForIIS分别是对三种寄存宿主进行测试的客户端;
一:注意点批量寄存宿主;把重点的一些配置一及实现代码贴出;

<configuration>
<configSections>
<section name="artech.batchingHosting"
type="Service.HostTest.Configuration.BatchingHostingSettings, Service.HostTest"/>
</configSections>
<appSettings>
<add key="ConnectionString" value="server=.;database=TestDb;uid=sa;pwd=admin"/>
</appSettings>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="portSharingBinding" portSharingEnabled="true"></binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="UsermetadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3721/UserService/metadata"/>
</behavior>
<behavior name="AccountmetadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3721/AccountService/metadata"/>
</behavior>
<behavior name="ExcptDivedeBehavior">
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Service.ServerDeom.UserService" behaviorConfiguration="UsermetadataBehavior">
<endpoint address="http://127.0.0.1:3721/UserService"
binding="wsHttpBinding"
contract="Service.Interface.IUser"/>
</service>
<service name="Service.ServerDeom.AccountService" behaviorConfiguration="AccountmetadataBehavior">
<endpoint address="http://127.0.0.1:3721/AccountService"
binding="wsHttpBinding"
contract="Service.Interface.IAccount"/>
</service>
<service name="Service.ServerDeom.DuplexTestService">
<endpoint address="net.tcp://127.0.0.1:3722/DuplexTestService"
binding="netTcpBinding"
contract="Service.Interface.IDuplexTest" bindingConfiguration="portSharingBinding"/>
</service>
<service name="Service.ServerDeom.ExcptDivideService" behaviorConfiguration="ExcptDivedeBehavior">
<endpoint address="http://127.0.0.1:3723/ExcptDivideService"
binding="wsHttpBinding"
contract="Service.Interface.IExcptDivide"/>
</service>
</services>
</system.serviceModel>
<artech.batchingHosting>
<add type="Service.ServerDeom.UserService, Service.ServerDeom"/>
<add type="Service.ServerDeom.AccountService, Service.ServerDeom"/>
<add type="Service.ServerDeom.DuplexTestService, Service.ServerDeom"/>
<add type="Service.ServerDeom.ExcptDivideService, Service.ServerDeom"/>
</artech.batchingHosting>
</configuration>


using System.ServiceModel;
using Service.ServerDeom;
using Service.Interface;
namespace Service.HostTest
{
class Program
{
static void Main(string[] args)
{
using (ServiceHostCollection hosts = new ServiceHostCollection())
{
foreach (ServiceHost host in hosts)
{
host.Opened += (sender, arg) => Console.WriteLine("服务{0}开始监听", (sender as ServiceHost).Description.ServiceType);
}
hosts.Open();
Console.Read();
}
}
}
}

二:宿主要IIS里的注意内容;
新建一个文本文件然后把它的后缀修改成.svc;并在其头部增加一行调用服务的代码;
<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServerDeom.UserService" %>
其配置如下:

<configuration>
<appSettings>
<add key="ConnectionString" value="server=.;database=TestDb;uid=sa;pwd=admin"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="Service.ServerDeom.UserService">
<endpoint binding="ws2007HttpBinding"
contract="Service.Interface.IUser"/>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer> </configuration>

把I宿主部署到IIS里;

三:宿主在Windows服务;创建一个Windows服务类库,增加相应的配置以及代码;


<configuration>
<appSettings>
<add key="ConnectionString" value="server=.;database=TestDb;uid=sa;pwd=admin"/>
</appSettings>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="portSharingBinding" portSharingEnabled="true"></binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="UsermetadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3718/UserService/metadata"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Service.ServerDeom.UserService" behaviorConfiguration="UsermetadataBehavior">
<endpoint address="http://127.0.0.1:3718/UserService"
binding="wsHttpBinding"
contract="Service.Interface.IUser"/>
</service>
</services>
</system.serviceModel>
</configuration>


using System.ServiceModel;
using Service.ServerDeom;
using Service.Interface;
namespace Windows_HostService
{
public partial class Service1 : ServiceBase
{
private ServiceHost serviceHost = null; //寄宿服务对象
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
try
{
serviceHost = new ServiceHost(typeof(UserService));
if (serviceHost.State != CommunicationState.Opened)
{
serviceHost.Open();
}
}
catch (Exception ex)
{
}
} protected override void OnStop()
{
}
}
}

然后安装服务文件的一些属性进行设置,比如服务自动重启,服务名称等;另一些图是对服务进行安装;
![]() |
![]() |
若服务安装成功后会在服务里可以看到如下:

客户端调用服务的代码大家就直接看源代码,由于本人也是刚开始学习WCF,实例内容为本人学习所做,若有不足或错误欢迎指正;[源代码下载]
WCF小实例以及三种宿主的更多相关文章
- 获得 LayoutInflater 实例的三种方式
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...
- [转]获得 LayoutInflater 实例的三种方式
转自:http://www.cnblogs.com/androidez/archive/2013/07/01/3164729.html 获得 LayoutInflater 实例的三种方式 在实际开 ...
- Java Class类以及获取Class实例的三种方式
T - 由此 Class 对象建模的类的类型.例如,String.class 的类型是Class<String>.如果将被建模的类未知,则使用Class<?>. publi ...
- Unity3d UGUI 界面适配 实例解析 三种适配方式
之前做游戏都是用公司自己的引擎,对于界面的适配分Android和IOS两种系统处理. 对于IOS设备,由于数量有限,只要做到 960x640 1024x769 1136x640 (当时还没有ipho ...
- 【转】delphi程序只允许运行一个实例的三种方法:
一. 创建互斥对象 在工程project1.dpr中创建互斥对象 Program project1 Uses Windows,Form, FrmMain in 'FrmMain.pas' ...
- StringBuffer 清空StringBuffer的实例的三种方法
@Test public void testStringbuffer(){ //StringBuffer类没有clear方法,不过可以通过下面两种方法来清空一个StringBuffer的实例: Str ...
- C#创建泛型类T的实例的三种方法
原文链接:https://www.cnblogs.com/lxhbky/p/6020612.html 方法一,通过外部方法传入的实例来实例化: //泛型类: public class MySQLHel ...
- WCF(二)三种通信模式
WCF在通信过程中有三种模式:请求与答复.单向.双工通信 请求与答复模式 客户端发送请求,然后一直等待服务端的响应答复(异步调用除外),期间处于假死状态,直到服务端有了答复后才能继续执行其他程序 请求 ...
- (二)微信小程序的三种传值方式
1.全局变量 app.js里 App({ //全局变量 globalData: { userInfo: null, host: 'http://localhost:8080/data.json' } ...
随机推荐
- 【百度地图API】发布静态图API啦!只需一个网址,即可展示定制百度地图!
原文:[百度地图API]发布静态图API啦!只需一个网址,即可展示定制百度地图! 摘要: 百度地图静态图API!您无须执行任何“特殊”操作便可在网页上显示此图片. 不需要 JavaScript.我们只 ...
- FTP下载帮助类
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...
- SQL Server 得到SPID,唯一的sessionID
像.net中的session一样,假设能知道了数据库中的sessionID,那全部的操作都能知道了,由于有了这个唯一的身份识别的标识. 能够做的事情有非常多,如:当前哪个用户在做什么操作,在运行什么s ...
- 记录近期小改K-Means至MapReduce上的心得
背景: 在所有聚类算法中KMeans算是表面上最简单的一种,没有过多恼人的古希腊符号公式,没有过分繁杂的公式嵌套.对于一个初学矩阵或者仅有向量概念的非专业人士的来说,不可不畏是一把踹门利器.这个世界上 ...
- RF1001: 各浏览器对 '@font-face' 规则支持的字体格式不同,IE 支持 EOT 字体,Firefox Safari Opera 支持 TrueType 等字体
http://w3help.org/zh-cn/causes/RF1001 http://blog.csdn.net/agileclipse/article/details/12450949 http ...
- Yii中CDbCriteria常用总结
Yii的Active Recorder包装了很多. 特别是把SQL中 把where,order,limit,IN/not IN,like等常用短句都包含进CDbCriteria这个类中去,这样整个代码 ...
- 设计模式学习--Factory Method
What Factory Method:定义一个创建对象的接口,让子类来决定实例化哪一个类.Factory Method使一个类的实例化延迟到其子类. Why Factory Method是一个比較基 ...
- PHP 18:data_valid_fns.php 看正则表达式
原文:PHP 18:data_valid_fns.php 看正则表达式 本章介绍正则表达式.先看看data_valid_fns.php的代码吧. 简要介绍一下.fill ...
- Cocos2d-x3.0 Json解析
2dx3.0下一个JSON解析库官员以及集成.我们参考一下吧OK. JSON文件hello.json内容 {"pets":["dog","cat&qu ...
- Virtualbox之Ubuntu虚拟机网络访问设置
在本机(Win7)中 利用VirtualBox安装了一个Ubuntu虚拟机,由于使用桥接,所以本机和虚拟机处于同一个网络局域网下,,主机能访问虚拟机.可是在Ubuntu更新软件的时候才发现不能联网.首 ...


