咨詢電話:023-6276-4481
熱門文章
電 話:023-6276-4481
郵箱:broiling@qq.com
地址:重慶市南岸區(qū)亞太商谷6幢25-2
因?yàn)樽畛蹂e(cuò)誤的理解了Contract與Service的關(guān)系,把每個(gè)業(yè)務(wù)定義了相應(yīng)的Contract與Service并將對(duì)應(yīng)的Service一一繼承相應(yīng)的Contract,因?yàn)樵赪CF每個(gè)host只能提供一個(gè)Service所以導(dǎo)致,當(dāng)你的服務(wù)很多的時(shí)候你要定義N多個(gè)host
看下面演示提供User(用戶)和Order(訂單)兩個(gè)服務(wù):
開(kāi)啟多個(gè)host代碼:
ServiceHost host1 = new ServiceHost(typeof(UserService));
host1.Open();
ServiceHost host2 = new ServiceHost(typeof(OrderService));
host2.Open();
開(kāi)啟多個(gè)host配置
代碼
代碼
<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多個(gè)服務(wù)?
http://www.cnblogs.com/vivid-stanley/archive/2007/02/21/653280.html
Host多個(gè)WCF服務(wù)(Self-host)
http://www.cnblogs.com/levinknight/archive/2007/05/25/760176.html
后來(lái)發(fā)現(xiàn)Service是可以和業(yè)務(wù)類型沒(méi)關(guān)系的,它僅用來(lái)表示當(dāng)前host能提供的服務(wù),不要理解Service要根據(jù)不同的業(yè)務(wù)類型來(lái)切分,Contract才是和業(yè)務(wù)類型有關(guān)系的原則上要按照業(yè)務(wù)類型來(lái)切分。然后其實(shí)WCF并不推薦在應(yīng)用程序域中創(chuàng)建多個(gè)ServiceHost實(shí)例。如果要托管多個(gè)服務(wù),完全可以在一個(gè)host中通過(guò)多個(gè)Endpoint公開(kāi)多個(gè)WCF服務(wù)的辦法來(lái)實(shí)現(xiàn),而每個(gè)Endpoint都可以對(duì)應(yīng)相應(yīng)的Contract。
User契約代碼:
namespace Wcf.Contract
{
[ServiceContract]
public interface IUserService
{
[OperationContract]
void Edit();
}
}
Order契約代碼:
namespace Wcf.Contract
{
[ServiceContract]
public interface IOrderService
{
[OperationContract]
void Add();
}
}
Service代碼:讓所有功能在一個(gè)Service上實(shí)現(xiàn)
代碼
代碼
namespace Wcf.Services
{
public class MallService : IUserService, IOrderService
{
public void Add()
{
throw new NotImplementedException();
}
void Edit()
{
throw new NotImplementedException();
}
}
}
當(dāng)然這里可以使用 partial 關(guān)鍵字 把代碼放在不同的文件上,來(lái)達(dá)到物理上的切分比如:
文件UserService.cs
public partial class MallService : IUserService
文件 OrderService.cs
public partial class MallService : IOrderService
host代碼
代碼
代碼
namespace Wcf.Host
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(Wcf.Services.MallService)))
{
host.Open();
Console.Read();
}
}
}
}
配置文件:在一個(gè)service上定義多個(gè)endpoint使用不同的契約接口
代碼
代碼
<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>
客戶端代碼:客戶端可以根據(jù)不同endpoint的契約實(shí)現(xiàn)不同的類
代碼
代碼
namespace Wcf.Test
{
class Program
{
static void Main(string[] args)
{
UserServiceClient user = new UserServiceClient();
user.Edit();
OrderServiceClient order = new OrderServiceClient();
order.Add();
}
}
}
以后如果要對(duì)WCF應(yīng)用程序擴(kuò)展只需定義契約 然后partial class MallService : 契約接口 實(shí)現(xiàn)代碼, 并在host的在配置文件中加入 相應(yīng)的 endpoint 即可