By default WCF Data Service do not support
Parametrized QueryInterceptor in some rare scenarios feel that
Parameterized Query Interceptor could have been a great solution.
However we can leverage custom parameters to archive
the same effect.
Solution
Unfortunately there is no direct support of providing parameters in QueryInterceptor but there is possibility to add custom support. belwo is step by step solution
Actually Data Service class provide a base member OperationContext that contains information about Absolute URL is being passed all you need to do is Intercept this Absolute URI and use it in your Query Interceptor. High level steps are below.
Add two new data member in your
data service class
|
DataServiceOperationContext OpsContext;
Dictionary<string,
string> QueryParameters;
|
Add an override ‘OnStartProcessingRequest
‘ to Initialized OpsContext
|
protected override
void OnStartProcessingRequest(ProcessRequestArgs args)
{
OpsContext =
args.OperationContext;
QueryParameters = GetQueryParameterBag(OpsContext);
base.OnStartProcessingRequest(args);
}
|
Implement a function to populate QueryParameters from incoming
URL
|
Dictionary<string,
string> GetQueryParameterBag(DataServiceOperationContext
serviceOperationContext)
……….<See code details Belwo>
|
Implement a Parameter Verification
function
[Optional]
|
void
VerifyEmployeesInterceptorParameters(Dictionary<string, string>
QueryParams)
|
Implement your interceptor that
calls Parameter Verification Function and consume QueryParameters collection
|
[QueryInterceptor("vEmployees")]
public
Expression<Func<vEmployee, bool>>
FilterEmplyee()
{
VerifyEmployeesInterceptorParameters(this.QueryParameters);
string
FirstName = QueryParameters["FirstName"];
return
o => (o.FirstName.StartsWith(FirstName));
}
|
Once above steps are applied you can use URL Query parameters to bind with Query Interceptors i.e.
http://localhost:2545/WcfDataService1.svc/vEmployees?FirstName=Rober
Limitation of solution
This solution will not work if entity
paging is enabled i.e. custom parameters will not be forwarded to next
page url , this is a data service limitation please refer to MSDN thread
"Custom Query Options and Server Driven Paging" for more detailsComplete Data Service Class File
Note :- Code
Attached the the very bottom suggest standard approach i.e. putting
Parameter collection object and parameter verification separate function
but can be placed in Query interceptor itself if there is no reuse
required.
//-----------------------------------------------------------------------
// <copyright
file="ServerHelpers.cs" company="None">
// None
//
</copyright>
//-----------------------------------------------------------------------
namespace SampleDataService
{
using
System;
using
System.Collections.Generic;
using
System.Data.Services;
using
System.Data.Services.Common;
using
System.Linq;
using
System.ServiceModel.Web;
using
System.Web;
using
System.Linq.Expressions;
[System.ServiceModel.ServiceBehavior(IncludeExceptionDetailInFaults =
true)]
public class WCFDataService
: DataService<NORTHWNDEntities>
{
DataServiceOperationContext
OpsContext;
Dictionary<string, string>
QueryParameters;
// This
method is called only once to initialize service-wide policies.
public
static void
InitializeService(DataServiceConfiguration
config)
{
//
TODO: set rules to indicate which entity sets and service operations are
visible, updatable, etc.
//
Examples:
config.UseVerboseErrors = true;
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
Dictionary<string, string>
GetQueryParameterBag(DataServiceOperationContext
serviceOperationContext)
{
Dictionary<string, string>
ParameterBag = null;
string
query = serviceOperationContext.AbsoluteRequestUri.Query;
if
(!string.IsNullOrEmpty(query))
{
ParameterBag = new Dictionary<string, string>();
query = query.Remove(0, 1);
string[]
param = query.Split('&');
string
paramName;
string
paramValue;
foreach
(var item in
param)
if(!item.StartWith("$")
{ paramName = item.Substring(0, item.IndexOf("="));
paramValue =
item.Substring(item.IndexOf("=")
+ 1);
ParameterBag.Add(paramName, paramValue);
}
}
}
return
ParameterBag;
}
protected
override void
OnStartProcessingRequest(ProcessRequestArgs
args)
{
OpsContext =
args.OperationContext;
QueryParameters =
GetQueryParameterBag(OpsContext);
base.OnStartProcessingRequest(args);
}
void
VerifyEmployeesInterceptorParameters(Dictionary<string, string>
QueryParams)
{
if
(QueryParams==null||!QueryParams.Keys.Contains("FirstName"))
{
throw
new ArgumentException("Expecting parameter 'FirstName' but not
found");
}
}
[QueryInterceptor("Employees")]
public
Expression<Func<Employee, bool>>
FilterEmplyee()
{
VerifyEmployeesInterceptorParameters(this.QueryParameters);
string
FirstName = QueryParameters["FirstName"];
return
o => (o.FirstName.StartsWith(FirstName));
}
}
}
|
Nice post. You might want to wrap your loop that builds the parameter dictionary "if (item[0] != '$')" to exclude the standard oData parameters from the list. Also, I am providing an alternate method for passing parameters from the client below. this took me a while to find so it might help other readers.
ReplyDeleteTraderList = new DataServiceCollection();
var dataService = ServiceLocator.Current.GetInstance();
var query = dataService.Traders.Expand("Trader")
.AddQueryOption("traderType","pm")
.OrderBy(o => o.ScreenName);
TraderList.LoadAsync(query);
Hi Ronald
ReplyDeleteThanks for your feedback , updated the post with your input
Ashwini
Thanks for sharing such an informative article.
ReplyDeleteThanks
Cpa offers
You have written a very informative article with great quality content and well laid out points. I agree with you on many of your views and you've got me thinking.
ReplyDeleteMicrosoft Windows Azure Training | Online Course | Certification in chennai | Microsoft Windows Azure Training | Online Course | Certification in bangalore | Microsoft Windows Azure Training | Online Course | Certification in hyderabad | Microsoft Windows Azure Training | Online Course | Certification in pune
MMORPG OYUNLARI
ReplyDeleteinstagram takipçi satin al
TİKTOK JETON HİLESİ
tiktok jeton hilesi
antalya saç ekimi
İNSTAGRAM TAKİPÇİ SATIN AL
Instagram takipçi satın al
metin2 pvp serverlar
instagram takipci satın al
En son çıkan perde modelleri
ReplyDeletesms onay
Mobil ödeme bozdurma
nftnasilalinir
ankara evden eve nakliyat
trafik sigortası
dedektör
web sitesi kurma
aşk kitapları
Smm panel
ReplyDeleteSmm Panel
iş ilanları
İnstagram takipçi satın al
hırdavat
WWW.BEYAZESYATEKNİKSERVİSİ.COM.TR
SERVİS
tiktok jeton hilesi
nft nasıl alınır
ReplyDeleteen son çıkan perde modelleri
minecraft premium
uc satın al
en son çıkan perde modelleri
yurtdışı kargo
özel ambulans
lisans satın al
i'm unequipped for perusing articles online especially every now and again, but Im cheerful I did these days. it is chosen dexterously composed, and your focuses are skillfully communicated. I request you agreeably, engage, dont at any point lower composing.! Sai 2 Crack
ReplyDeleteEpubor Ultimate Crack Download is a solitary of the top of the line use of it's sort. This application will help you to change over your records! Thums Up Epubor Ultimate
ReplyDeleteSweet Christmas Wishes For Him · With a guy like you by my side, my life is finally more full, sparkling, and merry than the Rockefeller Center's .
ReplyDeleteLoved Ones Christmas Wishes