Consuming WCF Data Services Action Provider for Entity Framework -
i have dataservice class on top of entity framework 5 dbcontext:
public class mydataservice : dataservice<mydbcontext>, iserviceprovider { [webget] public iqueryable<product> getproducts1(int category) { return p in this.currentdatasource.products p.category == category select p; } }
now want expose methods dbcontext dataservice , used sample code: http://efactionprovider.codeplex.com/
public class mydbcontext : dbcontext { public dbset<product> products { get; set; } [nonbindableaction] public iqueryable<product> getproducts2(int category) { return p in this.currentdatasource.products p.category == category select p; } }
accessing http://localhost:12345/mydataservice.svc/$metadata show both methods known, first 1 hat a
m:httpmethod="get"` attribut
<entitycontainer name="mydbcontext" m:isdefaultentitycontainer="true"> ... <functionimport name="getproducts1" returntype="collection(mynamespace.product)" entityset="products" m:httpmethod="get"> <parameter name="category" type="edm.int32" nullable="false"/> </functionimport> <functionimport name="getproducts1" returntype="collection(mynamespace.product)" entityset="products"> <parameter name="category" type="edm.int32" nullable="false"/> </functionimport> ...
i can execute getproducts1 accessing url
http://localhost:12345/mydataservice.svc/getproducts1?category=1
that doesn't work getproducts2 (propably because of not allowed) managed execute getproducts2 using fiddler:
post: http://localhost:12345/mydataservice.svc/getproducts1 request headers: user-agent: fiddler host: localhost:12345 content-length: 12 content-type: application/json request body: {category:1}
ok, here problem: consume service windows application using service reference. since code generation in dataservicecontext
derived class not cover actions need call them myself.
for first 1 (getproducts1
) can do:
public ienumerable<product> getproducts1(int category) { var proxy = new mydataservicecontext( "http://localhost:12345/mydataservice.svc"); var querystring = string.format("{0}/getproducts1?category={1}", proxy.baseuri, category); var uri = new uri(querystring, urikind.relativeorabsolute); return proxy.execute<product>(uri); }
but struggling second one. tried:
public ienumerable<product> getproducts2(int category) { var proxy = new mydataservicecontext( "http://localhost:12345/mydataservice.svc"); var querystring = string.format("{0}/getproducts2", proxy.baseuri); var uri = new uri(querystring, urikind.relativeorabsolute); return proxy.execute<product>(uri, "post", false, new urioperationparameter("category", category)); }
but dataserviceclientexception: content-type-header value missing.
(status-code 400)
is there way call method using execute method? prefer continue using dataservicecontext
, not make raw requests myself.
thanks in advance
btw.
i using visual studion 2010 , microsoft.data.services
, microsoft.data.services.client
packages nuget rather default system.data.services
dlls, because believe need set config.dataservicebehavior.maxprotocolversion = dataserviceprotocolversion.v3;
executing actions in first place.
try using bodyoperationparameter instead of urioperationparameter. action parameters included in request body instead of uri.
Comments
Post a Comment