Castle Windsor Namespace conflicts - c#

Say I'm working inside a namespace called, for example, Org.Company and that this namespace contains MyClass.
I'm also importing a nuget with a namespace called Company with a class named OtherClass.
So, when I'm working inside my Org.Company namespace, I can't do the following:
Company.OtherClass obj;
because the compiler assumes that I actually mean:
Org.Company.Otherclass obj
which doesn't exist.
So, as far as I know, instead of using the fully qualified name, I actually MUST import the other namespace as such using Company;
The problem, however, is that I need to reference this OtherClass from a XML file (Castle Windsor configuration file) and the fully qualified name Company.OtherClass isn't working.
Is there a way around this? Changing namespace names isn't a viable option.
EDIT:
This is what I have on my Castle Windsor xml file
<using assembly="MyProj, Version=1.0.0.0, Culture=neutral" />
...
<component
service="Company.IOtherClass"
type="Company.OtherClass"
/>
I get the following error:
{"Could not convert string 'Company.OtherClass' to a type. Make sure assembly containing the type has been loaded into the process, or consider specifying assembly qualified name of the type."}
Probably because its looking inside Org.Company instead, defined in the MyProj assembly.
I assume this could be fixed if there was a way to add another <using /> statement referencing the Nuget package... Is there a way to do that?

As far as I understand your problem is about referencing from XML? Right?
So as Castle.Windsor suggests "consider specifying assembly qualified name of the type.". What does it mean? It means that instead of:
<component
service="Company.IOtherClass"
type="Company.OtherClass"
/>
You should give class names with assembly they are coming from:
<component
service="Company.IOtherClass, OtherClassAssembly"
type="Company.OtherClass, OtherClassAssembly"
/>
or even fully qualified assembly name:
<component
service="Company.IOtherClass, OtherClassAssembly, Version=..., Culture=..., PublicKeyToken=..."
type="Company.OtherClass, OtherClassAssembly, Version=..., Culture=..., PublicKeyToken=..."
/>
NOTE: replace OtherClassAssembly with actual assembly name and DON'T add .dll extension here
NOTE: see http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx for more details, for example how to deal with nested classes
To use OtherClass in code, well, you can use global
global::Company.OtherClass
you can also use using:
using Company;
or just import one class:
using ClassFromOtherCompany = Company.OtherClass;

You can use namespace alias, something like this MSDN example:
using Co = Company.Proj.Nested;

Well, judging from the description of the problem and the error message you're getting it would appear your suspicion is correct.
The assembly from Nuget package hasn't been loaded into the AppDomain by the time Windsor runs its installers, so it has no way of knowing where to get the Company.OtherClass from.
You can then either do as the error message suggests, and use assembly qualified type name, or preload the assembly, with <using /> tag.

Related

namespace not found in xaml, but can be used in C# code

I have a WPF Project Home,it will reference a library project which defines namespace 'LibraryProjectExample', I want to use it in the Home Project,the namespace can not be found:
xmlns:view="clr-namespace:LibraryProjectExample"
But when I use using namespace LibraryProjectExample in C# code,I can use it normally.
I have checked that the LibraryProject has been referenced by the Home Project, I don't know why i can't use the namespace in xaml.Anyone can tell me,thanks!
You have to use as below in you Xaml page.
xmlns:object="clr-namespace:**namespace**;assembly=*assembly*"
here namespace is your packagename.classname and assembly is your packagename
For Example, xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
Thank You.
It looks like you're not adding the assembly to your xaml reference. Look in your LibraryProjectExample project and find it's package name, then you can append it to your namespace declarationlike so
xmlns:view="clr-namespace:LibraryProjectExample;assembly=YOUR_PACKAGE_NAME"
Besides the namespace you should also specify the name of the library project/assembly where the namespace is defined:
xmlns:view="clr-namespace:LibraryProjectExample;assembly=LibraryProject"
You need to change "LibraryProject" in the above sample markup to the actual name of the referenced project where the "LibraryProjectExample" namespace is defined.

