Spring.NET ObjectDefinitionStoreException using assembly to store configuration files - c#

I am learning Spring.Net, I created a class MyApplication and a library class MyLib with all the spring configuration files that MyApplication needs.
I retrieve the metadata using:
IApplicationContext ctx = new XmlApplicationContext("assembly...");
I have three different xml files, one (springconfiguration.xml) imports the two other. At the beginning of my tries, my spring configuration files were at the root level of MyLib. Everything worked fine.
----- MyLib
-- Properties
-- References
-- commonspring.xml
-- buttonspring.xml
-- springconfiguration.xml
Then I created some folders in MyLib to store my xml files and it fails if I use subfolders:
the following works:
----- MyLib
--Properties
--References
--Common
--commonspring.xml
--Gui
--buttonspring.xml
--Configuration
--springconfiguration.xml
my springconfiguration.xml file is then:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<import resource="Common/commonspring.xml"/>
<import resource="Gui/buttonspring.xml"/>
</objects>
and I retrieve the context with:
IApplicationContext ctx = new XmlApplicationContext("assembly://MyLib/MyLib/Configuration.springconfiguration.xml");
but if I use subfolders then it fails:
----- MyLib
--Properties
--References
--Common
--commonspring.xml
--Gui
--SpecialButton
--buttonspring.xml
--Configuration
--springconfiguration.xml
my springconfiguration.xml file is then:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<import resource="Common/commonspring.xml"/>
<import resource="Gui/SpecialButton/buttonspring.xml"/>
</objects>
and I retrieve the context with:
IApplicationContext ctx = new XmlApplicationContext("assembly://MyLib/MyLib/Configuration.springconfiguration.xml");
The error is:
{"Error registering object defined in 'assembly [MyLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null], resource [MyLib.Configuration.springconfiguration.xml] at line 5' : Invalid relative resource location 'Gui/SpecialButton/buttonspring.xml' to import object definitions from.\r\n<import resource=\"Gui/SpecialButton/buttonspring.xml\" xmlns=\"http://www.springframework.net\" />"}
I tried several syntaxes, I looked in the documentation and did not find any solution. Has someone an idee of where is the problem?
Update
ok I found by myself: the correct syntaxe is:
<import resource="Gui.SpecialButton.buttonspring.xml"/>
then no errors are raised anymore and everything work fine.

To let everybody know here is the solution I found: in springconfiguration.xml instead of refering to the spring configuration file nested in folders as:
<import resource="Gui/SpecialButton/buttonspring.xml"/>
One has to write:
<import resource="Gui.SpecialButton.buttonspring.xml"/>
#marijn: thank you for your explanation, understanding a solution is better than just finding it ;)

Related

sonar-dotnet-shared-library does not compile due non-existing dependencies, How to make it work?

