Skip to main content

Posts

Showing posts with the label OData

GraphQL and OData–Let’s discuss…

If you are following my blog, you should know that I’m a big fan of GraphQL. Of course you could wonder why am I not using OData instead? If you want to know my opinion on this topic, have a look at my previous post on this topic. But hey, it’s not about what I think, let’s hear from some of the people in the .NET community what they think about it. Therefore check out this .NET Data Community Standup: In the video Hassan Habib talks about a protocol agnostic OData spinoff: OData Neo .

GraphQL vs OData

In case you didn’t noticed yet, I’m a big fan of GraphQL. One of the questions I get a lot (especially from .NET developers) is what the difference is with OData? At first sight they have a lot of similarities and partially try to achieve the same goal, but there are some reasons why I prefer GraphQL over OData. Let’s first have a look at the “official” descriptions: From odata.org : OData (Open Data Protocol) is an ISO/IEC approved , OASIS standard that defines a set of best practices for building and consuming RESTful APIs. OData helps you focus on your business logic while building RESTful APIs without having to worry about the various approaches to define request and response headers, status codes, HTTP methods, URL conventions, media types, payload formats, query options, etc. OData also provides guidance for tracking changes, defining functions/actions for reusable procedures, and sending asynchronous/batch requests. OData RESTful APIs are easy to consume. The ODa...

ASP.NET Web API OData error: ValueFactory attempted to access the Value property of this instance.

