ODataConventionModelBuilder with multiple namespaces - odata

This is either super straight forward, or its relatively easy to answer. I have the following code in order to set up my OData Routing Conventions:
// OData
var builder = new ODataConventionModelBuilder();
// OData entity sets..
builder.EntitySet<Book>("Books");
builder.EntitySet<Shelf>("Shelves");
// Bound Function..has to be located on the Tables Controller...
builder.Namespace = "BookService";
builder.EntityType<Table>().Collection
.Function("MostRecent")
.Returns<DateTimeOffset>();
builder.Namespace = "ShelfService";
builder.EntityType<Shelf>()
.Action("NearestEmptyShelf");
...But the problem with this is when the application starts, everything is routed against ShelfService rather than the first function being accessible from BookService.MostRecent and ShelfService.NearestEmptyShelf.
I'm sure others have run into this particular problem when creating services (actions/functions) for their OData Controllers. But I'm just after a definitive answer as to whether or not you can have multiple namespaces in the OData Routing Collection?

You are overwriting your namespace of builder.Namespace = "Bookservice"; with builder.Namespace = "ShelfService";.
To utilize two separate namespaces you need two separate instances of new ODataConventionModelBuilder();
The below is for OData V4
// Book OData Endpoint
var book_builder = new ODataConventionModelBuilder();
// Book OData entity sets..
book_builder.EntitySet<Book>("Books");
// Book Bound Function..has to be located on the Tables Controller...
book_builder.Namespace = "BookService";
book_builder.EntityType<Table>().Collection
.Function("MostRecent")
.Returns<DateTimeOffset>();
// Book Config
config.MapODataServiceRoute(
routeName: "OData - Book",
routePrefix: "book",
model: book_builder.GetEdmModel()
);
// Shelf OData Endpoint
var shelf_builder = new ODataConventionModelBuilder();
// Shelf OData Entity Sets
shelf_builder.EntitySet<Shelf>("Shelves");
// Shelf Bound Function..has to be located on the Tables Controller...
shelf_builder.Namespace = "ShelfService";
shelf_builder.EntityType<Shelf>()
.Action("NearestEmptyShelf");
.Returns<whatever you planned on returning>()
//Shelf Config
config.MapODataServiceRoute(
routeName: "OData - Shelf",
routePrefix: "shelf",
model: shelf_builder.GetEdmModel()
);
It has been a while since I've implemented this mechanism, but you may have to overwrite AttributeRoutingConvention to utilize bound functions in multiple namespaces/controllers using the above method. I know I had a hiccup with it at some point and ended up finding a good method on stack overflow for a public class CustomAttributeRoutingConvention : AttributeRoutingConvention that utilized a public static class HttpConfigExt to provide a CustomMapODataServiceRoute to fix the issue.

It's been a while since this was answered, but I got into this problem recently and found an alternate solution so there it goes...
...
builder.Namespace = "Namespace_A"; // This would be the default namespace
...
function = builder.EntityType<EntityA>()
.Collection
.Function("FunctionInNamespace_B")
.ReturnsCollection<EntityB>();
function.Namespace = "Namespace_B";
Absolutely simple and works like a charm.

Related

Dynamic binding model for use in HttpClient

