一、服务契约介绍
       
我个人理解服务契约是一组公开的操作,其中公开的操作(OperationContract)只能定义在方法(Method)上。对于我们要公开的服务我们可以在接口或者类上加上标识ServiceContract。但是我们一般情况下,会把ServiceContract定义在接口上而不是类上,这样有几个好处:
       1.方便契约的继承,不同的类型可以去实现相同的契约,重用性高。
       2.同一服务可以去实现多个契约。
       3.可以随时去修改服务类型,而不需去修改接口。
       下面定义一个服务的契约:   
WCF学习(二)-------服务契约using System;
WCF学习(二)-------服务契约
using System.Collections.Generic;
WCF学习(二)-------服务契约
using System.Linq;
WCF学习(二)-------服务契约
using System.Text;
WCF学习(二)-------服务契约
using System.ServiceModel;
WCF学习(二)-------服务契约
WCF学习(二)-------服务契约
namespace Service
}
我们在上面首先定义了一个接口名称为:ICalucator,然后我们要将这个接口公开为服务契约在上面加上属性标识[ServiceContract],其中Name可以为契约指定别名,这样的话,如果我们在客户端遇到相同的接口时可以通过Name来制定别名区别开来。公开的操作为Add方法,在上面标识[OperationContract]即可。

二、方法的重载
    按照我们正常的方法去重载的话,只需要方法的参数不同(个数、类型)就可以实现方法的重载。但是我们在服务契约的定义的时候是不能怎样的,wdsl是编译同不过的如:
WCF学习(二)-------服务契约using System;
WCF学习(二)-------服务契约
using System.Collections.Generic;
WCF学习(二)-------服务契约
using System.Linq;
WCF学习(二)-------服务契约
using System.Text;
WCF学习(二)-------服务契约
using System.ServiceModel;
WCF学习(二)-------服务契约
WCF学习(二)-------服务契约
namespace Service
上面的方法是编译不能通过的。但是我们有一种解决的办法可以去解决这样的问题,就是通过OperationContract的Name属性来设定方法的别名是实现方法的重载。如:
WCF学习(二)-------服务契约using System;
WCF学习(二)-------服务契约
using System.Collections.Generic;
WCF学习(二)-------服务契约
using System.Linq;
WCF学习(二)-------服务契约
using System.Text;
WCF学习(二)-------服务契约
using System.ServiceModel;
WCF学习(二)-------服务契约
WCF学习(二)-------服务契约
namespace Service

下面我们通过数据元的方式来配置一个宿主主机,我们添加一个控制台的程序,通过配置App.Config来实现。
在App.Config中:
WCF学习(二)-------服务契约<?xml version="1.0" encoding="utf-8" ?>
WCF学习(二)-------服务契约
<configuration>
WCF学习(二)-------服务契约  
<system.serviceModel>
WCF学习(二)-------服务契约    
<services>
WCF学习(二)-------服务契约      
<service name="Service.Calucator" behaviorConfiguration="mex">
WCF学习(二)-------服务契约        
<host>
WCF学习(二)-------服务契约          
<baseAddresses>
WCF学习(二)-------服务契约            
<add baseAddress="http://localhost:8888"/>
WCF学习(二)-------服务契约          
</baseAddresses>
WCF学习(二)-------服务契约        
</host>
WCF学习(二)-------服务契约        
<endpoint address="Calucator" binding="basicHttpBinding" contract="Service.ICalucator"></endpoint>
WCF学习(二)-------服务契约      
</service>
WCF学习(二)-------服务契约    
</services>
WCF学习(二)-------服务契约    
<behaviors>
WCF学习(二)-------服务契约      
<serviceBehaviors>
WCF学习(二)-------服务契约        
<behavior name="mex">
WCF学习(二)-------服务契约          
<serviceMetadata httpGetEnabled="true"/>
WCF学习(二)-------服务契约        
</behavior>
WCF学习(二)-------服务契约      
</serviceBehaviors>
WCF学习(二)-------服务契约    
</behaviors>
WCF学习(二)-------服务契约  
</system.serviceModel>
WCF学习(二)-------服务契约
</configuration>
在主程序中:
WCF学习(二)-------服务契约using System;
WCF学习(二)-------服务契约
using System.Collections.Generic;
WCF学习(二)-------服务契约
using System.Linq;
WCF学习(二)-------服务契约
using System.Text;
WCF学习(二)-------服务契约
using System.ServiceModel;
WCF学习(二)-------服务契约
using Service;
WCF学习(二)-------服务契约
WCF学习(二)-------服务契约
namespace Host
然后我们启动主机,在浏览器中输入地址:http://localhost:8888/?wsdl 。我们可以看到:
WCF学习(二)-------服务契约



我们可以看到wsdl编译时已经将名称编译成为了我们Name中定义的别名。

相关文章:

  • 2021-08-12
  • 2021-06-05
  • 2022-12-23
  • 2021-08-30
  • 2022-02-13
  • 2021-11-09
  • 2021-08-11
猜你喜欢
  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2022-02-28
相关资源
相似解决方案