You must add a reference to assembly 'System, Version=1.0.5000.0

I'm trying to include a html to pdf converter in my C# program for windows mobile.
I found Pdfizer.
I added a refence to Pdfizer.dll and included it with using Pdfizer;
Also I added a reference to itextsharp that is required for Pdfizer.
Pdfizer is correctly added and I can use many of its functions and classes but I can't use HtmlToPdfConverter.Run()
My code:
FileStream filePDF;
filePDF = File.Create(path + ".pdf"); // path is string
html2pdf.Open(filePDF);
html2pdf.AddChapter(#"Chapter name");
html2pdf.Run(html); // html is a string that contains html code
html2pdf.Close();
Problem is in line html2pdf.Run(html); It says:
Error 1 The type 'System.Uri' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Definition of Run is
public void Run(string html);
public void Run(Uri uri);
I'm trying to use the Run(string html) option, but Uri is defined and visual studio says it is not referenced but yes it is.
I have using System; that contains Uri class, indeed I use it in other functions and works properly.
What is the problem? Should I add a reference to System, Version=1.0.5000.0? Where can I find It? Is there any way to tell Pdfizer Uri is in System.Uri?
Thanks
A public key token of b77a5c561934e089 indicates that you're referencing a desktop assembly, so my bet is that Pdfizer is built only for the desktop. There is no way to make that work in the COmpact Framework. If you can get the source, you can try to compile it for the CF, otehrwise you'll have to ask the owners if they have a CF version.

Get name of project from where dll is referenced via code

I'm making a class library project in C#. How to get the name of the project which will call the method from my class library?
I tried with Reflection:
System.Reflection.Assembly.GetExecutingAssembly()
and with
GetCallingAssembly
but it didn't work.
I am looking for this also. File name I can get from StackFrame just like the answer by #Maheep but to get project name is not really straight forward.
A. I just got a simple solution:
Based on the FileName (path) found from StackFrame, iterate for each parent folder.
Find any file with *.csproj (C#)
Optionally open csproj file to ensure the current file <Compile Include="Folder\File.cs" /> is included
The file name of csproj is the project name
B. I found alternative by using pre-build macro $(ProjectDir) and access the result in code here: What's an easy way to access prebuild macros such as $(SolutionDir) and $(DevEnvDir) from code in C#?
If I understand your question directly, this is not possible to do in code. The only way to do this is with static analysis of the code.
Resharper has this ability - To find out where a particular class/method/property is used, you can right click on the declaration and select "Find Usages". This is a very handy feature :)
But that only works of the code calling your method is known (in the same solution). It wont work when third parties consume your library.
What exactly are you trying to achieve here? If your method needs to identify callers, you should add this as a requirement (ie add a parameter containing the caller identity).
public void MyMethod()
{
// I need name of caller project, but how?
}
public void MyMethod(String callerProject)
{
// People who call this method know the name of their own project :)
}
You will have to use StackTrace Class for this. StackTrace class has GetFrame method which will give you calling method name. This will return MethodBase class object which has property DeclaringType. Using this type information you can get assembly details as well.
private void YouCalledMethod()
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(1);
Assembly assembly = stackFrame.GetMethod().DeclaringType.Assembly;
//use this assembly object for your requirement.
}
Look at this How to print the current Stack Trace in .NET without any exception? question also.

Need fully qualified type name

