我已经使用WCF创建了REST服务,并且我有不同的合同(Contract1,Contract2等等)。
这是web.config
<endpoint address="users" binding="webHttpBinding" behaviorConfiguration="web" contract="FilmInfoApi.Service.IUserService"/>
<endpoint address="actors" binding="webHttpBinding" behaviorConfiguration="web" contract="FilmInfoApi.Service.IActorService"/>
<endpoint address="films" binding="webHttpBinding" behaviorConfiguration="web" contract="FilmInfoApi.Service.IFilmService"/>
这是合同的例子。
[OperationContract]
[WebGet(UriTemplate = "?offset={offset}&count={count}", ResponseFormat = WebMessageFormat.Json)]
Films GetFilms(string offset, string count);
所以我的问题是如何为所有端点使用相同的地址(localhost / rest)。因为我需要契约UriTemplate更灵活,例如,如果我需要按类别返回电影列表(例如:localhost / rest / category / 2)。但是使用这个uri和当前配置(web.config中的地址属性),我必须将此方法放入Category合约中。但在我看来,这种方法必须在电影合同中。那它有解决方案还是正常的?
答案 0 :(得分:1)
如果要使用单个端点公开多个合同,则必须从另一个端点继承其中一个合同。然后让您的端点公开派生合同。如下所示:
[ServiceContract]
IFilmService : IActorService
然后有一个端点:
<endpoint address="rest" binding="webHttpBinding" behaviorConfiguration="web" contract="FilmInfoApi.Service.IFilmService"/>