Jump to content


Photo

Custom Service Operations


  • Please log in to reply
7 replies to this topic

#1 JohnPapa

JohnPapa

    New Member

  • O'Reilly Author
  • Pip
  • 3 posts

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



#2 LDanberger

LDanberger

    New Member

  • Members
  • Pip
  • 2 posts

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



#3 LDanberger

LDanberger

    New Member

  • Members
  • Pip
  • 2 posts

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



#4 CezarT

CezarT

    New Member

  • Members
  • Pip
  • 1 posts

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

#5 yorlesa

yorlesa

    New Member

  • Members
  • Pip
  • 1 posts

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

#6 lnwpen88

lnwpen88

    New Member

  • Members
  • Pip
  • 1 posts

Posted 11 September 2014 - 06:55 AM

I created a dummy table with guid and nvarchar row.

#7 Mark jonson

Mark jonson

    New Member

  • Members
  • Pip
  • 2 posts

Posted 19 January 2015 - 10:24 PM

I created a dummy table with guid and nvarchar row.



This was helpful :rolleyes:

#8 tongclub18

tongclub18

    Active Member

  • Members
  • PipPip
  • 24 posts

Posted 12 March 2015 - 09:02 PM

I would like to gclub ship packages of clothing.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users