开发者

How can I "wrap" a Linq2Sql data context in a webservice?

Relevants: Visual Studio 2010, WCF WebServices, Linq2Sql, .Net 4.0

Forgive me if this is a stupid question, but I believe I have seen something similar to what I'm trying to do.

We have a Linq2Sql data context we are using to read/write to a database. We'd like to expose a public API to the database for certain vendors. I seem to recall a way to expose most of the data context functionality through a web service without creating wrappers for all the CRUD methods manually. Anybody out there know how to do this? I seem to remember seing the data context exposed through a web service with just a few lines of code.

We would开发者_JAVA百科 also need to deny access to some methods, based on permissions, but can probably work through that ourselves.


Not sure what you saw or refer to... but WCF Data Services seem to be what you are after - check them out at http://msdn.microsoft.com/en-us/data/bb931106.aspx


@Yahia got me pointed in the right direction. This article is nearly what I was looking for, but deals with ADO.Net Entity Data Models rather than LINQ data contexts. Very nearly there... Data Access with ADO.Net Data Services.

So, it turns out the answer is pretty simple.

  1. Create a new ASP.Net web application.
  2. Right-click the web application.
  3. Add --> New Item --> WCF Data Service.
  4. Modify the code to support your context:

    using System.Data.Services;
    using System.Data.Services.Common;
    
    namespace DelvingWare.Data.FilestreamServer.WebServices
    {
        public class WcfDataService1 : DataService<MyDataContext>
        {
            // This method is called only once to initialize service-wide policies.
            public static void InitializeService(DataServiceConfiguration config)
            {
                // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
                // Examples:
                config.SetEntitySetAccessRule("MyEntitySet", EntitySetRights.ReadSingle);
                config.SetServiceOperationAccessRule("MyEntitySet", ServiceOperationRights.ReadSingle);
                config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            }
        }
    }
    
  5. Build --> Right Click Service --> View In Browser. Voila!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