我的WCF之旅(7):面向服务架构(SOA)和面向对象编程(OOP)的结合 - Part II
4.Host Service:http://localhost/Artech.InheritanceHierarchy
现在我们完成了Service的定义,现在我们来Host我们定义的Service,这次我们通过IIS的方式来host service。我们首先在该Website中引用Artech.InheritanceHierarchy.Service Project。然后为FullWhetherForecastService定义相应的.SVC文件(由于Service Contract的继承关系构成了一种Service Contract的层次结构,从而导致所有定义的Operation都出现在最底层的Contract中,由于SimpleWhetherForecastService的Operation没有被FullWhetherForecastServiceOverride,所以现在我们只需要Host FullWhetherForecastService就可以了)。
<%@ ServiceHost Service="Artech.InheritanceHierarchy.Service.FullWhetherForecastService,Artech.InheritanceHierarchy.Service" %>
接着我们在Web.config中为Service注册Endpoint。
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service name="Artech.InheritanceHierarchy.Service.FullWhetherForecastService" behaviorConfiguration="returnFaults">
<endpoint contract="Artech.InheritanceHierarchy.Service.IFullWhetherForecast" binding="wsHttpBinding"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="returnFaults">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true">
<assemblies>
<add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="Microsoft.Transactions.Bridge, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="SMDiagnostics, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.IdentityModel.Selectors, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Web.RegularExpressions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Messaging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
</system.web>
</configuration>
5.定义Client:Artech.InheritanceHierarchy.Client
到现在为止,我们完成了Service的定义和Host的工作,换句话说现在我们定义的Whether
Forecast service已经可以访问,并暴露在这样的一个Address上:http://localhost/Artech.InheritanceHierarchy/FullWhetherForecastService.svc。现在我们来编写我们Client来访问这个Service。值得一说的是,现在这个Sample中,Client是一个独立的Application,我们既没有让他引用我们定义的Artech.InheritanceHierarchy.BusinessEntity(在WCF中,这个相当于Data Contract),也没有让它和Service共享同一个Service Contract。这很类似于在纯Web环境下调用Service。