从IDataServiceQueryProvider.GetPropertyValue抛出的异常处理不当?

时间:2013-06-16 20:05:59

标签: wcf wcf-data-services odata

此问题适用于具有自定义提供程序的WCF数据服务。直到最近,我们对IDataServiceQueryProvider或IDataServiceUpdateProvider的实现引发的异常没有任何问题。我们面临的当前问题是IDataServiceQueryProvider.GetPropertyValue抛出的异常未得到正确处理。例如,此请求:

http://localhost:52788/NorthwindCustomers.svc/Customers('ALFKI')

有了这个实现:

public object GetPropertyValue(object target, ResourceProperty resourceProperty)
{
    throw new DataServiceException((int)HttpStatusCode.BadRequest, "Bad request!");
}

结果:

HTTP/1.1 200 OK
<?xml version="1.0" encoding="utf-8"?><entry xml:base="http://localhost:52788/NorthwindCustomers.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><id>http://localhost:52788/NorthwindCustomers.svc/Customers('ALFKI')</id><category term="NorthwindModel.Customer" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" title="Customer" href="Customers('ALFKI')" /><m:error><m:code /><m:message xml:lang="en-US">Bad request!</m:message></m:error>

而其他方法抛出相同的异常,例如:

public IQueryable GetQueryRootForResourceSet(ResourceSet resourceSet)
{
    throw new DataServiceException((int)HttpStatusCode.BadRequest, "Bad request!");
}

预期的结果:

HTTP/1.1 400 Bad Request
<?xml version="1.0" encoding="utf-8" standalone="yes"?><error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><code></code><message xml:lang="en-US">Bad request!</message></error>

我已经通过一个简单的服务和自定义提供商重现了这一点,以消除由我们的生产实施引起问题的可能性。

为什么框架会为IDataServiceQueryProvider和IDataServiceUpdateProvider方法的所有实现抛出的异常创建一个具有正确状态代码和正文的响应,但GetPropertyValue除外?

0 个答案:

没有答案