After following the tutorial here; https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint I got the following error when I tried to run the OData endpoint: System.InvalidOperationException was caught   HResult=-2146233079   Message=ValueFactory attempted to access the Value property of this instance.   Source=mscorlib   StackTrace:        at System.Lazy`1.CreateValue()        at System.Lazy`1.LazyInitValue()        at System.Lazy`1.get_Value()        at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping()        at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver...

WCF Data Services: Expression of type 'System.Data.Entity.Core.Objects.ObjectContext' cannot be used for return type 'System.Data.Objects.ObjectContext'

If you try to create a WCF Data Service using the Visual Studio Template(see image below) with an Entity Model created using Entity Framework 6, you’ll end with an exception like the following: Expression of type 'System.Data.Entity.Core.Objects.ObjectContext' cannot be used for return type 'System.Data.Objects.ObjectContext' The problem is that the WCF Data Services template does not offer support for the new Entity Framework 6 classes. In EF6 the ObjectContext is completely decoupled from the .NET framework. As a result the class no longer lives in the same namespace explaining the exception above. WCF Data Services Entity Framework Provider This does not mean you cannot use WCF Data Services with Entity Framework 6. There is a new NuGet package called WCF Data Services Entity Framework Provider . This NuGet package bridges the gap between WCF Data Services 5.6.0 and Entity Framework 6+. Here are the things you need to do to get this working: In...

The remote certificate is invalid according to the validation procedure

When calling an OData service from inside my ASP.NET MVC application, the call always fails with the following exception: The remote certificate is invalid according to the validation procedure The OData service I’m connecting to is available through HTTPS so I would guess that something is wrong with the SSL certificate. Let’s walk through the checklist: Hostname matches the name used in the certificate: OK Certificate is not expired: OK Encryption level is high enough: OK The full certificate chain is trusted: OK Mmm, strange. Everything seemed fine. And when I browsed to the OData service directly from the browser, I could get all the data as I would expect. In the end I figured out that I had to give the MVC application access to the private key of the certificate. Following steps fixed the problem: Export the certificate from the OData Service host(don’t forget to export the private key as well). Open MMC and load the Certificates snap-in...

OData: create a query using AddQueryOption

I got a question from a customer who wanted to create an ‘'IN” query(something like SELECT * FROM Address WHERE Postcode IN (2000, 1100, 1940, 1745) ). This is something you cannot do out-of-the-box using OData(not that I’m aware of). A possible alternative is using the AddQueryOption and build the filter condition yourself: Remark: This only works if the list of elements is rather small.

Combining oData and a custom authentication module

If you are looking for a way to integrate oData and custom authentication, I can recommend the following article: Securing OData services using Basic Authentication . There is one thing that you should notice: The moment you start using a custom authentication module, it’s important that you configure IIS for Anonymous Access. Otherwise the built-in authentication modules will kick in even when your own custom authentication module is called. We forgot to change this and it took us some time to figure out why we always got a login dialog although the authentication module authenticated us successfully.

Validate your OData feed: The OData validator

You’ve created your own OData feed and want to check if it is following the OData Specifications? Check out the OData Service Validation Tool , an OData protocol validation tool. It contains a set of rules to validate against a given OData payload. How does it work? Go to http://services.odata.org/validation/ Pass the URL of your service, for example let’s use the following sample service: http://services.odata.org/v3/(S(igakql3jm4icp3bxv0my53co))/odata/odata.svc/ Choose the response type that you want to validate(AtomPub/XML, JSON-Verbose, JSON-Min,… ) Specify the OData version of your endpoint Click on Validate to send a request to the OData service. The tool will send a request to the service and run the right set of rules against the returned payload. For each rule a result is shown together with a link to the relevant spec section .

WCF Data Service: The type initializer for 'System.Data.Services.Client.TypeSystem' threw an exception.

In an application, we are consuming an OData feed. In development, this worked without any issue but the moment we deployed the application to our test environment, it started to fail with the following exception: The type initializer for 'System.Data.Services.Client.TypeSystem' threw an exception. Looking in the logs on the server, we found the following stacktrace: Sync.exe Information: 0 : Error occured during synchronization: The type initializer for 'System.Data.Services.Client.TypeSystem' threw an exception. Sync.exe Information: 0 : Stacktrace:    at System.Data.Services.Client.TypeSystem.IsPrivate(PropertyInfo pi)    at System.Data.Services.Client.ResourceBinder.PatternRules.MatchNonPrivateReadableProperty(Expression e, PropertyInfo& propInfo, Expression& target)    at System.Data.Services.Client.ResourceBinder.VisitMemberAccess(MemberExpression m)    at System.Data.Services.Client.ALinq...

OData: attach a client certificate through code when connecting to an OData service

At a customer, we had to use a 3th party OData service. Using OData feeds in .NET is simple, but this one was a little bit harder to use because it was secured using a client certificate. So how can we tell OData to include the certificate when connecting to the service? Open or create a Visual Studio project where you want to use the OData feed. Add a service reference to the OData feed using the Visual Studio Add Service Reference option. If the metadata url is also secured, you can use Fiddler to temporarily work around the security. A reference is generated for you and added to the Visual Studio project. Inside this reference you’ll find a partial class containing a DataServiceContext object: To include our client certificate, we can extend the generated class with some extra code. Therefore add a partial.cs file in the same namespace: Now we can create the DataServiceContext and include the certificate using the property we just added:

OData: attach a client certificate through Fiddler when connecting to an OData service

At a customer, we had to use a 3th party OData service. Using OData feeds in .NET is simple, but this one was a little bit harder to use because it was secured using a client certificate. We first wanted to browse through the OData feed using LinqPad but we couldn’t find a way to configure LinqPad to add the certificate to each request. Use Fiddler to include a client certificate with each request We decided to follow a different route and use Fiddler to include the certificate for us. Fiddler is a web debugging proxy that can intercept all the HTTP traffic we are doing on our system. Here are the steps you need to take: Get the client certificate. If you have a .pfx file including the private key, extract it and install the certificate in your personal certificate store. Download and install Fiddler in case you didn’t have it. Start Fiddler . Go to Tools –> Fiddler Options Go to the HTTPS Tab and check the checkbox next to Decrypt HTTPS traffi...

OData: include multiple children in a result set

Last week I got the following question: “I have an OData service that returns a set of Portfolios. Each portfolio also has a list of Sectors, a list of Technologies and a list of Customers. How can I query this OData feed and get all the results back in one request?” To achieve this we need to use the Expand() keyword Here is a sample how to do this in Linq; And here is the resulting url that returns the same results: http://odatasample/Portfolio()?$expand=Sectors,Technologies,Customers

WCF Data Service: Error processing request stream. The property name Discount specified for type Order is not valid.

For a project we are using  a WCF Data Service. By using “Add service reference” we generated all the classes and a context on the client side. To simplify the usage of these classes, we want to extend some of them with some extra properties, e.g: public partial class Order { public int Discount { get { return CalculateDiscount(); } } } After setting this up, we couldn’t no longer send updates to the server, instead we always got the following error message back:  "Error processing request stream. The property name 'Discount' specified for type 'Order' is not valid." How can we tell WCF Data Service to ignore this property? On the server side, doing this is easy. There is an IgnoreProperties attribute class that you can use to control the visibility of a property in a WCF DataService class. But this one is meant to be used on the server side not the client side. But with some extra code, you can get the same thing don...

JayData–The cross-platform HTML5 data-management library for JavaScript

Last week, I wanted to call an oData service from my HTML5 metro app. First I was planning to use datajs until I discovered JayData which offers similar functionality to datajs and much more! What’s JayData? JayData is a standards-based, cross-platform Javascript library and a set of practices to access and manipulate data from various online and offline sources. JayData provides JavaScript Language Query (JSLQ) as a tool to query local (in-browser and mobile) and remote data sources with a simple, unified query syntax: JavaScript itself. Much like Microsoft .NET developers can utilize LINQ to perform operations on data from different databases. The aim of JayData library is to give a similar level of abstraction with the help of the $data.Expressions API and the JavaScript language itself. JayData is cross-platform (runs on HTML5 desktop and mobile browsers, can be hosted in PhoneGap environment on iPhone , iPad and A ndroid ) and cross-layer as it works on client-side a...

Use Fiddler with the Windows Phone 7 emulator

If you didn’t know it yet, I’ve build my own WP7 application . As I’m using oData in the backend, Fiddler is an invaluable tool that helps me debugging all the web traffic. However there is one problem, Fiddler doesn’t seem to work with the Windows Phone 7 emulator. I solved the problem by executing following steps mentioned on Eric Laws blog : Start Fiddler. Click Tools > Fiddler Options. Open the Connections tab and tick the Allow remote computers to connect box Click OK to close the Fiddler Options dialog. In the QuickExec box under the session list, type prefs set fiddler.network.proxy.registrationhostname HostName where HostName is the name of your desktop computer. Close and restart Fiddler. Start (or restart) the Windows Phone 7 Emulator. Open Internet Explorer on the Emulator. Observe, your traffic shows in Fiddler.

Microsoft Web Camps Training Kit

The Web Camps Training Kit includes all the content presented around the world at the Web Camps events; presentations, demos, labs and more. Inside you'll find content that covers the following technologies ASP.NET MVC 3, jQuery, IE 9 and HTML5, OData, Web Apps, WebMatrix and more! You’ll find the training kit for download here: http://trainingkit.webcamps.ms/Default.htm

WCF vNext: Linq to WCF

One of the great new features coming to WCF vNext is the ability to expose a service through an IQueryable interface. This introduces the rich query model of OData to your own WCF services. How does this work? Making the service queryable On the server side, your service operation should return an IQueryable<T>. Annotate the operation with the new [QueryComposition] attribute. Once you do that, your service becomes queryable using the OData uri format. 1: [WebGet(UriTemplate = "" )] 2: [QueryComposition] 3: public IQueryable<Customer> Get() 4: { 5: return customers.AsQueryable(); 6: } The Get method above returns an IQueryable of customers. With the query composition enabled, the host will now accept requests like “http://localhost/customers?$filter=Countrye%20eq%20Belgium” which says “find me all the customers from Belgium”. Querying the service, LINQ to WCF On the client side Microsoft added a CreateQuery<T> extension ...

Debugging a WCF Data Service

Creating a WCF Data Service is really easy thanks to the built-in item template. Unfortunately when something goes wrong, a useless “The server encountered an error processing the request” message is returned. There are 2 important settings that help you to return useful error messages. 1. Add a WCF serviceDebug behavior with the IncludeExceptionDetailInFaults property set to true. This is especially useful when something failes during the WCF request pipeline and the WCF Data Service couldn’t be initialized. 1: < system.serviceModel > 2: < behaviors > 3: < serviceBehaviors > 4: < behavior > 5: <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 6: < serviceDebug includeExceptionDetailInFaults ="true" /> 7: </ behavior > 8: </ serviceBehaviors > 9: </ behaviors ...

Community Day session: Building an enterprise application with Silverlight and NHibernate

At Community Day 2010, together with Gill Cleeren, I gave a session called Building an enterprise application with Silverlight and NHibernate. We not only showed Silverlight and NHibernate but also looked into topics like CQRS(Command-Query Responsibility Seggregation), OData, MVVM, and so on. Building an enterprise app in silverlight 4 and NHibernate View more presentations from bwullems . Our demo application had a lot more features than we could show, so certainly have a look at the code .

OData: Using the reflection based provider

If you start implementing WCF Data Services, sooner or later you will certainly see following error when calling the OData feed: “On data context type 'XXXXX’, there is a top IQueryable property 'XXXX' whose element type is not an entity type. Make sure that the IQueryable property is of entity type or specify the IgnoreProperties attribute on the data context type to ignore this property.” This can mean two things. Either you have a property that cannot be processed by the Entity Data Model used by OData or OData could not find a primary key for your class. OData requires a primary key for every resource. If you use the reflection based provider, it uses one of the following mechanisms to find the primary key: via the [DataServiceKey] attribute by looking for a "FooID" property, where the type is called "Foo" by looking for an "ID" property Important to know is that this convention is case-sensitive: "FooId" won't work without...