How to point to startup class in different assembly and directory? - owin

Host and application startup routine lives in separate projects that has no reference to each other, can I somehow reference this startup class using StartOptions.AppStartup property or App.config in host project?

Related

Migrations: purpose of the startup project

When carrying out an update-database in the package manager you have to select a project as the startup project and the output from the command tells you what the startup project it is using. My question is what is the significance of the startup project?
The start-up project is used to find the correct App/Web.config file and the output directory to use.
Consider the following. You're DbContext exists in ClassLibrary1 which is used by WebApplication1. The settings that EF will ultimately use are stored in the Web.config of the WebApplication1 project. When using migrations commands, you would specify ClassLibrary1 as the default project and WebApplication1 as the start-up project. Also, if you were using relative paths somewhere inside your context or migrations, you would want them to resolve relative to WebApplication1's output directory, not ClassLibrary1's.
Of course, if everything is in one project, you don't have to worry about the start-up project since it will always be the same as the default project.

Unable to load assembly in appDomain

Until recently I was loading my assembly by calling Assembly.LoadFrom and it was ok. But now I need to load it in a temporary appDomain but I keep having a FileLoadException when trying to load the assembly in the temp domain. I have tried to pass appDomainSetup parameters to the CreateDomain method but without success.
Here is my code.
var tempDomain = AppDomain.CreateDomain("TempDomain");
Assembly sampleAssembly = tempDomain.Load(pathToDll);
My assembly is in a sub directory of my application base directory
AppDomain.Load loads the assembly in the currently executing AppDomain and not in the "TempDomain" one. As remarked in the MSDN doc:
This method should be used only to load an assembly into the current
application domain. This method is provided as a convenience for
interoperability callers who cannot call the static Assembly.Load
method. To load assemblies into other application domains, use a
method such as CreateInstanceAndUnwrap.
Now in your case the call fails because the currently executing AppDomain (most probably your main AppDomain) cannot locate assemblies from the sub directory. When you try to load assemblies in the load context, you should make sure that these assemblies are located in one of the following places:
the base dir of the AppDomain
a sub directory of the base dir that is specified in the AppDomain's private bin paths
the GAC
For more info you can check the following articles:
How the Runtime Locates Assemblies
Best Practices for Assembly Loading
Back to Basics: Using Fusion Log Viewer to Debug Obscure Loader Errors

how to set log4j.configuration system variable in WebSphere 7?

I have a Java EE 5 web app I'm deploying to WebSphere 7 as an EAR file.
I want my log4j configuration to be external to the EAR file so I can tweak log content when needed without needing to rebuild and redeploy the EAR file.
My understanding is I can specify the location of my log4j.properties file by setting a
"system variable" called log4j.configuration. (ex. log4j.configuration=c:/log4j.properties)
My question is, how do I set this system variable in the WebSphere 7 admin console?
Browsing around I see there is Environment > WebSphere Variables, but that doesn't look right because that would be setting a variable for the entire server. I'm guessing I want to set a system variable just for my application EAR file.
Any help or suggestions is greatly appreciated!
Rob
The log4j.configuration property is a Java Virtual Machine system property. You can load this property by adding it to the end of your application server's list of generic JVM arguments. This is done in the WebSphere Console by navigating through the following:
Servers > Application servers > [app server name] > Process definition > Java Virtual Machine
Under Generic JVM arguments, add the following:
-Dlog4j.configuration=file:C:/log4j.properties
Click Apply at the bottom of this page, and save your changes. This will require a restart of the application server to take effect.
You can also use shared library for an application and put your log4j.xml there.
I find a solution to assign to each EAR or WAR an external log4j.xml configuration file:
I extracted the log4j.xml from our EAR, Zipped this lone XML into a JAR file. Placed it on the server in a path accessible by WebSphere,
Create a Shared Library (WebSphere > Environment > Shared Libraries) mapping the classpath to this archive. Make sure to select "Use an isolated class loader for this shared library" or you'd have to assign reference for each module of your application rather than just the parent EAR.
Assigning the reference within our EAR
(Application -> ApplicationType -> WebSphere enterprise applications -> EAR NAME -> Shared library references -> SELECT YOUR EAR -> Reference shared libraries -> SELECT THE LOG4J CONFIGURATION JAR) and ADD IT.
Restart the application
I used this way to assign a specific external log4j configuration file to each WAR in an an EAR.
Here you can find the original solution.
How to point Log4j.xml(not log4j2.xml) to external file
For Log4J (not Log4J2) in websphere 8.5 in Linux OS, please a custom properties under Servers > Application servers > [app server name] > Process definition > Java Virtual Machine.
property name: log4j.configuration property value : file:/xyz/abc/def/config/log4j.xml
NB: You don't need to append -D if you do it from custom properties. But if you give it under Generic JVM arguments use this:
-Dlog4j.configuration=file:/xyz/abc/def/config/log4j.xml -Dlog4j.debug
Each property must be separated by space. I added debug logs for Log4J itself to see from where it is picking the log4J.xml file.

.NET MEF DirectoryCatalog issue

I am developing Excel add-in and use MEF to provide extensibility. DirectoryCatalog works fine on my local drive, however when I deploy solution to the network drive composition silently fails.
In regular .exe application this problem can be resolved by adding "loadFromExternalSources=true" to "runtime" section of application configuration file, however for Excel add-in this file does not exist (and I can't add Excel.exe.config to the folder, containing Excel executable since this folder is read-only). Is there any way to programmatically enable loadFromExternalSources behavior?
Is there any way to programmatically enable loadFromExternalSources behavior?
If you create a seperate AppDomain with AppDomain.CreateDomain, then you can pass a AppDomainSetup which has a ConfigurationFile property.
However, I suppose that comvisible objects are normally created in the default appdomain so making the new AppDomain talk to Excel will probably be difficult. I guess it would involve shim classes in the default appdomain which pass calls through to the other appdomain.

Adding reference which is needed in Config file

I have two different projects 'A' and 'B'.A needs reference of B.I used spring.net setter property injection.I configured it in config file.
I have one more new console application where i added only 'A's reference and im getting the 'A's instance through XmlApplicationConext(path).but after running it im getting object creation exception.
Do i need to add 'B's refrence also in console application.if yes then what is the use of using spring .net config file.
Does Spring.Net internally automatically will load the dlls required.
Spring will automatically load the required dll's. As far as I know, it will look for the dll's to load in the same order as any other .NET application.
If you configure the console application to use classes from project B, then B.dll (the output dll from project B) has to be available to the console application at runtime only. You do not have to add a reference to project B to achieve this; you could also copy the B.dll file to the output directory.
I'm not sure what you mean by "the Spring.net config file", but be aware that config files are not automatically loaded, you have to specify them explicitly. You can import another configuration file in you configuration file, see the docs for a how-to:
<objects xmlns="http://www.springframework.net">
<import resource="file:///services.xml"/>
<import resource="assembly://ProjectB/MyDataAccess/data-access.xml"/>
<object id="object1" type="..."/>
<object id="object2" type="..."/>
</objects>

Resources