I am trying to compile in my machine the sonar-csharp-plugin, but in the pom.xml file there is two dependencies that do not exist in the Maven public repositories:
<dependency>
<groupId>org.sonarsource.dotnet</groupId>
<artifactId>sonar-dotnet-tests-library</artifactId>
<version>1.5.0.393</version>
</dependency>
<dependency>
<groupId>org.sonarsource.dotnet</groupId>
<artifactId>sonar-dotnet-shared-library</artifactId>
<version>1.0.1.138</version>
</dependency>
I download the code of both projects and try to compile them and generate the .jar files for each one.
Trying to compile sonar-dotnet-shared-library-1.0.1.138, I installed the https://www.nuget.org/packages/SonarAnalyzer.CSharp/1.20.0 package and proceed to install it in my maven local repository then when I compile sonar-dotnet-shared-library-1.0.1.138 I get :
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (unzip-nuget) on project sonar-dotnet-shared-library: An Ant Build Exception has occured: C:\Temp\sonar-dotnet-shared-library-1.0.1.138\target\analyzer\SonarAnalyzer.Scanner\protobuf does not exist.
[ERROR] around Ant part ...<copy todir="src/main/protobuf">... # 8:35 in C:\Temp\sonar-dotnet-shared-library-1.0.1.138\target\antrun\build-main.xml
I think I am in Maven hell.
What should I do to build the code from the latest release sonar-csharp-plugin??
Edit: when I installed the SonarAnalyzer I used
mvn install:install-file -DgroupId=org.sonarsource.dotnet -DartifactId=SonarAnalyzer.Scanner -Dversion=1.20.0 -Dpackaging=nupkg -Dfile="C:\Temp\SonarAnalyzer.CSharp.1.20.0-RC1.nupkg"
I disable the tasks that generate the error, now the java code start its compilation but I get errors related to
import org.sonarsource.dotnet.protobuf.SonarAnalyzer;
I think that it is a reference to the SonarAnalyzer Dll's, but neither Eclipse nor Maven are able to find it (protobuf is missing)
Edit2:
the POM.XML includes these tasks:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>unzip-nuget</id>
<phase>validate</phase>
<configuration>
<exportAntProperties>true</exportAntProperties>
<tasks>
<unzip src="${sonarAnalyzer.workDirectory}/SonarAnalyzer.Scanner.nupkg" dest="${sonarAnalyzer.workDirectory}/SonarAnalyzer.Scanner/" />
<delete>
<fileset dir="src/main/protobuf" excludes=".gitignore"></fileset>
</delete>
<copy todir="src/main/protobuf">
<fileset dir="${sonarAnalyzer.workDirectory}/SonarAnalyzer.Scanner/protobuf">
<include name="*.proto"/>
</fileset>
</copy>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>compile-protobuf-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<fileset id="fileset" dir="${project.basedir}/src/main/protobuf">
<include name="*.proto" />
</fileset>
<pathconvert refid="fileset" property="protos" pathsep=" " />
<mkdir dir="${project.build.directory}/generated-sources/protobuf" />
<chmod file="${protobuf.compiler}" perm="u+x" />
<exec failonerror="true" executable="${protobuf.compiler}">
<arg value="proto_path=${project.basedir}/src/main/protobuf" />
<arg value="java_out=${project.build.directory}/generated-sources/protobuf" />
<arg line="${protos}" />
</exec>
</target>
</configuration>
</execution>
</executions>
As I understand, in the SonarAnalyzer.Scanner.nupkg should be a protobuf folder, and the content of that folder is copied to src/main/protobuf.....well the SonarAnalyzer.Scanner.nupkg downloaded from Nuget does not contain that folder....so....
guys from Sonar...... Where do I get that nupkg?
I had the same problem, I've found the solution on this thread from SonarQube's Google group.
You need to fetch the missing artifacts from sonarsource's Artifactory server. As suggested by Duarte Meneses, you can add these lines to [user_home]/.m2/settings.xml :
<profiles>
<profile>
<id>sonarsource-repo</id>
<activation>
<property>
<name>!skip-sonarsource-repo</name>
</property>
</activation>
<repositories>
<repository>
<id>sonarsource</id>
<name>SonarSource Central Repository</name>
<url>https://repox.sonarsource.com/sonarsource</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:60</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>sonarsource</id>
<name>SonarSource Central Repository</name>
<url>https://repox.sonarsource.com/sonarsource</url>
<releases>
<enabled>true</enabled>
<!-- no need to always check if new versions are available when
executing a maven plugin without specifying the version -->
<updatePolicy>interval:60</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>false</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
I used the above configuration.I also had mirrors declared in my settings.xml, so I had to exclude sonarsource from the mirrored repositories :
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*,!sonarsource</mirrorOf>
<url>http://ci-server/nexus/content/groups/public</url>
</mirror>
</mirrors>
Of course there are other ways to achieve the same result, for example by declaring a proxy repository in your company's Nexus server.
With this configuration, I built SonarQube successfully.

Access properties of a Test Settings file at runtime

