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