给定合同,例如:
[ServiceContract] public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "GetData/{id}.{format}")]
ResponseData GetData(string id, string format);
}
有没有办法让服务在请求时以 json 响应:
/GetData/1234.json, xml 当请求为 /GetData/1234.xml 时,仍然可以在其他某个 url 上作为适当的肥皂服务使用,并具有强类型的 wsdl 合同?
使用 Stream 作为 GetData 的返回值是行不通的,尽管它满足了前两个要求,但 wcf 无法创建完整的 wsdl 规范,因为它不知道生成的 Stream 的内容是什么。
You should有两个单独的方法,它们采用 id 和 format (它们会调用一个共享实现,该实现返回ResponseData
)其中有不同的WebGet属性 http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webgetattribute.aspx:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "GetData/{id}.{format}.xml",
ResponseFormat=WebMessageFormat.Xml)]
ResponseData GetDataXml(string id, string format);
[OperationContract]
[WebGet(UriTemplate = "GetData/{id}.{format}.json",
ResponseFormat=WebMessageFormat.Json)]
ResponseData GetDataJson(string id, string format);
}
对于 SOAP 端点,您应该能够调用任一方法,但是您必须有一个单独的方法ServiceHost http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx托管合约执行的实例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)