Custom assemblies not being referenced in Views - azure

If a custom c# assembly is used in a View, the compiler fails to find the reference to the namespace.
It works if the assembly is referenced and called within a query but not from a view.
For instance, consider this U-SQL script for creating a view:
The assemblies being used are registered in mylocaldb before running this query, and are available to be accessed in any script.
USE DATABASE mylocaldb;
REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
USING Newtonsoft.Json;
USING Microsoft.Analytics.Samples.Formats.Json;
DROP VIEW IF EXISTS SearchAccountData;
CREATE VIEW MyView AS
EXTRACT
UserId string,
UserName string
FROM "Data/mydata.json"
USING new Microsoft.Analytics.Samples.Formats.Json.JsonExtractor();
After the execution of the script, the view gets created and stored an under Views in the mylocaldb database.
Upon using the view in another query:
USE DATABASE mylocaldb;
USE SCHEMA dbo;
REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
USING Newtonsoft.Json;
USING Microsoft.Analytics.Samples.Formats.Json;
#foo= SELECT * FROM MyView;
OUTPUT #foo
TO "/output/foo.csv"
USING Outputters.Csv();
(the USE SCHEMA dbo isn't necessary as the database defaults to that in case no other schema is being used):
the compiler throws an error:
E_CSC_USER_INVALIDCSHARP: C# error CS0234: The type or namespace name 'Samples' does not exist in the namespace 'Microsoft.Analytics' (are you missing an assembly reference?)
Now this might be conflicting with the library that comes with U-SQL: Microsoft.Analytics, but I have tried using another assembly with a different namespace, and I still have the same issue.
I am testing locally using the (Local) account in VisualStudio.

I decided to use a PROCEDURE.
In Views UDFs (User Defined Functions) or UDOs can't be used.
Views cannot:
Contain user-defined objects (such as UDFs or UDOs).
Will be inlined upon call.
This information can be found here on slide 27 at U-SQL - Azure Data Lake Analytics for Developers
It would be helpful if this was mentioned in U-SQL Language Reference docs for Views.
Unlike Views, procedures and table-valued functions have support for UDFs and can include Reference to assemblies:.
CREATE PROCEDURE ExtractTransactions(#data_stream string = #"Data/{*}")
AS BEGIN
REFERENCE ASSEMBLY [Newtonsoft.Json];
REFERENCE ASSEMBLY [Microsoft.Analytics.Samples.Formats];
USING Newtonsoft.Json;
USING Microsoft.Analytics.Samples.Formats.Json;
#transactions =
EXTRACT
UserId string,
UserName string
FROM #data_stream
USING new JsonExtractor();
INSERT INTO ExtractedTransactions
SELECT * FROM #transactions;
END;
And then simply call the procedure from another query:
ExtractTransactions(DEFAULT) (where DEFAULT is the default parameter).

Thanks Mike & sponturious. U-SQL Language Reference has been updated at CREATE VIEW (U-SQL) under the Query_Expression section at the official U-SQL Language Reference site
(Note: Would provide a link but the Gods keep deleting my responses when I do. Just do a search for "CREATE VIEW (U-SQL)".)

Related

How to rewrite the table qualifier at runtime with JOOQ

When generating code using JOOQ for a SQL Server database the generation creates three-part qualifiers like: [catalog].[schema].[table]. This is exactly what I want when working with the SQL Server databases but is an issue when using the generated code with another database like an H2 in memory database for unit testing.
The H2 dialect does not support these three-part qualifiers, H2 expects something like [catalog].[table]. This causes syntax errors when executing commands like the following against H2:
context.createTable(TBLBUSINESSENTITY).columns(TBLBUSINESSENTITY.fields()).execute();
To solve this I need to change the qualifier at runtime which I thought could be done using a render mapping and mapped schema. Unfortunately, this seems to only have the ability to modify the schema portion of the qualifier like this:
Settings settings = new Settings().withRenderMapping(new RenderMapping().withSchemata(
new MappedSchema().withInput("dbo").withOutput("mySchema")));
Given the qualifier [MyDatabase].[dbo].[MyTable], this maps to [MyDatabase].[mySchema].[MyTable] but I cant figure out how to remove that section entirely.
Is there some way to rewrite the mapping to [MyDatabase].[MyTable]?
Use this setting instead:
Settings settings = new Settings()
.withRenderCatalog(false)
.withRenderMapping(new RenderMapping()
.withCatalogs(new MappedCatalog()
.withInput("MyDatabase")
.withSchemata(new MappedSchema()
.withInput("dbo")
.withOutput("MyDatabase"))));

Getting the assembly a control is being used in, from a custom UITypeEditor, at design time

Although there are many threads dealing with similar questions I couldn't find any that cover this case.
I have a main app that references a class library. In the class library is a control with a property that must be populated with a form name from a drop-down of forms available in the main app - NOT the class library.
I've discovered that, inside of the UITypeEditor code,
Control owner = context.Instance as Control;
gives me a reference to the control for which the property value is required. But getting a reference to the proper assembly (the main app, not the library the control is in) so that I can list the available form names in a drop down has proved difficult.
owner.GetType().Assembly.ToString() -- gives me the class library name, not the main app
Assembly.GetExecutingAssembly().ToString() --- gives me the class library name
Assembly.GetCallingAssembly().ToString() --- gives me System.Windows.Forms
There is no route I can find to getting the assembly of the form onto which I'm placing the control which has the property with custom editor that needs that assembly.
I realize this is an old question, but it is easily answered if one understands the mechanisms employed in writing basic designer code. There are three articles that I recommend reading to gain a working knowledge of the subject.
Building Windows Forms Controls and Components with Rich Design-Time Features; MSDN Magazine, April 2003
Building Windows Forms Controls and Components with Rich Design-Time Features, Part 2; MSDN Magazine, May 2003
Tailor Your Application by Building a Custom Forms Designer with .NET; MSDN Magazine, December 2004
Note: The above links are to compiled HTML help files. Remember to unblock the content using the file's properties dialog.
To obtain a reference to the Assembly that contains the Form with the Control being manipulated on the design surface, you need to obtain a reference to the IDesignerHost service from the IServiceProvider instance "provider" that is passed the EditValue method. IDesignerHost exposes the property RootComponentClassName that will be the fully qualified name of the base component class which in this case is the containing form. With this name you can obtain a Type instance using the IDesignerHost.GetType method. Note that GetType may return a null value if the project has not been "built" since adding the form to the project.
C# Example Snippet For UITypeEditor
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IDesignerHost host = provider.GetService(typeof(IDesignerHost)) as IDesignerHost;
string typName = host.RootComponentClassName;
Type typ = host.GetType(typName);
Assembly asm = null;
if (typ == null)
{
MessageBox.Show("Please build project before attempting to set this property");
return base.EditValue(context, provider, value);
}
else
{
asm = typ.Assembly;
}
// ... remaining code
return base.EditValue(context, provider, value);
}
VB Example Snippet For UITypeEditor
Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
Dim host As IDesignerHost = TryCast(provider.GetService(GetType(IDesignerHost)), IDesignerHost)
Dim typName As String = host.RootComponentClassName
Dim typ As Type = host.GetType(typName)
Dim asm As Assembly
If typ Is Nothing Then
MessageBox.Show("Please build project before attempting to set this property")
Return MyBase.EditValue(context, provider, value)
Else
asm = typ.Assembly
End If
' ... remaining code
Return MyBase.EditValue(context, provider, value)
End Function

OrmLite dynamic database schema

Is it possible to define dynamic schema with OrmLite in runtime. For instance, when reading object through OrmLite is it possible to define which schema to read it from.
This would be best shown through an example. Let's say I have 3 User tables inside MSSQL 2008 R2 database:
Schema1.user
Schema2.User
Schema3.User
I have an object User with some properties defined. I select data like this "db.Select();". The problem is I have not defined from which schema to read User data from. I want to be able to do this at runtime, but I can't seem to find a propert way to do it.
Using C#, .NET 4.5 fw, MSSQL 2008 R2 Database
Thank you!
You can do this by modifying OrmLite's metadata that it maintains for each type, i.e:
var modelDef = SqlServerOrmLiteDialectProvider.GetModelDefinition(typeof(Poco));
var existingSchema = modelDef.Schema;
modelDef.Schema = "Schema2";
// All queries on Poco now use `Schema2`
modelDef.Schema = existingSchema; //Retain existing behavior
This functionality has gotten easier in the current release. You can just do data annotations over the class to set the as is explained here.
example:
[Schema("test")]
public class Foo
{
}

WCF DataService EF entities not found

I have an EDMX model with a generated context.
Now i generated a Self Tracking Entities library is separate project and referenced this from the EDMX model.
Also set the correct namespace in the context to the same namespace as the entities.
Now working with this all works except when i try to create a WCF data service with this context.
So just create new ObjectContext and working with it directly works fine.
But having referenced the context + model lib and the entities lib i get the following error when loading the service
The server encountered an error processing the request. The exception message is 'Value cannot be null. Parameter name: key'. See server logs for more details. The exception stack trace is:
Now i found that this could happen when using data service with external entity lib and fix was overriding the createcontext
with code
Collapse
System.Data.Metadata.Edm.ItemCollection itemCollection;
if (!context.MetadataWorkspace.TryGetItemCollection
(System.Data.Metadata.Edm.DataSpace.CSSpace, out itemCollection))
{
var tracestring = context.CreateQuery<ClientDataStoreContainer>("ClientDataStoreContainer.DataSet").ToTraceString();
}
return context;
Now the error is gone but i get the next one and that is:
Object mapping could not be found for Type with identity 'ClientDataStoreEntities.Data'.
This error occurs on the .toTraceString in the createcontext
the ssdl file has the defined type
Collapse
<EntitySetMapping Name="DataSet">
<EntityTypeMapping TypeName="IsTypeOf(ClientDataStoreEntities.Data)">
So it has to load the ClientDataStoreEntities.Data type which is the namespace and type of the STE library that i have generated from the model.
EDIT: with
var tracestring = context.CreateQuery<Data>("ClientDataStoreContainer.DataSet").ToTraceString();
It does seem to load all types , however now the service does not have any methods that i can call.
there should be 2 DataSet and PublishedDataSet but:
<service xml:base="http://localhost:1377/WcfDataService1.svc/">
−
<workspace>
<atom:title>Default</atom:title>
</workspace>
</service>
is what i get.
I ran into the same issue (the first one you mention). I have worked around using the suggestion by Julie Lerman in this thread. The other suggestion didn't work for me although I will experiment with them more since Julie's solution may have performance implications since it's executed (and has some cost) for every query.
MSDN Fail to work with POCO ModelContainer which entities are located in other assembly
Edit: Sorry, just realized you utilized the other solution mentioned in this thread.

Is it possible to use ASP.NET Dynamic Data and SubSonic 3?

Is it possible to use ASP.NET Dynamic Data with SubSonic 3 in-place of Linq to SQL classes or the Entity Framework? MetaModel.RegisterContext() throws an exception if you use the context class that SubSonic generates. I thought I remembered coming across a SubSonic/Dynamic Data example back before SubSonic 3 was released but I can't find it now. Has anyone been able to get this to work?
I just got Subsonic 3.0.0.4 ActiveRecord working last night in Visual Studio 2010 with my SQLite database after a little bit of work and I've tried to document the steps taken here for your benefit.
Start by adding a New Item -> WCF Data Service to the project you're using to host your webapp/webservices then modify it similar to my PinsDataService.svc.cs below:
public class PinsDataService : DataService<PINS.Lib.dbPINSDB>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.UseVerboseErrors = true;
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
At this point your Dynamic Data Service would probably be working if you matched all the database naming conventions perfectly but I didn't have that kind of luck. In my ActiveRecord.tt template I had to prepend the following two lines before the public partial class declarations:
[DataServiceKey("<#=tbl.PrimaryKey #>")]
[IgnoreProperties("Columns")]
public partial class <#=tbl.ClassName#>: IActiveRecord {
I then added references to System.Data and System.Data.Services.Client followed by the inclusion of using statements for using System.Data.Services and using System.Data.Services.Common at the top of the ActiveRecord.tt template.
The next step was to use the IUpdateable partial class implementation from this blog post http://blogs.msdn.com/aconrad/archive/2008/12/05/developing-an-astoria-data-provider-for-subsonic.aspx and change the public partial class dbPINSDB : IUpdatable to match my subsonic DatabaseName declared in Settings.ttinclude
Then to consume the data in a separate client app/library I started by adding a 'Service Reference' named PinsDataService to the PinsDataService.svc from my client app and went to town:
PinsDataService.dbPINSDB PinsDb =
new PinsDataService.dbPINSDB(new Uri("http://localhost:1918/PinsDataService.svc/"));
PinsDataService.Alarm activeAlarm =
PinsDb.Alarms.Where(i => i.ID == myAA.Alarm_ID).Take(1).ElementAt(0);
Note how I'm doing a Where query that returns only 1 object but I threw in the Take(1) and then ElementAt(0) because I kept getting errors when I tried to use SingleOrDefault() or First()
Hope this helps--also, I'm already aware that dbPINSDB is a really bad name for my Subsonic Database ;)

Resources