7

I've been experimenting with C# script (*.csx) to do some data processing with the results of service calls to some internal APIs. It's been pretty smooth thus far, but I'm now trying to call some methods from a C# project I have access to. The C# project doesn't have a published nuget package, which means referencing the dll directly using #r. However in order to access the necessary functions, I've found I also need to add references to the top of my script for any of that projects dependencies and any nuget packages that dependency uses.

#r "PATH_TO_SOLUTION\Project\bin\Debug\netstandard2.0\binary.dll"
#r "PATH_TO_SOLUTION\ProjectDependency\bin\Debug\netstandard2.0\dependent.dll"
#r "nuget: Some.Dependency, 11.0.2"
#r "nuget: Some.Other.Dependency, 10.0.2"
#r "nuget: Some.Third.Dependency, 9.0.2"
using Project;

I'm new to the C# scripting world, and didn't find anything directly addressing this question so hopefully I'm not asking something super obvious here.

Some projects depend on a pretty large number of nuget packages, is there a way to reference a csproj from a C# script that doesn't require explicitly referencing all of the dependencies for the project?

Let me know if there's any additional information I can provide.

RAnderson
  • 180
  • 7
  • Since you have access to the project file itself, you can build the project. Correct? If you can do that, you can reference the assembly generated by the project (a dll I presume) in your C# script – i.do.stuff Dec 27 '18 at 15:13
  • 3
    Definitely, I'm able to build the project and successfully reference the compiled dll (`#r "PATH_TO_SOLUTION\Project\bin\Debug\netstandard2.0\binary.dll"`). On execution the script throws an error when the project attempts to use a method that requires a nuget package (`Some.Dependency`). The only way I've found to resolve the error is by adding a reference to that package to my C# script (`#r "nuget: Some.Dependency, 11.0.2"`). The number of references add up pretty quickly. So I'm wondering if there's away to reference the project without having to add explicit references to its packages. – RAnderson Dec 27 '18 at 15:33

1 Answers1

2

Please note Essential .NET - C# Scripting at Microsoft Docs:

you can take the entire listing and save it as a CSX file, then “import” or “inline” the file into the C# REPL window using #load Spell.csx. The #load directive allows you to include additional script files as if all the #load files were included in the same “project” or “compilation.” Placing code into a separate C# script file enables a type of file refactoring and, more important, the ability to persist the C# script over time.

Take all of the reference, save it as a CSX file and give it a shot.

for example:

//Load scripts
#load "Script.csx"  
using Project;
Barr J
  • 10,636
  • 1
  • 28
  • 46
  • 2
    I'd seen the load command, but this hadn't occurred to me. It worked quite well, although it unfortunately still requires listing all of the nuget dependencies required by a project dll. It's _much_ cleaner and pretty maintainable as well. Thanks for the help! – RAnderson Jan 07 '19 at 23:46