Overriding methods on System.Data.Services.DataService<T>

There are three methods on the DataService<T> class that you might want to override.

The first is CreateDataSource. Typically when the data source (the 'T') needs to be constructed, an empty constructor will be invoked. This is great if the default constructor can pick up information from a web.config file, for example, but sometimes you need to do some initialization yourself, like reading a connection string from somewhere else. In this case, you can simply override CreateDataSource to do all the work you need and create a new instance. Typically this method will be called once per request.

The second is OnStartProcessingRequest. You don't need to do anything here, but it might be useful to look at the incoming URI if you want to do some validation or do some checks based on the current WebOperationContext, for example. You can log information away, or perhaps throw an exception to stop processing (consider throwing a DataServiceException if you'd like to set a specific HTTP status code or message you want to send to the client).

Finally, the third virtual method is HandleException. Here you can take a look at how the exception is about to get handled, and it's a great location to just set a breakpoint and see what exceptions are ocurring on your service and perhaps do some logging. There are also a couple of interesting things you can do, which is set the Exception property or the UseVerboseErrors property on the method arguments. If you do so, processing will continue using the values you supply as the effective exception and verbose flags. A common thing to do, for example, would be to set UseVerboseErrors to true if you trust the client and the connection is secure.



Comments (0)

Skip to main content