public override void InitializeContext(IConnectionInfo cxInfo, object context, QueryExecutionManager executionManager) { // This method gets called after a DataServiceContext has been instantiated. It gives us a chance to // perform further initialization work. // // And as it happens, we have an interesting problem to solve! The typed data service context class // that Astoria's EntityClassGenerator generates handles the ResolveType delegate as follows: // // return this.GetType().Assembly.GetType (string.Concat ("<namespace>", typeName.Substring (19)), true); // // Because LINQPad subclasses the typed data context when generating a query, GetType().Assembly returns // the assembly of the user query rather than the typed data context! To work around this, we must take // over the ResolveType delegate and resolve using the context's base type instead: var dsContext = (DataServiceContext)context; var typedDataServiceContextType = context.GetType ().BaseType; dsContext.ResolveType = name => typedDataServiceContextType.Assembly.GetType (typedDataServiceContextType.Namespace + "." + name.Split ('.').Last ()); // The next step is to feed any supplied credentials into the Astoria service. // (This could be enhanced to support other authentication modes, too). var props = new AstoriaProperties (cxInfo); dsContext.Credentials = props.GetCredentials (); // Finally, we handle the SendingRequest event so that it writes the request text to the SQL translation window: dsContext.SendingRequest += (sender, e) => executionManager.SqlTranslationWriter.WriteLine (e.Request.RequestUri); }
static XmlReader GetSchemaReader(AstoriaProperties props) { var uri = new DataServiceContext (new Uri (props.Uri)).GetMetadataUri (); var settings = new XmlReaderSettings (); XmlResolver resolver = new XmlUrlResolver (); // If credentials have been supplied, feed these in as we're querying the schema: resolver.Credentials = props.GetCredentials (); settings.XmlResolver = resolver; return XmlReader.Create (uri.ToString (), settings); }