Use AppDomain.Load(byte[]) without loading from file? - reflection

I crack my mind... I trying to manually load and unload domain with one assembly:
var domainInfo = new AppDomainSetup
{
PrivateBinPath = baseDir
};
_hostedDomain = AppDomain.CreateDomain("Domain", null, domainInfo);
_hostedDomain.UnhandledException += HostedDomainUnhandledException;
_hostedDomain.DomainUnload += _hostedDomain_DomainUnload;
var bytes = File.ReadAllBytes("Facade.dll"); // Loads facade from hosts location.
_hostedDomain.Load(bytes);
string asmName = Path.Combine(baseDir, Properties.Settings.Default.AssemblyName);
bytes = File.ReadAllBytes(asmName);
var entryPoint = _hostedDomain.Load(bytes); // exception here!
And what I see:
When I tryed to load assembly I see that system load it from CurrentDir/assemblyname.dll (and fails).
Exception: System.IO.FileNotFoundException:
Could not load file or assembly Worker, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
LOG: This bind starts in default load context.
LOG: Using application configuration file: D:#Development!test\trunk\bin\Debug\Bootstrapper.vshost.exe.Config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///D:/#Development/!test/trunk/bin/Debug/Worker.DLL.
LOG: Attempting download of new URL file:///D:/#Development/!test/trunk/bin/Debug/Worker/Worker.DLL.
LOG: Attempting download of new URL file:///D:/#Development/!test/trunk/bin/Debug/Worker.EXE.
LOG: Attempting download of new URL file:///D:/#Development/!test/trunk/bin/Debug/Worker/Worker.EXE.
BTW. It is impossible to use LoadFrom due to limitations (I've got the problem with XML serialization....).
Wat the hell .NET doing in this routines?
Why it is so hard to load and unload domain?
Thanks for the help!

Related

Custom MsBuild Task fails with assembly loading errors

I have defined a custom MSBuild Task, using .netcore 5.0 and it is accessible here.
The "Application" project uses "SnykTaskFile" in its build process.
When I build "Application" I got the following error :
System.BadImageFormatException: Could not load file or assembly
'System.Diagnostics.Process, Version=5.0.0.0,
If I remove the following code from "SnykTaskFile" :
var processInfo = new ProcessStartInfo();
processInfo.FileName = "cmd";
processInfo.WorkingDirectory = Location;
Process.Start(processInfo);
And build the "Application" again, it builds successfully, however if I have a logic around "Location" :
if (Location.Contains("...."))
{
return false;
}
It throws another error :
System.IO.FileNotFoundException: Could not load file or assembly
'System.Runtime, Version=5.0.0.0,
According to the answer here and the discussion "Custom Tasks don't work with NET5.0" in git hub, it seems I should use netstandard 2.0 for task project type.

'bootstrap' is not a valid script name. The name must end in '.js'

I'm learning asp.net and so I'm trying to build up the project named "Wingtip Toys" from MSDN.
The project is developed on VS 2013 and I have VS 2012. When I try to add Bootstrap per the instruction from here I get the error on runtime in browser.
Server Error in '/' Application.
'bootstrap' is not a valid script name. The name must end in '.js'.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: 'bootstrap' is
not a valid script name. The name must end in '.js'.
when I add the .js on the master page it shows me to another error:
The assembly 'System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not contain a Web resource that has the name 'bootstrap.js'. Make sure that the resource name is spelled correctly.
I also add the CSS file to Bundle.config but still the same result. I already have the NuGet package available, like for the jQuery.js error, but I'm unable to sort this out yet.
Here's what worked for me.
I added this to my BundleConfig.cs file:
ScriptManager.ScriptResourceMapping.AddDefinition("bootstrap", new ScriptResourceDefinition
{
Path = "~/scripts/bootstrap.min.js",
DebugPath = "~/scripts/bootstrap.js",
LoadSuccessExpression = "bootstrap"
});
Please go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solutions.
In Browse, search for: AspNet.ScriptManager.bootstrap
If your version of bootstrap is higher than AspNet.ScriptManager.bootstrap first uninstall bootstrap and then install AspNet.ScriptManager.bootstrap
go to:
Tools
NuGet Package Manager
Manage NuGet Packages for Solutions...
search for Bootstrap.css
install it
try to run your app

Prism + Log4Net build error : "Cannot resolve dependency to assembly log4net"

I am trying to add log4net support in a Prism application. Unfortunately i get the following error for each prism modules :
Error 101 Unknown build error, 'Cannot resolve dependency to assembly 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.' [PRISM MODULE 1 PROJECT NAME]
Error 101 Unknown build error, 'Cannot resolve dependency to assembly 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.' [PRISM MODULE 2 PROJECT NAME]
ect ...
I set up the logger as follow :
I added log4net assembly reference to both the main app, and the bootstrapper.
I added log4net configuration to the app.config file.
I added [assembly: log4net.Config.XmlConfigurator(Watch = true)] to the App.xaml.cs file.
I created a logger implemeting ILoggerFacade and overwrote CreateLogger() in the Bootstrapper to make it return my custom logger.
That's it.
If i try to build, it fails with the error mentioned above for each prism module project.
The only work around i found was to manually add log4net references to each prism modules projects which, i think, kind of defeats the abstraction between ILoggerFacade and the dependencies of the actual implementation.
The error suggest to use the ReflectionOnlyAssemblyResolve event, but i don't see how it could help here as it is a build error, not runtime.
Any suggestion ? :-)
Ok i found the problem, each modules were referencing the bootstrapper which is just wrong, i removed that dependency and VOILA, fixed !

How to play sound located in different folder in flex?

I can access and play file named "Song.mp3" located in the root directory of my project but when I put it into "assets" directory I got error.
Here is my project directory:
- Test
-- src
--- Homeview.mxml
-- assets
--- Song.mp3
-- Song.mp3
I can access and play Song.mp3 located in the root folder but I get error when I want to play assets/Song.mp3.
This works:
var req:URLRequest = new URLRequest('../Song.mp3');
This does not:
var req:URLRequest = new URLRequest('../assets/Song.mp3');
I get following Runtime error:
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: app:/assets/Song.mp3
I think that Song.mp3 in the root directory get exported in the final swf file but assets/Song.mp3 does not. If that is the case is there a way to force Flex to export assets/Song.mp3?
You can use file class i.e. File.applicationStorageDirectory.resolvepath("your url")
or File.applicationDirectory.resolvepath("your url").Store it in File type variable;use the file.url or file.nativePath to access the file.
This will be able to retrieve your external file which is there in the applicaton folder.

Load Assembly stored in Package (C#)

Is it possible to load an assembly that is stored in a package (a System.IO.Packaging type package, that is) in C#?
I am using ReflectionLoadFrom and passing the Uri that is formed by the PackageUriHelper, which looks correct. At runtime, I get a FileLoadException.
Edit:
Here's the code loading the assembly:
Assembly assembly = Assembly.ReflectionOnlyLoadFrom(mainDllUri.ToString());
Here's the code that forms the URI:
UriBuilder packageUriBuilder = new UriBuilder();
packageUriBuilder.Path = this.path;
packageUriBuilder.Scheme = "pack";
Uri packageUri = packageUriBuilder.Uri;
Uri partUri = currRelationship.TargetUri;
return PackUriHelper.Create(packageUri, partUri);
I know you can use other URIs (e.g. file://), but I've never seen anyone use the pack URI.

Resources