WCF service implementing multiple service contracts
来源:互联网 发布:nginx 域名配置 编辑:程序博客网 时间:2024/05/29 19:30
在上一篇中,我们创建了一个简单的WCF服务,那么接着在这一篇我们来展示一下,如何让一个WCF实现多个服务契约。
1、创建WCF服务契约
在上一篇中只是创建了一个WCF服务契约,这次为了展示WCF实现多个服务契约,所以创建两个服务契约。
<span style="font-family:SimSun;font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace CompanyService{ // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“ICompanyService”。 [ServiceContract] public interface ICompanyPublicService { [OperationContract] string GetPublicInfomation(); } [ServiceContract] public interface ICompanyConfidentialService { [OperationContract] string GetConfidentialInfomation(); }}</span>
2.创建WCF服务
让WCF服务同时实现这两个服务契约
<span style="font-family:SimSun;font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace CompanyService{ // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“CompanyService”。 public class CompanyService : ICompanyConfidentialService,ICompanyPublicService { public string GetConfidentialInfomation() { return "This is public information"; } public string GetPublicInfomation() { return "This is confidential information"; } }}</span>
3、创建宿主
同上篇博客中的配置类似,WCF服务还是需要依赖于宿主,宿主中的配置文件如下
<span style="font-family:SimSun;font-size:18px;"><?xml version="1.0" encoding="utf-8" ?><configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <services> <service name="CompanyService.CompanyService" behaviorConfiguration="mexBehavior"> <endpoint address="CompanyService" binding="basicHttpBinding" contract="CompanyService.ICompanyPublicService"> </endpoint> <endpoint address="CompanyService" binding="netTcpBinding" contract="CompanyService.ICompanyConfidentialService"> </endpoint> <host> <baseAddresses> <add baseAddress="http://localhost:8080/"/> <add baseAddress="net.tcp://localhost:8090/"/> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel></configuration></span>
4、启动宿主
在宿主中启动我们创建的服务
<span style="font-family:SimSun;font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.ServiceModel;namespace CompanyServiceHost{ class Program { static void Main(string[] args) { using(ServiceHost host=new ServiceHost(typeof(CompanyService.CompanyService))) { host.Open(); Console.WriteLine("host started" + DateTime.Now); Console.ReadLine(); } } }}</span>
5、创建web客户端
剩下的过程,就是在web客户单中引用我们的服务,根据自己的需求来调用不同的服务实现。
<span style="font-family:SimSun;font-size:18px;">using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace CompanyClient{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { CompanyService.CompanyPublicServiceClient client = new CompanyService.CompanyPublicServiceClient("BasicHttpBinding_ICompanyPublicService"); Label1.Text = client.GetPublicInfomation(); } protected void Button2_Click(object sender, EventArgs e) { CompanyService.CompanyConfidentialServiceClient client = new CompanyService.CompanyConfidentialServiceClient("NetTcpBinding_ICompanyConfidentialService"); Label2.Text = client.GetConfidentialInfomation(); } }}</span>
6、小结
本篇博客通过一个Demo,向大家展示了一个WCF服务实现多个服务契约。下一篇博客的内容是如何在不停止客户端的情况下,改变一个WCF服务。
本篇博客DEMO:WCF服务实现多个服务契约
0 0
- WCF service implementing multiple service contracts
- WCF 4 Step By Step Chapter 6 Note (Maintaining Service Contracts and Data Contracts)
- WCF 4 Step By Step Chapter13 Note (Implementing a WCF Service for Good Performance)
- Exposing a WCF Service With Multiple Bindings and Endpoints
- WCF-Contracts
- [WCF] Service contract
- WCF Service Configuration Editor
- WCF Data Service QuickStart
- WCF service performance issue
- WCF Service Binding Explained
- WCF Service Reference error
- wcf/web service 编码
- WCF Data Service 使用
- WCF Data Service
- Service Stack 与 WCF
- WCF与Web Service
- WCF与Web Service
- Creating a WCF Service
- extjs3.4 ComboBoxTree高级实现
- Esper学习之三:进程模型 .
- 在真机运行报错:The provisioning profile specified in your build settings...
- 乱码
- 开发中遇到的问题(2015-06-01)
- WCF service implementing multiple service contracts
- jquery $(this).attr $(this).val方法使用介绍
- 接入微信SDK64位包报错
- Java多线程学习笔记
- objective c 的performSelector 与block
- 00-自测4Have Fun with Numbers (20)
- 源码阅读 1 SimpleRatingView
- 关于删除(删除单个与多个)
- WebStorm之——配置svn(插曲)