I have a Web Api that takes a complex object and adds it to the database.
var myWidgit= new Widgit() {
Name = "WidgitName",
Price = 50,
Category = "Appliance" };
HttpResponseMessage response = client.PostAsync("api/createwidgit", myWidgit);
I need to make a one off call to the API and I would like to avoid creating a separate class file for Widgit.
Is there a way to define the Widgit class and assign it values in the method that makes use of it? Sort of like a dynamic class just used in this method.
I think I figured it out ...
HttpResponseMessage response = client.PostAsync("api/createwidgit", new StringContent(string.Format("Name={0}&Price={1}&Category={2}", HttpUtility.UrlEncode("WidgitName"), HttpUtility.UrlEncode(50), HttpUtility.UrlEncode("Appliance"), Encoding.UTF8, "application/x-www-form-urlencoded"));
Essentially the dynamic object I was looking to create is accomplished using
new StringContent(string.Format("Name={0}&Price={1}&Category={2}", HttpUtility.UrlEncode("WidgitName"), HttpUtility.UrlEncode(50)), HttpUtility.UrlEncode("Appliance"), Encoding.UTF8,
"application/x-www-form-urlencoded")
Sorry I could not figure out better formatting

How to inject DbContext in a custom localization provider in ASP.NET Core?

As explained in the asp.net core docs you can configure a custom provider for request localization. As stated in the docs:
Suppose you want to let your customers store their language and culture in your databases. You could write a provider to look up these values for the user.
For that the following code snippet is provided in the docs and also in the github sample Localization.StarterWeb:
services.Configure<RequestLocalizationOptions>(options => {
var supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr")
};
options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
{
// My custom request culture logic
// DbContext needed here <--
return new ProviderCultureResult("en");
}));});
Can anybody explain me how to inject a DbContext to load the user specific language from DB in the above function?
Well, you can't inject it via constructor because you need to instantiate it during ConfigureServices method and the container isn't available at this point.
Instead you can resolve via HttpContext.
public class CustomRequestCultureProvider : RequestCultureProvider
{
// Note we don't inject any dependencies into it, so we can safely
// instantiate in ConfigureServices method
public CustomRequestCultureProvider() { }
public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
{
var dbContext = httpContext.RequestServices
.GetService<AppDbContext>();
}
}
Be aware though that this may be less than optimal, as you'll have calls to database on every request, so maybe it's worth to abstract this further and use an caching strategy depending on what exactly you want to do with the DbContext.
Usually one should avoid database calls in culture providers, filters etc. for performance reasons
Update:
There is a generic version of GetService<T>, but you need to import the namespace via using Microsoft.Extensions.DependencyInjection;.

Web API 2 Odata 4 Parameter Issue