I have a test project which uses MSTest. And I have a testsettings file and have a properties in that file. as below.
<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="local" id="77572268-dd99-4f8c-a660-f5c8c1eec977"
xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Description>These are default test settings for a local test run.</Description>
<Execution>
<TestTypeSpecific>
<UnitTestRunConfig testTypeId="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b">
<AssemblyResolution>
<TestDirectory useLoadContext="true" />
</AssemblyResolution>
</UnitTestRunConfig>
</TestTypeSpecific>
<AgentRule name="Execution Agents">
</AgentRule>
</Execution>
<Properties >
<Property name="AAA" value="val1"></Property>
<Property name="BBB" value="val2"></Property>
</Properties>
</TestSettings>
But how can I access these properties in testsettings file values by name in runtime. How can I do that?
This is what currently I'am trying..
[ClassInitialize]
public static void TestClassInitialize(TestContext context)
{
var sad = context.Properties["AAA"].ToString();
}
And it gives following exception
An exception of type 'System.NullReferenceException' occurred in
TestAutomation.dll but was not handled in user code
Additional information: Object reference not set to an instance of an
object.
And this is not about the System.NullReferenceException and this is about how to access a property in a Test settings file in runtime. So this question is not a duplicate.
I suspect you have not properly configured your .runsettings file.
Follow this link .runsettings file configuration and configure the "SettingsFile" section properly.
Alternatively you can also try "TestRunParameters" section to get this working.
The way you are accessing the properties is not correct. You need to use runsettings file instead.
You were close but you dont need the ending tag of the property tag, here is an example:
<Properties>
<Property name="test" value="testValue"/>
</Properties>
Once that is done you can use the same code to access the data:
context.Properties["AAA"].ToString()

How to manually evaluate msbuild condition?

