Invalid request handling in OData

放肆的年华 提交于 2019-12-12 04:24:01

问题


Im writing an OData webservice with C# and I need some advice on how to handle invalid requests? An example of an error would be if a customer requested a service on an item they no longer own. I would idealy like to report back to the customer exactly why it is invalid as there are multiple possible reasons.

Many thanks


回答1:


I would recommend using FaultContracts as part of the OperationContract. You can define them just like DataContracts, and handle exceptions just like a custom exception.

[OperationContract]
[FaultContract(typeof(ItemNotOwned))]
Item GetItem(int ItemId);

You would define ItemNotOwned as a seperate contract definition.

Check it out on MSDN: http://msdn.microsoft.com/en-us/library/ms733841.aspx#Y323




回答2:


Just throw an exception with the message containing that information. The exception will be serialized to the response sent to the client. If you don't care about the response status code, any exception will do and the client will recieve 500 (Internal Server Error). If you want a specific status code throw an instance of DataServiceException.




回答3:


Look at using QueryInterceptors and ChangeInterceptors

http://robbincremers.me/2012/01/24/wcf-rest-service-with-odata-and-entity-framework-with-client-context-custom-operations-and-operation-interceptors/



来源:https://stackoverflow.com/questions/6508003/invalid-request-handling-in-odata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!