中国a级毛片免费观看,啦啦啦资源视频在线完整免费高清,真人性囗交69视频,日本毛X片免费视频观看视频

rexian

咨詢電話:023-6276-4481

熱門文章

聯(lián)系方式

電 話:023-6276-4481

郵箱:broiling@qq.com

地址:重慶市南岸區(qū)亞太商谷6幢25-2

當(dāng)前位置:網(wǎng)站首頁 > 技術(shù)文章 > WCF一個Host實(shí)現(xiàn)多契約服務(wù)

WCF一個Host實(shí)現(xiàn)多契約服務(wù)

編輯:T.T 發(fā)表時間:2018-04-11 12:13:06
T.T

因?yàn)樽畛蹂e誤的理解了Contract與Service的關(guān)系,把每個業(yè)務(wù)定義了相應(yīng)的Contract與Service并將對應(yīng)的Service一一繼承相應(yīng)的Contract,因?yàn)樵赪CF每個host只能提供一個Service所以導(dǎo)致,當(dāng)你的服務(wù)很多的時候你要定義N多個host
看下面演示提供User(用戶)和Order(訂單)兩個服務(wù):

開啟多個host代碼:

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

 

 

開啟多個host配置

代碼 

復(fù)制代碼

代碼

<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>

復(fù)制代碼

 

 

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

 

后來發(fā)現(xiàn)Service是可以和業(yè)務(wù)類型沒關(guān)系的,它僅用來表示當(dāng)前host能提供的服務(wù),不要理解Service要根據(jù)不同的業(yè)務(wù)類型來切分,Contract才是和業(yè)務(wù)類型有關(guān)系的原則上要按照業(yè)務(wù)類型來切分。然后其實(shí)WCF并不推薦在應(yīng)用程序域中創(chuàng)建多個ServiceHost實(shí)例。如果要托管多個服務(wù),完全可以在一個host中通過多個Endpoint公開多個WCF服務(wù)的辦法來實(shí)現(xiàn),而每個Endpoint都可以對應(yīng)相應(yīng)的Contract。

 

User契約代碼:

 

復(fù)制代碼

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

復(fù)制代碼

 

 Order契約代碼:

復(fù)制代碼

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

復(fù)制代碼

 

 

Service代碼:讓所有功能在一個Service上實(shí)現(xiàn)

代碼 

復(fù)制代碼

代碼

namespace Wcf.Services
{
    
public class MallService : IUserService, IOrderService
    {

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


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


    }
}

復(fù)制代碼

 

 

當(dāng)然這里可以使用 partial 關(guān)鍵字 把代碼放在不同的文件上,來達(dá)到物理上的切分比如:
文件UserService.cs
 public partial  class MallService : IUserService
文件 OrderService.cs
 public partial  class MallService : IOrderService

 

host代碼

代碼 

復(fù)制代碼

代碼

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

復(fù)制代碼

 

 

配置文件:在一個service上定義多個endpoint使用不同的契約接口

代碼 

復(fù)制代碼

代碼

<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>

復(fù)制代碼

 

 

客戶端代碼:客戶端可以根據(jù)不同endpoint的契約實(shí)現(xiàn)不同的類

代碼 

復(fù)制代碼

代碼

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

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

復(fù)制代碼

 

 

以后如果要對WCF應(yīng)用程序擴(kuò)展只需定義契約 然后partial  class MallService : 契約接口 實(shí)現(xiàn)代碼, 并在host的在配置文件中加入 相應(yīng)的 endpoint 即可