我有一个Rest API(WCF Net 4.0):
[ServiceContract]
interface ISubscriptionService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = Routing.ProductsRoute, BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
[Description("Получение информации о продуктах")]
ProductsResult Products(string timestamp, string transaction_id);
}
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
class SubscriptionService : ISubscriptionService
{
public ProductsResult Products(string timestamp, string transaction_id)
{
SubscriptionProcessing processing = new SubscriptionProcessing();
processing.SaveHistory(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri);
return processing.GetProducts(timestamp, transaction_id);
}
}
的Web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="DefaultEndPointBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="CloudWebAPI.SubscriptionService">
<endpoint address="" kind="webHttpEndpoint" behaviorConfiguration="DefaultEndPointBehavior" contract="CloudWebAPI.ISubscriptionService" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE"/>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
Class ProductsResult:
[DataContract(Name = "result", Namespace = "")]
public class ProductsResult : CommonResult
{
public ProductsResult()
{
Products = new List<ProductResult>();
}
[DataMember(Name = "products", Order = 3)]
public List<ProductResult> Products { get; set; }
}
Class CommonResult:
[DataContract(Name = "result", Namespace = "")]
public class CommonResult
{
public CommonResult()
{
TransactionId = string.Empty;
Error = new ErrorResult();
}
[DataMember(Name = "transaction_id", Order = 1)]
public string TransactionId { get; set; }
[DataMember(Name = "error", Order = 2)]
public ErrorResult Error { get; set; }
}
Class ErrorResult:
[DataContract(Name = "error", Namespace = "")]
public class ErrorResult
{
public ErrorResult()
{
Code = 0;
Message = string.Empty;
}
[DataMember(Name = "error", Order = 1)]
public int Code { get; set; }
[DataMember(Name = "message", Order = 2)]
public string Message { get; set; }
}
打开页面... / rest / SubscriptionService.svc / help / operations / Products,我得到:
Message direction Format Body
Request N/A The Request body is empty.
Response Unknown Could not generate schema document.
但如果您更改ProductsResult [DataContract(Name =“blabla”,Namespace =“”)] 类的属性,一切正常:
Response Xml Example,Schema
Response Json Example
The following is an example response Xml body:
...
是什么原因?
答案 0 :(得分:0)
正如ask125342在上面的评论中所说,类不能与它们继承的类具有相同的名称装饰器 - 这将破坏WCF帮助页面的自动生成。