O'Reilly Forums: Custom Service Operations - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Custom Service Operations

#1 User is offline   JohnPapa 

  • New Member
  • Pip
  • Group: O'Reilly Author
  • Posts: 3
  • Joined: 17-September 08

Posted 30 December 2008 - 06:44 PM

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);


0

#2 User is offline   LDanberger 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 30-December 08

Posted 30 December 2008 - 08:19 PM

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


0

#3 User is offline   LDanberger 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 30-December 08

Posted 03 January 2009 - 01:49 PM

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;
      }


0

#4 User is offline   CezarT 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 17-February 09

Posted 17 February 2009 - 01:22 PM

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
0

#5 User is offline   yorlesa 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 25-August 12

Posted 25 August 2012 - 01:48 AM

I want to setup a small operation. I would like to ship packages of clothing (around 3 items at a time, 3 lbs at most) from Peru to the US weekly or bi-weekly.
Vivexin
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users