您的位置:首页 > 其它

WCF中服务继承多个契约的使用

2011-12-15 14:48 417 查看
服务继承多个契约其实也就是服务类实现了多个接口,主要是在配置中需要添加多个endpoint,各个endpoint之间的address不同、contract不同

契约:

[ServiceContract]
public interface IReportService
{
[OperationContract]
[WebGet(UriTemplate="Task",ResponseFormat=WebMessageFormat.Json)]
List<SampleItem> GetList();
}


[ServiceContract]
public interface ITestMoreContract
{
[OperationContract]
[WebGet(UriTemplate = "GetListTask/{objID}", ResponseFormat = WebMessageFormat.Json)]
List<SampleItem> GetListTask(string objID);
}


服务实现:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IReportService, ITestMoreContract
{
public List<SampleItem> GetList()
{
return new List<SampleItem>
{
new SampleItem {Id=1,StringValue="value1"},
new SampleItem {Id=2,StringValue="value2"},
};
}

public List<SampleItem> GetListTask(string objID)
{
if (objID == "1")
{
return new List<SampleItem>
{
new SampleItem {Id=55,StringValue="value55"},
new SampleItem {Id=66,StringValue="value66"},
};
}

return null;
}
}


配置文件中endpoint节:

<endpoint address="" behaviorConfiguration="BXQS.Service.Report.ReportManage.Behavior" binding="webHttpBinding"
contract="WcfRestService1.IReportService"/>
<endpoint address="Test" behaviorConfiguration="BXQS.Service.Report.ReportManage.Behavior" binding="webHttpBinding"
contract="WcfRestService1.ITestMoreContract"/>


客户端调用时访问:

localhost:8001/Service1.svc/Task

localhost:8001/Service1.svc/Test/GetListTask/1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: