How can I reference system assembly in Cake script? - cakebuild

I want to use System.Data in my cake script, to perform some DB operations.
My script is sth like this:
#r "System.Data"
void DoSomething()
{
System.Data.SqlConnection conn = new System.Data.SqlConnection(..);
}
However, this does not compile resulting in error
error CS0246: The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)
What is the proper way to reference System assemblies (from GAC) in Cake scripts?

System.Data is already loaded with cake.
Your problem is the namespace of SqlConnection.
The correct NS is System.Data.SqlClient.SqlConnection
your file can just have this
using System.Data.SqlClient;
void DoSomething()
{
SqlConnection conn = null;
}

You can use intellisense as well if you are using vs code.
Go to Extensions and add Cake Extension.
Then open command pallete and select Add tool from Nuget.
and just Type Cake.Bakery and install it.
Run the build script once and restart the vs code again.
You will get intellisense. Not to worry.

Related

I don't know how to call using UnityEngine.XR.ARFoundation in a C# script inside my own Package in Unity

I don't know how to call using UnityEngine.XR.ARFoundation in a C# script inside my own Package in Unity.
I have added ARFoundation in Dependencies of Package as shown in the image. But I'm getting "error CS0246: The type or namespace name '~' could not be found (are you missing a using directive or an assembly reference?)". How can I resolve this?
I solved the problem by creating Assembly Difinition Files in my own package and adding references to Assembly Difinition References.
I fixed this issue in "Edit" ... "Preferences" ... "External Tools", by checking "Regenerate Project Files"; this hopefully regenerates all the necessary ".csproj" files for IntelliSense tracking.

Azure Functions: Can compile but cannot run with custom datalayer library

I've tried to come up with a better title but can't.
The issue is I am new to Azure functions but have made a simple one work that writes to a SQL Azure table. Now I've attempted to build the simplest kind of Entity Framework based Datalayer and uploaded it. Right now it is compiled as .Net 4.6 and using EF 6.1.3.
I'm using a connection string as per the second answer here Second answer and have checked it is being retrieved correctly. Update - I also used this guide.
Removing this {#r "D:\home\site\wwwroot\sharedbin\TestDataLayer.dll"} causes the editor to complain about missing assemblies, so it IS finding the dll in question.
However it will not run - it cannot find TestDataLayer.dll.
I'm only running this in the portal editor (I've not yet mastered deployment direct from a Visual Studio Project - don't laugh :P).
#r "System.Configuration"
#r "System.Data.Entity"
#r "D:\home\site\wwwroot\sharedbin\TestDataLayer.dll"
using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.SqlServer;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Net;
using TestDataLayer;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
var connection = ConfigurationManager.ConnectionStrings["sql_connection"].ConnectionString;
using(var db = new SyncDbContext(connection))
{
var RK = new RKAzureTest() {TestField1 = "It finally worked?" };
db.RKAzureTests.Add(RK);
db.SaveChanges();
}
}
[DbConfigurationType(typeof(myDBContextConfig))]
public partial class SyncDbContext : System.Data.Entity.DbContext
{
public SyncDbContext(string cs) : base(cs) {}
public DbSet<RKAzureTest> RKAzureTests {get;set;}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
public class myDBContextConfig : DbConfiguration
{
public myDBContextConfig()
{
SetProviderServices("System.Data.EntityClient",
System.Data.Entity.SqlServer.SqlProviderServices.Instance);
SetDefaultConnectionFactory(new System.Data.Entity.Infrastructure.SqlConnectionFactory());
}
}
This is the function.json:
{
"frameworks": {
"net46":{
"dependencies": {
"EntityFramework": "6.1.3"
}
}
}
}
I've compiled the dll itself to .Net 4.6 after a suspicion that the Azure Functions don't support .net 4.7.1 and via Kudu uploaded the compiled dll to a sharedbin folder (checked the path a dozen times!).
This is the error thrown up:
2018-05-01T11:00:00.012 [Warning] Unable to find assembly 'TestDataLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Are you missing a private assembly file?
2018-05-01T11:00:00.012 [Error] Exception while executing function: Functions.TimerTriggerCSharp1. mscorlib: Exception has been thrown by the target of an invocation. f-TimerTriggerCSharp1__514732255: Could not load file or assembly 'TestDataLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
Not quite sure what else can be left - I'm using runtime version 1.0.11702 in the Application settings as I found life got a LOT more complicated if I went onto the Beta version.
If anyone can point me to a working guide for this use case (Database first, EF 6.1.3 etc) I'd be grateful.
Any help offered gratefully received!
Thank you :)
Go to Azure Portal, create a folder called, 'bin' inside your Azure functions using CMD Shell, upload the 'TestDataLayer.dll' file to bin folder which has just been created.
#r "System.Configuration"
#r "System.Data.Entity"
#r "TestDataLayer.dll"
Project structure should look like,
AzureFunctionProjectName001
bin
TestDataLayer.dll
run.csx
project.json
project.lock.json
...
Azure functions should be able to discover your library this time. I believe, EntityFramework works just fine.

How do I load Nuget packages that a custom DLL depends on?

