Search code examples
sslf#odatatype-providers

ODataService Type Provider error: (401) Unauthorized


The type provider 'Microsoft.FSharp.Data.TypeProviders.DesignTime.DataProviders' reported an error: Error reading schema. The remote server returned an error: (401) Unauthorized.

Is there a way to use the OData type provider with an OData service which requires a username and password?

Static type parameters for the type provider:

  • ServiceUri : string The URI string for the OData service.
  • LocalSchemaFile : string The path to a file that contains the schema. This file is written by the type provider.
  • ForceUpdate : bool Requires that the direct connection to the service is available at design/compile time and the local service file is refreshed. The default value is true. When ForceUpdate is false, the provider reacts to changes in the LocalSchemaFile.
  • ResolutionFolder : string A folder to be used to resolve relative file paths at compile time. The default value is the folder that contains the project or script.
  • DataServiceCollection : bool Generates collections derived from DataServiceCollection. The default value is false.

Solution

  • Yes, but unfortunately it's not quite as slick, and you don't get compile-time validation, which is one of the nice benefits of type providers.

    You need to grab the $metadata from your service and save it locally as a .csdl file, then use the LocalSchemaFile static parameter in your code. You can then set credentials on the data context object in order to authenticate at runtime.

    // download http://services.odata.org/Northwind/Northwind.svc/$metadata to local file Metadata.csdl
    type Northwind = ODataService<"http://services.odata.org/Northwind/Northwind.svc/",
                                  LocalSchemaFile="Metadata.csdl",
                                  ForceUpdate=false>
    
    let db = Northwind.GetDataContext()
    db.Credentials <- System.Net.CredentialCache.DefaultCredentials  // or whatever creds you need
    
    // go party