-
Notifications
You must be signed in to change notification settings - Fork 5
Installing MongOData using NuGet package
If you want to embed MongoDB OData feed into your Web service or application, you can use MongOData NuGet package. NuGet package adds custom WCF Data Service to your Web component, so you only need to specify the MongoDB connection string (MongOData NuGet package does not support wild cards, you will have to refer to a specific database). Here is what you need to do.
- Create a Web project using one of Visual Studio templates, for example, “ASP.NET MVC3 Web Application” (almost any generic ASP.NET Web application template will do).
- Open Package Manager Console window and write the following command:
Install-Package MongOData
NuGet package manager will install the latest MongOData component along with its dependency mongocsharpdriver, so you will see the package installation report similar to this:
Attempting to resolve dependency 'mongocsharpdriver (≥ 1.4.1)'. Successfully installed 'mongocsharpdriver 1.4.2'. Successfully installed 'MongOData 0.4.0'. Successfully added 'mongocsharpdriver 1.4.2' to MongODataTest. Successfully added 'MongOData 0.4.0' to MongODataTest.
If you check the project content, you will see that there are references to MongoDB driver and MongOData assembly files, and two new source files MongoDataService.cs and MongoDataService.svc. Here is the content of the newly added C# source file:
public class MongoDataService : MongoQueryableDataService { public MongoDataService() : base(ConfigurationManager.ConnectionStrings["MongoDB"].ConnectionString) { }
// 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.
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.DataServiceBehavior.AcceptCountRequests = true;
config.DataServiceBehavior.AcceptProjectionRequests = true;
config.UseVerboseErrors = true;
}
}
Note that MongoDataService refers to a connection string named as "MongoDB". If you are comfortable with this, you don't need to do any modification to the source files, just update the connection string in web.config file so it points to the actual MongoDB database that you are going to expose as OData feed.
- Build the project and go the MongoDataService.svc page, you will see something like this:
You're done! Now you can query your MongoDB OData feed using standard OData V3 URI convention.