I'm having problems defining a function for odata4. The default get would work but I want to require a user parameter so a client set can be determined, other tables are involved so LINQ is required, I also return a DTO instead of the default table info (EF). Below is the code. I get a "Invalid EntitySetPath detected. 'bindingParameter/Client' is not a valid entity set path for procedure 'Default.GetClients'." What am I doing wrong here?
WebApiConfig
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<client>("Client").EntityType.HasKey(p => p.int_id);
var function = builder.Function("GetClients");
function.Parameter<string>("user");
function.ReturnsCollectionFromEntitySet<client>("Client");
builder.EntitySet<ClientDTO>("ClientDTO");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
WebApp.Controller
[ODataRoute("GetClients(user={user})")]
[EnableQuery(PageSize=25)]
public IQueryable<ClientDTO> GetClients([FromODataUri] string user)
{
var clients = (from c in db.clients
join ...
If your OData controller is returning the DTO, the function should look like this:
var function = builder.Function("GetClients");
function.Parameter<string>("user");
function.ReturnsCollectionFromEntitySet<ClientDTO>("Client");
With your current setup, your OData route of GetClients says that it is returning a ClientDTO object, but your WebApiConfig is stating you are returning a Client object.
As the Entity Collection being returned is actually the DTO. The part that shows ("Client") is simply how the OData service will report the name of the object to the project consuming the OData service. For my own personal sanity, I typically include DTO as well so I know when I'm using a DTO and when I'm using a direct entity. So in my own setup i'd return ("ClientDTO"), just a personal preference.

Adding a service reference to a breeze odata service

Is it possible to consume an OData service (implemented using .Net MVC) with Breeze controllers?
I tried adding a Service Reference from a client aplication, but it simply cannot find a service endpoint when I use Breeze controller on the service.
Any help will be appreciated.
Yes, on the server you will need to create a WCF DataService, something like this:
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class ODataService : DataService<Your_EF_DbContext> {
// Add your Entity Set names here ... for example
config.SetEntitySetAccessRule("Customers", EntitySetRights.All);
config.SetEntitySetAccessRule("Orders", EntitySetRights.All);
config.SetEntitySetAccessRule("Employees", EntitySetRights.All);
// V3 supported in our next release as well.
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.UseVerboseErrors = true;
}
Then from the Breeze client you will need to call
breeze.config.initializeAdapterInstance("dataService", "OData");
to initialize Breeze's OData handling. Then you create an EntityManager and connect to your service. Something like this:
var myEntityManager = new breeze.EntityManager("http://localhost:9009/ODataService.svc");
You can now query and save from your data service via the EntityManager.

Using Ninject.MockingKernel with Asp.Net Web API

I've set up a Web API project using Ninject, and I've used the fix detailed here for getting it to work with the latest version of the Web API. Everything is working fine, but I'm now trying to write some tests.
I'm using in-memory hosting to run the project for the tests, as detailed here, as I have a DelegatingHandler that performs authentication and then sets a property on the request message that is used by all the Api Controllers.
So, I've got a base class for my tests, and have a SetUp method where I set up the HttpServer and configuration, which I've pretty much taken from my working Ninject code:
[SetUp]
public void Setup()
{
bootstrapper = new Bootstrapper();
DynamicModuleUtility.RegisterModule(
typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(
typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
var config = new HttpConfiguration();
config.Routes.MapHttpRoute("Login",
"api/auth/token",
new { controller = "Users", action = "Login" });
config.IncludeErrorDetailPolicy =
IncludeErrorDetailPolicy.Always;
config.DependencyResolver =
new NinjectResolver(CreateKernel());
config.MessageHandlers.Add(
new AuthenticationHandler(CreateUserManager()));
Server = new HttpServer(config);
}
This is how I create the MoqMockingKernel:
private static IKernel CreateKernel()
{
var kernel = new MoqMockingKernel();
kernel.Bind<Func<IKernel>>()
.ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>()
.To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver =
new NinjectResolver(kernel);
return kernel;
}
And this is how I register the objects to use:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IUserManager>().ToMock();
kernel.Bind<UsersController>().ToSelf();
}
While I'm not testing the Controller per se, I do want a proper instance of it to be called, which is why I'm binding it ToSelf. I must admit that I am assuming that this is correct. This is an example of a test:
public void UserCannotLogin()
{
System.Net.Http.HttpClient client =
new System.Net.Http.HttpClient(Server);
string json = string.Format(
"{{ \"Username\": \"{0}\", \"Password\": \"{1}\" }}",
"wrong", "wrong");
HttpRequestMessage request =
CreateRequest(#"api/auth/token", json, HttpMethod.Get);
Action action = () => client.SendAsync(request);
using (var response = client.SendAsync(request).Result)
{
response.StatusCode.Should()
.Be(HttpStatusCode.Unauthorized);
}
}
I'm basically getting a 404 error. When I debug it, it does go to my DelegatingHandler, but it doesn't go to my controller.
I get the feeling that I'm fundamentally missing a point here, and it may not even be possible to do what I'm trying to do, but if anyone has any suggestions for either how to do this, or a different way to achieve the same thing, I'm all ears.
Update I think that it's because the default behaviour of the MockingKernel is to provide a Mock unless told otherwise, so it is returning a Mock of IHttpControllerSelector. I've set up a couple of default ones now:
kernel.Bind<IHttpControllerSelector>()
.To<DefaultHttpControllerSelector>();
kernel.Bind<IContentNegotiator>()
.To<DefaultContentNegotiator>();
It's still not working, I think because there are no formatters specified. I'll try that tomorrow and see if that gets me there.
Ok, I think that I was correct when I said that I was fundamentally missing a point here, but I'll answer this in case it helps someone else avoid the same mistake!
The Ninject MockingKernel is, I think, primarily about auto-mocking, so where you have a lot of interfaces you don't care about how they are set up in your test, you can ignore them in your tests and they will be automatically created for you.
In the case of the Web API, this is most definitely not the case, as you don't want the controller selector class to be auto mocked, otherwise you won't end up calling your controllers.
So, the solution I've come up with is to stick with using a standard Ninject Kernel, and then bind your interface to a constant Mock object:
kernel.Bind<IUserManager>().ToConstant(CreateUserManager());
private IUserManager CreateUserManager()
{
Mock<IUserManager> userManager = new Mock<IUserManager>();
// Set up the methods you want mocked
return userManager.Object;
}
Doing this, I've been able to successfully write tests that use an HttpClient to call an in-memory HttpServer that successfully call my DelegatingHandler and then end up at my controllers.

Resources