WCF小实例以及三种宿主
2013-07-31 23:38
246 查看
最近一直在学习WCF相关知识,下面将通过一个小实例对所学的知识进行简单的回顾;本实例是一个简单三层操作数据库,并且也简单实现的三种宿主(控制台宿主,IIS宿主以及Windows服务宿主)的练习;还包含一个双工的功能,下图为程序所创建分层结构图;
首先了解为这个实例所创建的两张简单表;
接下来简单了解每一层的作用以及一些注意事项;源代码后面直接提供下载;
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分别是对三种寄存宿主进行测试的客户端;
一:注意点批量寄存宿主;把重点的一些配置一及实现代码贴出;
二:宿主要IIS里的注意内容;
新建一个文本文件然后把它的后缀修改成.svc;并在其头部增加一行调用服务的代码;
<%@ ServiceHost Language="C#" Debug="true" Service="Service.ServerDeom.UserService" %>
其配置如下:
把I宿主部署到IIS里;
三:宿主在Windows服务;创建一个Windows服务类库,增加相应的配置以及代码;
然后安装服务文件的一些属性进行设置,比如服务自动重启,服务名称等;另一些图是对服务进行安装;
若服务安装成功后会在服务里可以看到如下:
客户端调用服务的代码大家就直接看源代码,由于本人也是刚开始学习WCF,实例内容为本人学习所做,若有不足或错误欢迎指正;[源代码下载]
首先了解为这个实例所创建的两张简单表;
USE [TestDb] GO /****** 对象: Table [dbo].[T_Account] 脚本日期: 07/31/2013 23:09:27 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[T_Account]( [ID] [int] IDENTITY(1,1) NOT NULL, [Uid] [int] NULL, [Money] [int] NULL, CONSTRAINT [PK_T_Account] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO USE [TestDb] GO /****** 对象: Table [dbo].[T_User] 脚本日期: 07/31/2013 23:10:00 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[T_User]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL, [PassWord] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] |
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小实例以及三种宿主
- WCF小实例以及三种宿主
- WCF实例管理的三种方式
- WCF三种实例激活的类型——实践理解三种类型的区别
- WCF寄宿(Host)之自我寄宿(Self-Hosting)简单实例【Console应用为宿主】
- WCF消息安全模式之自定义用户名密码:Message CustomUserNamePassword wsHttpBinding 使用Windows Services宿主以及客户端免证书验证
- (2)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- C# WCF 完整实例,winform 窗体作为 宿主
- 使用IIS作为宿主发布WCF服务常见错误以及解决方法
- 使用IIS作为宿主发布WCF服务常见错误以及解决方法
- 三种方式获得LayoutInflater 实例以及两种形式inflate方法
- VS 2010中使用C#创建及调用WCF完整实例(Windows服务宿主)
- 深入理解java.lang.Class类以及class类获取实例的三种方法
- Hibernate连接池三种配置实例 以及解决C3P0错误:com.mchange.v2.c3p0.stmt.GooGooStatementCache - Problem with checked-in Statement, discarding.
- Java Class类以及获取Class实例的三种方式
- Java Class类以及获取Class实例的三种方式
- WCF实例上下文以及会话学习
- (3)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- Java Class类以及获取Class实例的三种方式
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志