I'm developing a Function App via the Portal (not local development). I have a custom DLL that depends on 1 nuget package: Entity Framework 6.1.3
I have uploaded my DLL to "../bin" and my code compiles successfully when I reference my DbContext object. So far, so good.
I also have a Project.json file and I see it acquiring the nuget packages when I save. So far, so good.
{
"frameworks": {
"net46":{
"dependencies": {
"EntityFramework": "6.1.3"
}
}
}
}
My Run.csx code compiles successfully and looks like this:
#r "../bin/Library.dll"
using System;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info("My code: Started");
log.Info(typeof(Library.MyContext).ToString());
}
However, the code doesn't actually run (I don't even see the "My code: Started" log item). The error I receive is:
2017-02-27T06:37:28.731 Exception while executing function:
Functions.TimerTriggerCSharp1. mscorlib: Exception has been thrown by
the target of an invocation. f-TimerTriggerCSharp1__-205940111: Could
not load file or assembly 'EntityFramework, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its
dependencies. The system cannot find the file specified.
My custom DLL is the simplest possible EF-referencing DLL I can possibly make. All you need to recreate it is this:
Custom DLL packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net46" />
</packages>
Custom DLL Class1.cs
using System.Data.Entity;
namespace Library
{
public class MyContext : DbContext
{
}
}
What am I doing wrong that is keeping my custom DLL from being able to utilize the downloaded EntityFramework nuget package?
Just to verify that my nuget references are actually working, if I comment out most of my Run.csx code and replace it with this line, all executes correctly and logs what you would expect:
log.Info($"My code: {typeof(System.Data.Entity.DbContext).ToString()}");
As somebody suggested, I have tried changing my Run.csx references to look like this and it doesn't change the runtime error I get (it does compile so the path is correct):
#r "../../../data/Functions/packages/nuget/entityframework/6.1.3/lib/net45/EntityFramework.dll"
#r "../bin/My.dll"
I can also change my Run.csx file to contain this code and it does successfully execute:
using System;
public static void Run(TimerInfo myTimer, TraceWriter log)
{
log.Info(typeof(MyContext).ToString());
}
public class MyContext : System.Data.Entity.DbContext
{
}
In order to consume the assemblies coming from your referenced packages, you can deploy your custom dependency as a private assembly:
Deploy the assembly into a bin folder, in your function folder (e.g. wwwroot\myfunction\bin)
Reference the assembly without the relative path, by file name only (e.g. Library.dll)
If you wish to use shared assemblies, deployed to a common location and referenced as you have above, you'd need to deploy the assembly with its dependencies (essentially the output from your project build).
Another option that I would recommend if you want to take advantage of the shared model is to deploy your dependency as a NuGet package (which you can deploy to either a custom source hosted somewhere or as a file), that package would then specify its package dependencies and they would all be resolved automatically.
Looks like configuration problem, you need to define the reference in the project file to be copied to output.
In the Solution Explorer, expand the project, References, right click the EntityFramework, Properties, Set Copy Local = true.
Sometimes the default value is false, so then it will not in the output folder of the project.
I hope this helps.

How do I reference a UWP+NET46 portable library from a .NET 4.6 console application?

I have a portable class library project that targets .NET 4.6 and Universal Windows Platform. This class library contains just one class with the following line of code in its constructor:
Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()));
Now I create a new .NET 4.6 console application project in the same solution and add a project reference to the portable class library. Calling the method that houses the above line of code results in the following exception at runtime:
Could not load file or assembly 'System.IO.FileSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
What am I doing wrong here? There are no compile-time errors or warnings.
Things I have tried: add missing(?) NuGet package manually
It seems that System.IO.FileSystem is a library delivered via NuGet, as part of the Microsoft.NETCore mega-package. Okay, perhaps I need to explicitly add this package to any project that uses my portable class library. I attempt to do so.
Could not install package 'Microsoft.NETCore.Platforms 1.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.6', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
No luck with this approach.
Things I have tried: create a project.json file
While there is no clear info on the web, I read a few tidbits about a new project.json based NuGet harness or build system. Just to experiment, I created the following project.json file in my console application project:
{
"dependencies": {
},
"frameworks": {
"net46": { }
},
"runtimes": {
"win-anycpu": { }
}
}
It works! The runtime error goes away! However, I soon found that this was either not the right solution or not a complete solution. I started writing some code to read configuration section values, which involved making use of the IConfigurationSectionHandler interface, and got the following compile-time error:
error CS0246: The type or namespace name 'IConfigurationSectionHandler' could not be found (are you missing a using directive or an assembly reference?)
This interface is part of the System assembly. I see a reference to this assembly, but it has a yellow exclamation mark icon, and a warning appears in the warnings window:
The referenced component 'System' could not be found.
This is where I ran out of ideas. Am I missing something totally obvious?
I have found the solution. My initial attempt was to install the Microsoft.NETCore package into the console application, resulting in the error shown in my original post.
However, if I install only the narrowly-scoped packages, e.g. System.IO.FileSystem, then I achieve success and the application works correctly. Apparently there is something special about the Microsoft.NETCore "master package" that prevents it from correctly installing into dependent projects.

Custom Assembly in SSRS 2008

I created a custom assembly that I'm trying to run in my SSRS 2008 project. This is a simple static method in a class:
namespace Utilties
{
public class Expressions
{
public static string SayHello()
{
return "Hello Test!";
}
}
}
The project compiles into an assembly. Then I go to Report > Properties and add the assembly there and in the following location:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies
My understanding is that by default, the assembly already has execute permissions as I do not intend on adding Code support using CAS.
I inserted a textbox in my report with the expression:
=Utilities.Expressions.SayHello()
But when I build, I get an error:
[rsCompilerErrorInExpression] The Value expression for the textrun
‘Textbox16.Paragraphs[0].TextRuns[0]’ contains an error: [BC30451]
Name 'Utilities' is not declared.
Is there an additional step I'm missing?
Never mind the question. The problem was that I mispelled the namespace. Once I corrected the issue, the problem went away and everything worked the way it should.