WCF学习(二)-------服务契约

一、服务契约介绍
       
我个人理解服务契约是一组公开的操作,其中公开的操作(OperationContract)只能定义在方法(Method)上。对于我们要公开的服务我们可以在接口或者类上加上标识ServiceContract。但是我们一般情况下,会把ServiceContract定义在接口上而不是类上,这样有几个好处:
       1.方便契约的继承,不同的类型可以去实现相同的契约,重用性高。
       2.同一服务可以去实现多个契约。
       3.可以随时去修改服务类型,而不需去修改接口。
       下面定义一个服务的契约:   
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ServiceModel;

namespace  Service
{
    [ServiceContract(Name
="Service_Calucator",Namespace="Henllyee")]
    
public interface ICalucator
    
{
        [OperationContract]
        
int Add(int x, int y);

    }

}
我们在上面首先定义了一个接口名称为:ICalucator,然后我们要将这个接口公开为服务契约在上面加上属性标识[ServiceContract],其中Name可以为契约指定别名,这样的话,如果我们在客户端遇到相同的接口时可以通过Name来制定别名区别开来。公开的操作为Add方法,在上面标识[OperationContract]即可。

二、方法的重载
    按照我们正常的方法去重载的话,只需要方法的参数不同(个数、类型)就可以实现方法的重载。但是我们在服务契约的定义的时候是不能怎样的,wdsl是编译同不过的如:
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ServiceModel;

namespace  Service
{
    [ServiceContract(Name
="Service_Calucator",Namespace="Henllyee")]
    
public interface ICalucator
    
{
        [OperationContract]
        
int Add(int x, int y);

        [OperationContract]
        
double Add(double x, double y);
    }

}

上面的方法是编译不能通过的。但是我们有一种解决的办法可以去解决这样的问题,就是通过OperationContract的Name属性来设定方法的别名是实现方法的重载。如:
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ServiceModel;

namespace  Service
{
    [ServiceContract(Name
="Service_Calucator",Namespace="Henllyee")]
    
public interface ICalucator
    
{
        [OperationContract(Name
="IntAdd")]
        
int Add(int x, int y);

        [OperationContract(Name
="DoubleAdd")]
        
double Add(double x, double y);
    }

}


下面我们通过数据元的方式来配置一个宿主主机,我们添加一个控制台的程序,通过配置App.Config来实现。
在App.Config中:
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>
< configuration >
  
< system.serviceModel >
    
< services >
      
< service name = " Service.Calucator "  behaviorConfiguration = " mex " >
        
< host >
          
< baseAddresses >
            
< add baseAddress = " http://localhost:8888 " />
          
</ baseAddresses >
        
</ host >
        
< endpoint address = " Calucator "  binding = " basicHttpBinding "  contract = " Service.ICalucator " ></ endpoint >
      
</ service >
    
</ services >
    
< behaviors >
      
< serviceBehaviors >
        
< behavior name = " mex " >
          
< serviceMetadata httpGetEnabled = " true " />
        
</ behavior >
      
</ serviceBehaviors >
    
</ behaviors >
  
</ system.serviceModel >
</ configuration >
在主程序中:
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ServiceModel;
using  Service;

namespace  Host
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            ServiceHost host 
= null;
            
try
            
{
                host 
= new ServiceHost(typeof(Calucator));
                host.Open();

                Console.Write(
"Host is opening now! Press any key to stop");
                Console.Read();
            }

            
finally
            
{
                host.Close();
            }

        }

    }

}

然后我们启动主机,在浏览器中输入地址: http://localhost:8888/?wsdl 。我们可以看到:




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

转载于:https://www.cnblogs.com/Henllyee/archive/2008/06/30/1232284.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值