I'm creating a custom msbuild task that will be processing a configuration from custom XML file. I want to allow to use Condition attribute in that xml file. Syntax of that attribute should be the same as MSBuild Conditions (https://msdn.microsoft.com/en-us/library/7szfhaft.aspx)
How can I evaluate value of that attribute? Is there an existing library that automate that or I'm forced to write my own parser?
So far I was able only to get value of all variables that probably will be necessary to evaluate that conditions (How to access the MSBuild 's properties list when coding a custom task?)
I’m not sure if this will be helpful for you. I was solving the similar problem c++ projects. I was thinking to use Microsoft.Build.BuildEngine.Project class but later changed my mind. Finally I’ve created mine config in msbuild style (including namespace). I’ve enforced importing my config by msbuild (I’ve misused ForceImportAfterCppTargets property). Msbuild evaluated everything for me. Mine injected config (or props/target file) contained target that was injected into build process by overriding some build property (at the project level) in a way my target was called. Mine custom target called mine custom task with passed all necessary properties and items by parameters.
Following content is response on Uriel Jun 12 at 16:26:
Because you've marked question with tag c# I tried to make sample with C# vs 2010.
I made sample really simple. I put task and xml configuration file into one file named my.props. My custom task just prints values of my configuration provided by item. It prints metadata of item.
One think you have to do is to manually modify your .csproj by adding one simple line. After the line where is Microsoft.CSharp.targets imported add import of custom my.props file.
This sample expects your my.props is in the same directory as .csproj.
Diff style change:
+
Content of my.props:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="MyTool" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<Cfg ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
<![CDATA[
if (Cfg.Length > 0)
{
for (int i = 0; i < Cfg.Length; ++i)
{
ITaskItem item = Cfg[i];
string value1 = item.GetMetadata("Value1");
string value2 = item.GetMetadata("Value2");
Log.LogMessage(MessageImportance.High, "MyTool: {0} - {1}", value1, value2);
}
}
]]>
</Code>
</Task>
</UsingTask>
<ItemDefinitionGroup Condition=" '$(Configuration)' == 'Release' ">
<MyConfig>
<Value1>Hello</Value1>
<Value2>World</Value2>
</MyConfig>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition=" '$(Configuration)' == 'Debug' ">
<MyConfig>
<Value1>Hello</Value1>
<Value2>Debug world</Value2>
</MyConfig>
</ItemDefinitionGroup>
<ItemGroup>
<MyConfig Include="MyCfg" />
</ItemGroup>
<Target Name="AfterBuild">
<MyTool Cfg="#(MyConfig)" />
</Target>
</Project>

Expand MSBuild Properties containing wildcard into Items

I am trying to write MSBuild script that will perform some action (eg. print its path) on an arbitrary files (specified as a property on a command line) in some predefined directory (F:\Files).
Given the following directory structure
F:\Files\TextFile.txt
F:\Files\Subdir1\ImageFile.bmp
F:\Files\Subdir1\SubSubdir\ImageFile2.bmp
F:\Files\Subdir1\SubSubdir\TextFile2.txt
And MSBuild Script
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="PrintNames" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetDir>F:\Files</TargetDir>
</PropertyGroup>
<ItemGroup>
<Files Include="$(TargetDir)\$(InputFiles)"/>
</ItemGroup>
<Target Name="PrintNames">
<Message Text="Files: #(Files, ', ')" />
</Target>
</Project>
running the script with InputFiles set to "**\*.bmp;**\*.txt" works fine only for bmp files. Txt files are taken from the current working directory, not from "F:\Files"
There are two problems that you have to solve:
$(InputFiles) is specifed as a scalar property, but you want to interprete it as an array
$(InputFiles) contains wildcards you want to expand after you do transformation on the list of patterns in $(InputFiles).
It is easy to solve either of two problems separately, but combination of the two is actually tricky. I have one possible solution, and it works, but the downside is you have to encode '*' characters in your pattern definition.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="PrintNames" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetDir>c:\temp\MyContent</TargetDir>
<InputFilesRelativeEsc>%2A%2A\%2A.bmp;%2A%2A\%2A.txt</InputFilesRelativeEsc>
</PropertyGroup>
<Target Name="PrintNames">
<ItemGroup>
<_TempGroup Include="$(InputFilesRelativeEsc)" />
</ItemGroup>
<CreateItem Include="#(_TempGroup->'$(TargetDir)\%(Identity)')">
<Output TaskParameter="Include" ItemName="_EvaluatedGroup" />
</CreateItem>
<Message Text="_EvaluatedGroup: %(_EvaluatedGroup.FullPath)" />
</Target>
</Project>
It works as follows. Property InputFilesRelativeEsc is a list of relative file patterns. Notice wildcard characters are encoded (%2A is a hex code for asterisk). Since the wildcards encoded, the group _TempGroup does not attempt to search for and extract list of files while you Include this patterns into this group. Now _TempGroup is a group which consists of two elements: **\*.bmp and **\*.txt. Now that you have a real group you can transform it. The only complication is that the normal MSBuild mechanism of running transform does not expand wildcards. You have to use older CreateItem task. The CreateItem task is actually declared deprecated by MSBuild team, but it still works.

Could not find schema information for the element 'castle'

I'm creating a Custom tag in my web.config. I first wrote the following entry under the configSections section.
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
Castle.Windsor" />
But, when I try to create a castle node inside the configuration node as below
<castle>
<components>
</components>
</castle>
I get the following error message:"*Could not find schema information for the element '**castle'*." "***Could not find schema information for the element '**components'***."
Am I missing something? I can't find why. And, if I run the application anyway, I get the following error "Could not find section 'Castle' in the configuration file associated with this domain."
Ps.// The sample comes from "Pro ASP.NET MVC Framework"/Steven Sanderson/APress ISBN-13 (pbk): 978-1-4302-1007-8" on page 99.
Thank you for the help
============================================================
Since I believe to have done exactly what's said in the book and did not succed, I ask the same question in different terms. How do I add a new node using the above information?
=============================================================================
Thank you. I did what you said and do not have the two warnings. However, I've go a big new warning:
"The element 'configuration' in namespace 'MyWindsorSchema' has invalid child element 'configSections' in namespace 'MyWindsorSchema'. List of possible elements expected: 'include, properties, facilities, components' in namespace 'MyWindsorSchema'."
What you get is not an error that will prevent you from running your application. It is just a warning that Visual Studio emits because it does not know the castle node in a config file. You could use a schema to enable intellisense. Download the Castle Windsor Schema file and take a look at the readme.txt inside. It tells you to put windsor.xsd somewhere on your hard drive and then reference it in the config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="MyWindsorSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="MyWindsorSchema file://S:\Common\Windsor\windsor.xsd">
<configSections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<components>
</components>
</castle>
</configuration>

Categories