I need to resolve an assembly and type at runtime and I need to find the fully qualified type name. For some reason I cannot get it right, as I keep get an exception saying it cannot find the type specified.
The app.config file, in which the assembly to look for is defined, looks like this:
<configSections>
<section name="modules" type="Microsoft.Practices.Composite.Modularity.ModulesConfigurationSection, Microsoft.Practices.Composite"/>
</configSections>
<modules>
<module assemblyFile="G:\Data\Visual Studio 2008\Projects\Race Management System.Shell\ConfigurationModularity\bin\Debug\Modules\Driver.Data.Module.dll" moduleType="Driver.Data.Module.DriverDataModule, DriverDataModule" moduleName="Driver.Data.Module.DriverDataModule"></module>
</modules>
The assembly is called: Driver.Data.Module
Namespace in assembly is: Driver.Data.Module
and type name is: DriverDataModule, and this is also the name of the .cs file.
I cannot seem to find how to specify the name correctly in the xml file. Can somebody help me with the fully qualified type name?
This is for a Composite WPF application.
Thanx!
Try Driver.Data.Module.DriverDataModule, Driver.Data.Module.
You can also find the full assembly-qualified name of your type by instantiating an object of that type and examining the AssemblyQualifiedName property of its Type:
DriverDataModule module = new DriverDataModule();
string fullyQualifiedName = module.GetType().AssemblyQualifiedName;
What error do you get? If you're having a hard time getting a full error message out of the app, and you think your app is having problems loading the assembly itself, you can use the fuslogvw tool to log full details to disk.
It's worth also opening the assembly in Reflector to double-check the assembly's full name (displayed on the bottom-left of the window when you open Reflector), and to check that the type is in fact defined in the namespace you think it is.

Why am I getting an exception raised from Spring.NET on the call to ContextRegistry.GetContext()?

Even though the solution is so obvious I should have never have posted this, I'm leaving it up as a reminder and a useful point of reference to others.
I've got the following in my app.config file:
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
Followed by:
<spring>
<context>
<resource uri="config://spring/objects"/>
</context>
<objects xmlns="http://www.springframework.net">
<object name="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>
</objects>
</spring>
Then in my app I've got:
using Spring.Context;
using Spring.Context.Support;
public partial class AlbumChecker : Window
{
private DataTable dataTable;
private Library library;
private Thread libraryThread;
public AlbumChecker()
{
InitializeComponent();
CreateToolTips();
IApplicationContext ctx = ContextRegistry.GetContext();
library = (Library)ctx.GetObject("mediaLibrary");
// Other initialisation
}
// Other code
}
It all compiles quite nicely, however, I'm getting an exception raised on the call to GetContext():
Error creating context 'spring.root': Could not load type from string value
'AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF'.
I've checked the Spring.NET documentation and can't see what I'm doing wrong - but I clearly have got something wrong, otherwise it wouldn't raise the exception!
AlbumLibraryWPF is the namespace and AlbumLibraryWPF.AlbumLibrary is the fully qualified name of the class I want to instantiate. I'm guessing that it's this I've got wrong, but can't see how.
I feel such a fool.
It was because I'd failed to copy the AlbumLibrary.dll to the correct output directory. That meant that Spring couldn't find it - even after I'd fixed the assembly name problem Kent highlighted.
The name after the comma should be the assembly name, which is not necessarily the same as the namespace name.
I was getting this error because by mistake there was a typo [!*2] in app.config file. Once I took that out , error went away. some thing like this
<context>
<!--<resource uri="~//Aspects.xml"/>-->
<!--<resource uri="~//Dao.xml"/>-->
<!--<resource uri="~//Spring.xml"/>-->
<resource uri="file://Spring.xml"/>
<resource uri="file://Dao.xml"/>
</context>
!*2
You should use tha id attribute instead of name:
<object id="mediaLibrary" type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF"/>
Also it should be config://spring/objects instead of config://spring/obects.
You need to double check that you have a type called AlbumLibrary in AlbumLibraryWPF namespace defined in AlbumLibraryWPF assembly.
You can try change the type. The type="AlbumLibraryWPF.AlbumLibrary, AlbumLibraryWPF", first parameter means NameSpace and the second parameter (behind the dot) means Solution Name.
"AlbumLibraryWPF.AlbumLibrary" = NameSapce name
"AlbumLibraryWPF" = solution name
Open VS2012 or VS2010 with Administrator Permissions
Config: type="namespace.type, assembly"
Then try running your solution again.

Categories

Resources