locked
ADO.NET SERVICE CROSS DOMAIN RRS feed

  • Question

  • Hi,

    I have added an ADO.NET Service (Customer.svc) to my web site and will be using the Microsoft Ajax Library to call this web service and pass a JSON object to it.

    I need the Customer.svc service located on my web site to call another ADO.NET Service that will be located on another domain so that the service on the other domain can insert the data into their own database using the ENTITY to SQL Framework.

    What I cannot figure out is how I will call the other web service located on the other domain from my local service .

    With ASMX web services this was easy. We would just add a ASMX web service to our web site and then we would add a web reference to a web service located in the other domain and instantiate this web service inside our web service.

    This is what the service in the other domain looks like currently

    public class CustomersService : DataService<CustomersModel.CustomersEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(IDataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        }
    }


    I do not know what to add to my local .svc service. I cannot reference the ENTITY class here since this service will
    not be inserting any data to the database. I just need to use it as a facade service.

    public class Customers : DataService< to do >
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(IDataServiceConfiguration config)
        {
           
        }
    }

    This is how I am going to call my service from my javascript code:

    dataService = new Sys.Data.AdoNetServiceProxy("Customers.svc");
    dataService.query("Contact");

    What am I missing here? Have I misunderstood the way to use ADO.NET services? It is goint to be a shame if we cannot access other web services in other domain with ADO.NET Services.

    I would appreciate very much if someone could shed some light and show us some code example or point us to an online tutorial

    Cheers

    C


    Thursday, December 17, 2009 5:43 PM

Answers

