ODataService provider error: (401) Unauthorized

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

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

Static type parameters for type provider :

  • ServiceUri: string The URI string for the OData service.
  • LocalSchemaFile: string The path to the file containing the schema. This file is written by a type provider.
  • ForceUpdate: bool Requires a direct connection to the service at the time of design / compilation, and the local file service is updated. The default value is correct. When ForceUpdate is false, the provider responds to changes in the LocalSchemaFile.
  • ResolutionFolder: string The folder that will be used to resolve relative file paths at compile time. The default value is the folder that contains the project or script.
  • DataServiceCollection: bool Creates collections obtained from a DataServiceCollection. The default value is false.
+1
source share
1 answer

Yes, but, unfortunately, this is not so smooth, and you do not get compile-time checking, which is one of the nice advantages of type providers.

You need to take it $metadatafrom your service and save it locally as a file .csdl, and then use the static parameter LocalSchemaFilein your code. You can then set the credentials for the data context object for authentication 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
+3
source

All Articles