我有一个 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);
}
}
网络配置:
<?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>
类产品结果:
[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; }
}
类 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; }
}
类错误结果:
[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:
...
是什么原因?