All replies

  • Hi,

    So you have a service A which hosts the working Data Service. And then you have a site B which hosts the HTML with the java script and you want to be able to call A from that java script. Right?
    As you note you will need to create a "proxy" in site B. Your code above looks like you would like to have a DataService working on top of another DataService. Currently this is rather hard to implement. On the other hand you don't need a full blown data service on B. All you need is a true proxy there. So you could create a WCF endpoint in site B which simply forwards all incomming requests to the service A and when it gets a response it also forwards that back to the client. It doesn't need to understand the payload of the requests. (Note that to be 100% correct it would also need to propagate errors as well). The OData (the protocol used by Data Service) is definitely designed to work correctly with proxies between the client and the server. You could in fact even add some caching if you would like to (it's a correct REST, so you can cashe the GET responses and so on).

    Thanks,
    Vitek Karas [MSFT]
    Thursday, December 17, 2009 10:50 PM
    Moderator
  • Hi Vitek Karas,

    Many thanks for your reply. I am so glad that you replied. I thought noboy would know the asnwer for my question.

    You said:

    "On the other hand you don't need a full blown data service on B. All you need is a true proxy there. So you could create a WCF endpoint in site B which simply forwards all incomming requests to the service A and when it gets a response it also forwards that back to the client. It doesn't need to understand the payload of the requests. (Note that to be 100% correct it would also need to propagate errors as well). "


    Unfortynately, I do not know how to create a WCF endpoint in site B which simply forwards all incomming requests to the service A and when it gets a response it also forwards that back to the client. I only know how to implment an ADO.NET service that uses an ENTITY framework to get data.

    I would appreciate very very much if you could post some sample code so that we could learn how to create the WCF endpoint which forwards all incoming requests to the service located on another domain and also gets the response back to the client.

    How will I reference and instantiate the service on the other domain from the WCF enpoint on my web site?


    Cheers

    C






    Thursday, December 17, 2009 11:08 PM
  • Cross Domain calls from an Ajax application are tricky with any kind of Service , be it Web Services or WCF Services.

    One approach is to build a Server-side proxy as Vitek suggests , another way is to use JSONP to setup a callback for a Data Service call.

    Pablo built a WCF extension on top of Astoria that helps add JSONP support for Astoria Services.
    Read about it here :
    http://blogs.msdn.com/pablo/archive/2009/02/25/adding-support-for-jsonp-and-url-controlled-format-to-ado-net-data-services.aspx

    I dont know if the current AdoNetServiceProxy javascript client library supports JSONP natively , but there are some tutorials available to help with this :

    JSONP made easy in ASP.NET AJAX

    I'll look around internally to see if there is a way to make the AdoNetServiceProxy that ships with the ASP.NET Ajax Library use JSONP to make Data Service calls.
    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Friday, December 18, 2009 3:48 PM
    Moderator
  • Hi Phani Raj,

    Thanks for your reply.

    The way to do it with ASMX was very easy.

    We would create two ASMX web services. One as part of the web application (the facade or pass through web service) and another web service that would be deployed to another domain and have all the logic (code) to retrieve, updated, insert and delete from DB.

    So the facade ASMX service i.e. the web service that was part of the web site would just pass the calls and receive the response from the other web service. It would work as a proxy between my web site and the real web service on another doamin.

    I should be possible to use the same technique with .SVC.

    Thanks for your help.

    Cheers

    C
    Friday, December 18, 2009 4:07 PM
  • Hi

    Could you elaborate a bit more on this statement?

    "One approach is to build a Server-side proxy as Vitek suggests "

    How would I do that with an SVC service? If I understand this statement right, it should be something very similiar to what I used to do with ASMX services.

    Cheers

    Friday, December 18, 2009 4:10 PM
  • This and this post cover making an actual proxy service.

    I would attempt to create a simple proxy for the HTTP requests, forwarding on the headers, cookies, content, etc.  I think the Data Service payload will still have the originating domain for the relationship links, but it's been a while since I dealt with it.  Worst case in that situation is you'll need to swap out some domain names.

    In either situation, you should be able to target the proxy the same as any other service.  The proxy service would expose the same contract as the real service, and the HTTP proxy would just forward the requests/replies blindly between service and client.  I would do an HTTP proxy; simplest solution and more reusable.
    Friday, December 18, 2009 7:26 PM
  • Hi,

    The technique of layering one data service over the other as described in the second blog post mentioned by Nick is a bit tricky. It will only work for simple enough requests. If you want the "proxy" service to support all the features of the protocol (like complex $filter expressions for example) it gets really complicated. But if you know that your queries are going to be simple, it might work for you.
    The second part of that blog post which talks about IExpandProvider will also only work sometimes. With the release of V2 we now support projections ($select) for which the IExpandProvider doesn't work (in fact if you implement IExpandProvider and use $select in the query, the request will fail always). Solving this also requires some rather complicated code.
    I think the plain HTTP proxy solution is better for your case as you don't want to modify the service in any way, you just need to forward the requests. The problem with the different domain might not be a big one. At the end you can always do some kind of string replace which might work for you (it's not bullet proof though).

    Thanks,
    Vitek Karas [MSFT]
    Friday, December 18, 2009 9:15 PM
    Moderator
  • Hi Nick-Williams,

    Thanks for your post. I will read the posts you mentioned and see if I can understand how to create the server proxy.

    Cheers,

    C
    Friday, December 18, 2009 9:58 PM
  • Hi Vitek Karas, 

    Thanks again for your info.

    You said

    "I think the plain HTTP proxy solution is better for your case as you don't want to modify the service in any way, you just need to forward the requests"

    Could you please show a simple example of how to build this plain HTTP proxy solution? and how to reference the real SVC service on the other domain. How will I pass the request from the HTTP proxy to the SVC server? How will I get the response back to the aspx client?

    You are right, I do not need anything complicated for my proxy. I only need it to be able to pass the request and get the response from the real SVC and then send it back to the aspx client.

    Cheers

    C
    Friday, December 18, 2009 10:03 PM
  • Hi,

    I'm sorry, but I don't know how to do an HTTP proxy in WCF. Instead of me trying to figure it out, I think you would be much better of asking this question on the WCF forums: http://social.msdn.microsoft.com/Forums/en-US/wcf/threads

    Thanks,
    Vitek Karas [MSFT]
    Saturday, December 19, 2009 4:23 PM
    Moderator