Custom Service Operations |
![]() ![]() |
Custom Service Operations |
Dec 30 2008, 06:44 PM
Post
#1
|
|
|
|
This topic was posed on my web site, so I thought I might move it here in case anyone else had the same question ..... here is the chain ....
----------------------------------------- Larry D on 12.30.2008 at 4:55 PM Hi John, everyone else: following the ADO.Net Data Services (chpt 11) approach, I'd like to add a service that returns the current user identity from the server. In NorthwindDataService.svc.cs (EFDataServices) I thought I could just add [WebGet] public string CurrentUser() { return HttpContext.Current.User.Identity.ToString(); } However it does not show up, I'm assuming due to it not being IQueryable. Is there a recommended approach to adding services such as this to the ado.net data services? The example in the book for CustomersByCity is IQueryable. thanks, -Larry ----------------------------------------- Larry D on 12.30.2008 at 7:38 PM Hi John, the WebGet CustomersByCity doesn't appear to be working for me (from chapter 11 EFDataServices NorthwindDataService.svc.cs). I get 404 resource not found when trying as per the inline doc. Also when browsing to NorthwindDataService.svc, should CustomersByCity appear with the other endpoints listed? No errors when compiling, and I'm using the code as downloaded. suggestions? thanks, -Larry ----------------------------------------- John Papa on 12.30.2008 at 9:40 PM Larry, The CustomersByCity method will not appear as a proxy method explictly. Instead, it is accessible via a RESTful URI off of the EntityContainer. For example: http://localhost:8890/NorthwindDataService...#39;London' This will invoke the CustomersByCity method in the service. You would also need to allow permission to the web method using this line of code (which I commented out in the sample code, so you can just uncomment it ) config.SetServiceOperationAccessRule("CustomersByCity", ServiceOperationRights.All); Finally, make sure Customers entityset is accessible by doing this: config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead); |
|
|
|
Dec 30 2008, 08:19 PM
Post
#2
|
|
|
|
QUOTE Finally, make sure Customers entityset is accessible by doing this: config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead); This was what I was missing for EFDataServices. Without this (and the config.SetServiceOperationAccessRule... line uncommented) the app fails in ProductView (index out of range). I haven't figured out the other part of the question yet though (returning the user identity). thanks, Larry |
|
|
|
Jan 3 2009, 01:49 PM
Post
#3
|
|
|
|
Hi John,
in case you were loosing sleep over the second question (regarding how to return useridentity via ado.net data services). okay it's a hack but all I could figure out: I created a dummy table with guid and nvarchar row, then a routine and a WebGet routine that takes as parameter a string indicating what environment varb (could be extended to any non table/bo specific function) I want. seems to work locally but I haven't sorted out deploying on server yet. CODE [WebGet] public IQueryable<Dummy> DummyByQuestion(string question) { Guid id = System.Guid.NewGuid(); ServerUtility.WriteDummy(id,question); var query = from d in CurrentDataSource.Dummys where d.DummyID == id select d; return query; } ... static public bool WriteDummy(Guid dummyID,string question) { // example http://localhost:8666/xxService.svc/DummyByQuestion?question=%27HostAddress%27 bool bSuccess = false; string result = ""; switch (question) { case "UserIdentity": result = HttpContext.Current.User.Identity.Name; break; case "HostAddress": result = HttpContext.Current.Request.UserHostAddress; break; default: result = "unknown request"; break; } using (var cn = new SqlConnection(xCn)) { try { var cmd = new SqlCommand("Insert INTO Dummy (DummyID, DummyValue) VALUES (@DummyID, @DummyValue) ", cn); cmd.Parameters.AddWithValue("@DummyID", dummyID.ToString()); cmd.Parameters.AddWithValue("@DummyValue", result ?? "null"); cn.Open(); cmd.ExecuteNonQuery(); bSuccess = true; } catch (Exception ex) { Debug.WriteLine(string.Concat("save failed: ", ex.Message)); bSuccess = false; } } return bSuccess; } |
|
|
|
Feb 17 2009, 01:22 PM
Post
#4
|
|
|
|
Hi John,
I have one question: why in my Reference.cs I have "DataServiceQuery<Product> Product" instead of "DataServiceQuery<Product> Products"? This happens, of course, for any occurence of Products (in my case, Product). This thing seams to be very confusing for statements like "var qry = from p in Context.Product". Thank you |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 22nd November 2009 - 10:54 PM |