[转]WCF一个Host实现多契约服务

因为最初错误的理解了Contract与Service的关系,把每个业务定义了相应的Contract与Service并将对应的Service一一继承相应的Contract,因为在WCF每个host只能提供一个Service所以导致,当你的服务很多的时候你要定义N多个host
看下面演示提供User(用户)和Order(订单)两个服务:

开启多个host代码:

ServiceHost host1  =   new  ServiceHost( typeof (UserService)); 
host1.Open();
ServiceHost host2 
=   new  ServiceHost( typeof (OrderService)); 
host2.Open();

 

 

开启多个host配置

代码 

ExpandedBlockStart.gif 代码
< system.serviceModel >
    
< services >
      
< service  name ="Wcf.Services.UserService" >
        
< endpoint  address ="net.tcp://localhost:8001/UserService"  binding ="netTcpBinding"   contract ="Wcf.Contract.IUserService"   >
        
</ endpoint >
      
</ service >
      
< service  name ="Wcf.Services.OrderService" >
        
< endpoint  address ="net.tcp://localhost:8001/OrderService"   binding ="netTcpBinding"  contract ="Wcf.Contract.IOrderService"   >
        
</ endpoint >
      
</ service >
    
</ services >
</ system.serviceModel >

 

 

参考:
WCF中ServiceHost能不能host多个服务?
http://www.cnblogs.com/vivid-stanley/archive/2007/02/21/653280.html
Host多个WCF服务(Self-host)
http://www.cnblogs.com/levinknight/archive/2007/05/25/760176.html

 

后来发现Service是可以和业务类型没关系的,它仅用来表示当前host能提供的服务,不要理解Service要根据不同的业务类型来切分,Contract才是和业务类型有关系的原则上要按照业务类型来切分。然后其实WCF并不推荐在应用程序域中创建多个ServiceHost实例。如果要托管多个服务,完全可以在一个host中通过多个Endpoint公开多个WCF服务的办法来实现,而每个Endpoint都可以对应相应的Contract。

 

User契约代码:

 

namespace  Wcf.Contract
{
    [ServiceContract]
    
public   interface  IUserService
    {
        [OperationContract]
        
void  Edit();
    }
}

 

 Order契约代码:

namespace  Wcf.Contract
{
    [ServiceContract]
    
public   interface  IOrderService
    {
        [OperationContract]
        
void  Add();
    }
}

 

 

Service代码:让所有功能在一个Service上实现

代码 

ExpandedBlockStart.gif 代码
namespace  Wcf.Services
{
    
public   class  MallService : IUserService, IOrderService
    {

    
        
public   void  Add()
        {
            
throw   new  NotImplementedException();
        }


        
void  Edit()
        {
            
throw   new  NotImplementedException();
        }


    }
}

 

 

当然这里可以使用 partial 关键字 把代码放在不同的文件上,来达到物理上的切分比如:
文件UserService.cs
 public partial  class MallService : IUserService
文件 OrderService.cs
 public partial  class MallService : IOrderService

 

host代码

代码 

ExpandedBlockStart.gif 代码
namespace  Wcf.Host
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            
using  (ServiceHost host  =   new  ServiceHost( typeof (Wcf.Services.MallService)))
            {
                host.Open();
                Console.Read();
            }
        }
    }
}

 

 

配置文件:在一个service上定义多个endpoint使用不同的契约接口

代码 

ExpandedBlockStart.gif 代码
< system.serviceModel >
    
< services >
      
< service  name ="Wcf.Services.MallService"  behaviorConfiguration ="MallServiceBehaviors"   >       
        
< endpoint   address =""  contract ="Wcf.Contract.IUserService"  binding ="basicHttpBinding" ></ endpoint >
        
< endpoint   address =""  contract ="Wcf.Contract.IOrderService"  binding ="basicHttpBinding" ></ endpoint >
        
< host >
          
< baseAddresses >
            
< add  baseAddress ="http://localhost:8899/MallService%22/>
          </baseAddresses>
        </host>
      </service>
      
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="
MallServiceBehaviors"  >
          
< serviceMetadata  httpGetEnabled ="true"   />
        
</ behavior >
      
</ serviceBehaviors >
    
</ behaviors >

  
</ system.serviceModel >

 

 

客户端代码:客户端可以根据不同endpoint的契约实现不同的类

代码 

ExpandedBlockStart.gif 代码
namespace  Wcf.Test
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            UserServiceClient user 
=   new  UserServiceClient();
            user.Edit();

            OrderServiceClient order 
=   new  OrderServiceClient();
            order.Add();
        }
    }
}

 

 

以后如果要对WCF应用程序扩展只需定义契约 然后partial  class MallService : 契约接口 实现代码, 并在host的在配置文件中加入 相应的 endpoint 即可

转载于:https://www.cnblogs.com/dotnet010/archive/2010/05/23/1741928.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值