MongOData
MongOData copied to clipboard
How to filter/intercept query?
Hello, I want to intercept a query in a way that only data are returned that contains a given GUID so the user can only see his own data. How can I do this?
Can you provide some more details of what's you're trying to achieve? An example? Is this something that must be filtered on a server?
I just found issue #9 where a sample in VB.Net was posted,so I'am currently using this piece of code which seems to work:
[QueryInterceptor("LogMessages")]
public Expression<Func<DSPResource, bool>> OnPostQuery()
{
Guid? myApiKey = new Guid("00000000-0000-0000-0000-000000000000");
return x => (Guid)x.GetValue("ApiKey") == myApiKey;
}
Is this the right way to it?
#Edit: Is there any way to use the DB model so I could do something like this:
[QueryInterceptor("LogMessages")]
public Expression<Func<LogMessage, bool>> OnPostQuery()
{
Guid myApiKey = new Guid("00000000-0000-0000-0000-000000000000");
return x => x.ApiKey == myApiKey;
}
Currently it just throws an exception that the types doesn't match:
The server encountered an error processing the request. The exception message is 'Return type of method 'OnPostQuery' on type 'WCFTest.MongoDataService' is of type 'System.Linq.Expressions.Expression`1[[System.Func`2[[WCFTest.LogMessage, WCFTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' but a type assignable to 'System.Linq.Expressions.Expression`1[[System.Func`2[[DataServiceProvider.DSPResource, DataServiceProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' is required for a query interceptor.'. See server logs for more details. The exception stack trace is:
at System.Data.Services.DataServiceStaticConfiguration.CheckQueryInterceptorSignature(Type dataServiceType, MethodInfo method, ResourceSet container) at System.Data.Services.DataServiceStaticConfiguration.RegisterCallbacks(Type dataServiceType) at System.Data.Services.DataServiceStaticConfiguration..ctor(Type dataServiceType, IDataServiceMetadataProvider provider) at System.Data.Services.DataService`1.CreateProvider() at System.Data.Services.DataService`1.EnsureProviderAndConfigForRequest() at System.Data.Services.DataService`1.HandleRequest() at System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody) at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
Thanks for an example. I will try to reproduce the exception.