I need to build a project programmatically for a .csproj I am creating on the fly.
While searching Google I found the classes and API provided by the MS for the MSBuild Engine. With that information, I create a process which executes msbuild.exe and then reads the output, but now I want to use the namespace Microsoft.Build.Execution to build the project. This is my program:
public class Compiler
{
private static string locationOfMSBuilldEXE = "";
public static void Build(string msbuildFileName)
{
BuildManager manager = BuildManager.DefaultBuildManager;
ProjectInstance projectInstance = new ProjectInstance(msbuildFileName);
var result = manager.Build(new BuildParameters()
{
DetailedSummary = true
},
new BuildRequestData(projectInstance, new string[] { "Build" }));
var buildResult = result.ResultsByTarget["Build"];
var buildResultItems = buildResult.Items;
string s = "";
}
}
The results show that this is building fine, but I need to know the detailed output from the compile and how to view it. It would be really helpful if someone can give me link to a good tutorial or a book on MSBuild.
Thanks #ritchmelton. Though I figured it out myself.
Here is my code : I have used an in built logger ConsoleLogger
public class Compiler
{
private static string locationOfMSBuilldEXE = "";
public static void Build(string msbuildFileName)
{
ConsoleLogger logger = new ConsoleLogger(LoggerVerbosity.Normal);
BuildManager manager = BuildManager.DefaultBuildManager;
ProjectInstance projectInstance = new ProjectInstance(msbuildFileName);
var result = manager.Build(
new BuildParameters()
{
DetailedSummary = true,
Loggers = new List<ILogger>(){logger}
},
new BuildRequestData(projectInstance, new string[] { "Build" }));
var buildResult = result.ResultsByTarget["Build"];
var buildResultItems = buildResult.Items;
string s = "";
}
}
You need to add a instance of a class that implements the ILogger interface to your BuildParameters. You can add a new instance of one of the supplied loggers in the Microsft.Build.Logging namespace, or you can implement ILogger yourself as it is very small and there is a helper class in the Microsoft.Build.Utilities namespace called Logger that is easy to extend.
Build loggers
ILogger interface
Logger helper
If you just want to build a project or solution, without elaborate parameters, you can do it more simply. Pseudocode:
using namespace Microsoft.Build.Evaluation;
var p = Project.Load("path to project");
p.SetGlobalProperty("Configuration", "Release");
p.Build(...);
That's it! BuildParameters and so forth are for quite advanced scenarios. Visual Studio itself uses them.
Dan (msbuild dev)
Related
I'm trying to test some code in LINQPad. However, the base class calls Configuration Manager. How can I simulate that when testing in LINQPad.
void Main()
{
var tRepo = new TestRepository();
var result = tRepo.GetAsync(1);
result.Dump();
}
public partial class TestRepository : BaseRepository<Customer>, ICustomerRepository
{
// Here base throws the errror
public TestRepository() : base("DbConnString")
{
}
}
Here's the constructor for BaseRepository (from a compiled DLL, so not editable in LINQPad):
protected BaseRepository(string connectionStringName)
{
var connectionString = ConfigurationManager.ConnectionStrings[connectionStringName];
Connection = new SqlConnection(connectionString.ConnectionString);
Connection.Open();
}
The answer can be found on the LinqPad website FAQ
http://www.linqpad.net/faq.aspx
I'm referencing a custom assembly that reads settings from an application configuration file (app.config). Where should I put my application config file so that LINQPad queries will pick it up?
Create a file called linqpad.config in the same folder as LINQPad.exe and put your configuration data there. Don't confuse this with linqpad.exe.config:
•linqpad.exe.config is for the LINQPad GUI
•linqpad.config is for your queries.
Something that might be useful for you, I created it some time ago.
This is an extension method, which you can use to force the reload of configuration from specific file. It uses reflection to change the private fields in the manager, clears the configuration and then conditionally reloads it. It is much easier than manually editing the config file of LINQPad.
public static void ForceNewConfigFile(this Type type, bool initialize = true)
{
var path = type.Assembly.Location + ".config";
if (!File.Exists(path))
throw new Exception("Cannot find file " + path + ".");
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
var typeOfConfigManager = typeof(ConfigurationManager);
typeOfConfigManager.GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, 0);
typeOfConfigManager.GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, null);
var typeOfClientConfigPaths = typeOfConfigManager.Assembly.GetTypes().Where(x => x.FullName == "System.Configuration.ClientConfigPaths").Single();
typeOfClientConfigPaths.GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, null);
if (initialize)
{
var dummy = ConfigurationManager.AppSettings;
}
}
Example usage:
typeof(SomeType).ForceNewConfigFile();
System.Configuration.ConfigurationManager.AppSettings.Dump();
SomeType is just a type contained in the assembly, which will be used as a source for location of the config file. Assumption is: configuration file exists beside the DLL file and is named {Assembly.Location}.config.
I went looking for this as well, but didn't want to create a whole new file, point, have other share that file etc. So I added in the ConfigurationBuilder, created a dictionary and added it to a InMemoryCollection.
#nullable enable
private IConfiguration _config;
void Main()
{
var builder = new ConfigurationBuilder();
var dictonary = new Dictionary<string, string>
{
{"Authentication:SecretKey","SuperSecret"}
};
builder.AddInMemoryCollection(dictonary);
_config = builder.Build();
_config.GetValue<string>("Authentication:SecretKey").Dump();
}
Example
I'm moving from the easy world of Ninject over to the fast world of Simple Injector, but I'm getting stuck with SolrNet. A lot of the popular IoC's have SolrNet integrations already, but not SimpleInjector.
I've started creating my own integration by converting the Unity stuff found at
https://github.com/mausch/SolrNet/tree/master/Unity.SolrNetIntegration
Does anyone either know if this has been done before ( by someone with better knowledge than me) or can anyone convert the below to use simple injector:
private static void RegisterSolrOperations(SolrCore core, IUnityContainer container, bool isNamed = true) {
var ISolrReadOnlyOperations = typeof (ISolrReadOnlyOperations<>).MakeGenericType(core.DocumentType);
var ISolrBasicOperations = typeof (ISolrBasicOperations<>).MakeGenericType(core.DocumentType);
var ISolrOperations = typeof (ISolrOperations<>).MakeGenericType(core.DocumentType);
var SolrServer = typeof (SolrServer<>).MakeGenericType(core.DocumentType);
var registrationId = isNamed ? core.Id : null;
var injectionConstructor = new InjectionConstructor(
new ResolvedParameter(ISolrBasicOperations, registrationId),
new ResolvedParameter(typeof (IReadOnlyMappingManager)),
new ResolvedParameter(typeof (IMappingValidator)));
container.RegisterType(ISolrOperations, SolrServer, registrationId, injectionConstructor);
container.RegisterType(ISolrReadOnlyOperations, SolrServer, registrationId, injectionConstructor);
}
So far I have:
private static void RegisterSolrOperations(SolrCore core, Container container, bool isNamed = true)
{
var ISolrReadOnlyOperations = typeof(ISolrReadOnlyOperations<>).MakeGenericType(core.DocumentType);
var ISolrBasicOperations = typeof(ISolrBasicOperations<>).MakeGenericType(core.DocumentType);
var ISolrOperations = typeof(ISolrOperations<>).MakeGenericType(core.DocumentType);
var SolrServer = typeof(SolrServer<>).MakeGenericType(core.DocumentType);
var registrationId = isNamed ? core.Id : null;
var injectionConstructor = new InjectionConstructor(
new ResolvedParameter(ISolrBasicOperations, registrationId),
new ResolvedParameter(typeof(IReadOnlyMappingManager)),
new ResolvedParameter(typeof(IMappingValidator)));
container.Register(ISolrOperations, SolrServer, registrationId, injectionConstructor);
container.Register(ISolrReadOnlyOperations, SolrServer, registrationId, injectionConstructor);
}
But the injectionConstructor lines are obviously not for simple injector.
Thanks
UPDATE
I should have mention that I was using solrNet 0.4.x so config will be slightly different.
After looking at Steven's answer I now have:
container.RegisterSingle<IReadOnlyMappingManager>(
new MemoizingMappingManager(new AttributesMappingManager()));
container.Register<ISolrDocumentPropertyVisitor, DefaultDocumentVisitor>();
container.Register<ISolrFieldParser, DefaultFieldParser>();
container.RegisterAllOpenGeneric(typeof(ISolrDocumentActivator<>), typeof(SolrDocumentActivator<>));
container.RegisterAllOpenGeneric(typeof(ISolrDocumentResponseParser<>), typeof(SolrDocumentResponseParser<>));
container.Register<ISolrFieldSerializer, DefaultFieldSerializer>();
container.Register<ISolrQuerySerializer, DefaultQuerySerializer>();
container.Register<ISolrFacetQuerySerializer, DefaultFacetQuerySerializer>();
container.RegisterAllOpenGeneric(typeof(ISolrAbstractResponseParser<>), typeof(DefaultResponseParser<>));
container.Register<ISolrHeaderResponseParser, HeaderResponseParser<string>>();
container.Register<ISolrExtractResponseParser, ExtractResponseParser>();
container.RegisterAll<IValidationRule>(new[] {
typeof(MappedPropertiesIsInSolrSchemaRule),
typeof(RequiredFieldsAreMappedRule),
typeof(UniqueKeyMatchesMappingRule)
});
container.Register<ISolrConnection>(() => new SolrConnection(url));
container.RegisterAllOpenGeneric(typeof(ISolrMoreLikeThisHandlerQueryResultsParser<>), typeof(SolrMoreLikeThisHandlerQueryResultsParser<>));
container.RegisterAllOpenGeneric(typeof(ISolrQueryExecuter<>), typeof(SolrQueryExecuter<>));
container.RegisterAllOpenGeneric(typeof(ISolrDocumentSerializer<>), typeof(SolrDocumentSerializer<>));
container.RegisterAllOpenGeneric(typeof(ISolrBasicOperations<>), typeof(SolrBasicServer<>));
container.RegisterAllOpenGeneric(typeof(ISolrBasicReadOnlyOperations<>), typeof(SolrBasicServer<>));
container.RegisterAllOpenGeneric(typeof(ISolrOperations<>), typeof(SolrServer<>));
container.RegisterAllOpenGeneric(typeof(ISolrReadOnlyOperations<>), typeof(SolrServer<>));
container.Register<ISolrSchemaParser, SolrSchemaParser>();
container.Register<ISolrDIHStatusParser, SolrDIHStatusParser>();
container.Register<IMappingValidator, MappingValidator>();
but with this I get the error:
The constructor of the type MappingValidator contains the parameter of
type IValidationRule[] with name 'rules' that is not registered.
Please ensure IValidationRule[] is registered in the container, or
change the constructor of MappingValidator.
That Unity configuration looks overly complex, the Ninject configuration is much simpler. I'm not sure why that Unity configuration is that complex.
I grabbed the Ninject configuration and translated to Simple Injector. Since I'm not familiar with Solr, it could be that Solr depends a few extra tweaks in the Simple Injector configuration, but I think this will get you going:
// using SimpleInjector;
public static void Configure(Container container, string serverURL)
{
container.RegisterInstance<IReadOnlyMappingManager>(
new MemoizingMappingManager(new AttributesMappingManager()));
container.Register<ISolrDocumentPropertyVisitor, DefaultDocumentVisitor>();
container.Register<ISolrFieldParser, DefaultFieldParser>();
container.Register(typeof(ISolrDocumentActivator<>), typeof(SolrDocumentActivator<>));
container.Register(typeof(ISolrDocumentResponseParser<>), typeof(SolrDocumentResponseParser<>));
container.Register<ISolrFieldSerializer, DefaultFieldSerializer>();
container.Register<ISolrQuerySerializer, DefaultQuerySerializer>();
container.Register<ISolrFacetQuerySerializer, DefaultFacetQuerySerializer>();
container.Collection.Register(typeof(ISolrResponseParser<>), new[] {
typeof(ResultsResponseParser<>),
typeof(HeaderResponseParser<>),
typeof(FacetsResponseParser<>),
typeof(HighlightingResponseParser<>),
typeof(MoreLikeThisResponseParser<>),
typeof(SpellCheckResponseParser<>),
typeof(StatsResponseParser<>),
typeof(CollapseResponseParser<>)
});
container.Register<ISolrHeaderResponseParser, HeaderResponseParser<string>>();
container.Collection.Register<IValidationRule>(new[] {
typeof(MappedPropertiesIsInSolrSchemaRule),
typeof(RequiredFieldsAreMappedRule),
typeof(UniqueKeyMatchesMappingRule)
});
container.RegisterInstance<ISolrConnection>(new SolrConnection(this.serverURL));
container.Register(typeof(ISolrQueryResultParser<>), typeof(SolrQueryResultParser<>));
container.Register(typeof(ISolrQueryExecuter<>), typeof(SolrQueryExecuter<>));
container.Register(typeof(ISolrDocumentSerializer<>), typeof(SolrDocumentSerializer<>));
container.Register(typeof(ISolrBasicOperations<>), typeof(SolrBasicServer<>));
container.Register(typeof(ISolrBasicReadOnlyOperations<>), typeof(SolrBasicServer<>));
container.Register(typeof(ISolrOperations<>), typeof(SolrServer<>));
container.Register(typeof(ISolrReadOnlyOperations<>), typeof(SolrServer<>));
container.Register<ISolrSchemaParser, SolrSchemaParser>();
container.Register<IMappingValidator, MappingValidator>();
}
We would like the use the bundling mechanism of System.Web.Optimization in combination with the Less transformer.
The problem is that the same application/server serves pages for different branded websites. So depending on the 'SiteContext' the same .less files are used but different values should be used by the .less variables. So we want the (re)use the same less files but with different variables depending on the context of the request.
I tried a couple of different theories:
In all 3 cases I setup different bundles depending on the SiteContext.
1 inject an #import directive with the themed variables by using a custom VirtualPathProvider that intercepts the variables.less file.
So I have:
the styling file eg: header.less (imports the variables file)
the variables file: variables.less
a themed variables file: variables-theme.less (injected in variables.less via the VirtualPathProvider)
This is not working because the BundleTransformer cache sees this as the same file and doesn't know about the SiteContext. The cache key is based on the Url of the IAsset and we cannot influence this behavior.
2 Replace the variables.less import by variables-themed.less with an custom transformer that runs before the Less transformer.
Again no luck, same caching issues.
And as a side effect, the extra transformer was not called in debug because the assets are not bundled but called individually by the LessAssetHandler. This could be solved by writing your own AssetHandler that calls all required transformers.
3 create themed Asset names that are resolved by a custom VirtualPathProvider
Eg. Add header-themeX.less to the bundle, this file doesn't exist but you resolve this file to header.less and use method 2 to set the correct variables file import. (replace the import of the variables.less to the themed version).
Once again no luck. I think this could solve the caching issue if it wasn't for the Bundle.Include(string virtualPath) that does a File.Exists(path) internally. It doesn't pass via the CustomVirtualPathProvider.
Am I looking to deep to solve this?
All ideas are welcome, I can imagine that this will become a problem to more and more people as the System.Web.Optimization library gets more popular...
Keep in mind that:
we have a lot of .less/css files
we will have 5 or so themes
we like to keep things working in visual studio (that is why header.less has a ref. to variables.less)
Thanks for any feedback.
Michael!
You use the Microsoft ASP.NET Web Optimization Framework and the Bundle Transformer in multi-tenant environment, so you need to replace some components of the System.Web.Optimization and create own versions of the debugging HTTP-handlers (see «Problem: LESS file imports are added to BundleResponse.Files collection» discussion). As far as I know, Murat Cakir solve all these problems in the SmartStore.NET project.
In the Bundle Transformer there are 2 ways to inject of LESS-variables:
Look a properties GlobalVariables and ModifyVariables of LESS-translator:
using System.Collections.Generic;
using System.Web.Optimization;
using BundleTransformer.Core.Builders;
using BundleTransformer.Core.Orderers;
using BundleTransformer.Core.Transformers;
using BundleTransformer.Core.Translators;
using BundleTransformer.Less.Translators;
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
var nullBuilder = new NullBuilder();
var nullOrderer = new NullOrderer();
var lessTranslator = new LessTranslator
{
GlobalVariables = "my-variable='Hurrah!'",
ModifyVariables = "font-family-base='Comic Sans MS';body-bg=lime;font-size-h1=50px"
};
var cssTransformer = new CssTransformer(new List<ITranslator>{ lessTranslator });
var commonStylesBundle = new Bundle("~/Bundles/BootstrapStyles");
commonStylesBundle.Include(
"~/Content/less/bootstrap-3.1.1/bootstrap.less");
commonStylesBundle.Builder = nullBuilder;
commonStylesBundle.Transforms.Add(cssTransformer);
commonStylesBundle.Orderer = nullOrderer;
bundles.Add(commonStylesBundle);
}
}
Create a custom item transformation:
using System.Text;
using System.Web.Optimization;
public sealed class InjectContentItemTransform : IItemTransform
{
private readonly string _beforeContent;
private readonly string _afterContent;
public InjectContentItemTransform(string beforeContent, string afterContent)
{
_beforeContent = beforeContent ?? string.Empty;
_afterContent = afterContent ?? string.Empty;
}
public string Process(string includedVirtualPath, string input)
{
if (_beforeContent.Length == 0 && _afterContent.Length == 0)
{
return input;
}
var contentBuilder = new StringBuilder();
if (_beforeContent.Length > 0)
{
contentBuilder.AppendLine(_beforeContent);
}
contentBuilder.AppendLine(input);
if (_afterContent.Length > 0)
{
contentBuilder.AppendLine(_afterContent);
}
return contentBuilder.ToString();
}
}
And register this transformation as follows:
using System.Web.Optimization;
using BundleTransformer.Core.Orderers;
using BundleTransformer.Core.Bundles;
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
var nullOrderer = new NullOrderer();
const string beforeLessCodeToInject = #"#my-variable: 'Hurrah!';";
const string afterLessCodeToInject = #"#font-family-base: 'Comic Sans MS';
#body-bg: lime;
#font-size-h1: 50px;";
var commonStylesBundle = new CustomStyleBundle("~/Bundles/BootstrapStyles");
commonStylesBundle.Include(
"~/Content/less/bootstrap-3.1.1/bootstrap.less",
new InjectContentItemTransform(beforeLessCodeToInject, afterLessCodeToInject));
commonStylesBundle.Orderer = nullOrderer;
bundles.Add(commonStylesBundle);
}
}
Both ways have disadvantage: the injection of LESS-variables does not work in debug mode.
I am trying to test mef and mefcontrib in asp.net mvc2 app but i got an error:
Cannot cast the underlying exported value of type LoggerExtSys.Domain.WebLogger
(ContractName="LoggerExtSys.Domain.IWebLogger") to type LoggerExtSys.Domain.IWebLogger.
My test project here
code in Global.asax:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
var catalog = new CatalogBuilder()
.ForAssembliesInDirectory(HttpRuntime.BinDirectory, "*ExtSys.dll")
.Build();
// Create interception configuration
var cfg = new InterceptionConfiguration()
.AddInterceptor(new StartableStrategy());
// Create the InterceptingCatalog with above configuration
var interceptingCatalog = new InterceptingCatalog(catalog, cfg);
// Create the container
var container = new CompositionContainer(interceptingCatalog);
// exception here
var barPart = container.GetExportedValue<IWebLogger>();
barPart.Debug("Test");
}
Exception when i try to get GetExportedValue
code in WebLogger:
[Export(typeof(IWebLogger))]
public class WebLogger : IWebLogger
{
#region IWebLogger Members
public void Debug(string str)
{
}
#endregion
#region ICoreExtension Members
public void Initialize()
{
}
#endregion
}
But in desktop app all working good.
How to fix it? Thanks for all
Ok, the problem was in code block which load assemblies:
public AggregateCatalog ForAssembliesInDirectory(string directory, string pattern)
{
IList<ComposablePartCatalog> _catalogs = new List<ComposablePartCatalog>();
var dir = new DirectoryInfo(directory);
Assembly assembly;
foreach (var file in dir.GetFiles(pattern))
{
assembly = Assembly.LoadFile(file.FullName);
_catalogs.Add(new AssemblyCatalog(assembly));
}
return new AggregateCatalog(_catalogs);
}
After all test i remove it and use DirectoryCatalog. I dont know why but its work in desktop and web app.
Who will tell me why my old code not working in web app will get accepted answer and 50 bounty. Thanks for all
I think the problem is either here:
[Export(typeof(IWebLogger))]
public class WebLogger : IWebLogger
{
or in the way you handle type referencing and resolution.
I would try to change the line:
var barPart = container.GetExportedValue<IWebLogger>();
into:
var barPart = container.GetExportedValue<WebLogger>();
or you can also try to always use fully qualified names so not only IWebLogger but put its full namespace before.
you say this works well in windows based application, what assemblies did you reference in that project or how do you write in there the content of your Application_Start event handler? Are you sure it's absolutely the same?
I have a string variable contain:
string classCode = "public class Person { public string Name{get;set;} }";
How can I create an instance of an object from the classCode ?
like
object obj = CreateAnInstanceAnObject(classCode);
You'll need to use CodeDom to compile an in-memory assembly, and then use reflection to create the type.
Here's a sample article on MSDN that walks through the process of code generation.
Once you've compiled the code, you can use Activator.CreateInstance to create an instance of it.
Building on the answers from above, here is a working demo to generate, compile and instantiate a class from an in-memory assembly:
namespace DynamicCompilation
{
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
internal static class Program
{
private static void Main()
{
var ccu = new CodeCompileUnit();
var cns = new CodeNamespace("Aesop.Demo");
cns.Imports.Add(new CodeNamespaceImport("System"));
var ctd = new CodeTypeDeclaration("Test")
{
TypeAttributes = TypeAttributes.Public
};
var ctre = new CodeTypeReferenceExpression("Console");
var cmie = new CodeMethodInvokeExpression(ctre, "WriteLine", new CodePrimitiveExpression("Hello World!"));
var cmm = new CodeMemberMethod
{
Name = "Hello",
Attributes = MemberAttributes.Public
};
cmm.Statements.Add(cmie);
ctd.Members.Add(cmm);
cns.Types.Add(ctd);
ccu.Namespaces.Add(cns);
var provider = new CSharpCodeProvider();
var parameters = new CompilerParameters
{
CompilerOptions = "/target:library /optimize",
GenerateExecutable = false,
GenerateInMemory = true
};
////parameters.ReferencedAssemblies.Add("System.dll");
var results = provider.CompileAssemblyFromDom(parameters, ccu);
if (results.Errors.Count == 0)
{
var t = results.CompiledAssembly.GetType("Aesop.Demo.Test");
var inst = results.CompiledAssembly.CreateInstance("Aesop.Demo.Test");
t.InvokeMember("Hello", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, inst, null);
}
Console.ReadLine();
}
}
}
Simple put you cannot do this in one line as you are attempting. It is possible to create an instance of an existing class via it's name and one of the overloads of Activator.CreateInstance.
What you are trying to achieve here though is quite different. You are attempting to both 1) define a new class type and 2) create an instance of it. Defining new metadata in the running process dynamically is very difficult to achieve with static languages like C#. It requires a significant amount of work that can't easily be put into a StackOverflow answer.
The following project should guide you in what your trying to accomplish:
RunTime Code Compilation
However, if you are attempting to write code at runtime, you may want to rethink your architecture. You may be creating more of a headache for yourself than you need to be.
What are you trying to accomplish by creating this object?