I have written an C# WPF Application to compile code during runtime. The application does basically the following steps
click button [Compile stuff]
Create code files via StreamWriter
Build code files using the Microsoft.Build.Execution.BuildManager class
Uses reflection to access the DLL file (Assembly.LoadFrom(filePath))
creates an instanz of the class contained in the dll (assembly.CreateInstance(NamespaceName + "." + ClassName))
I works fine, but only once (I need to restart the application to do it again)
This is what happens during the next execution
click button [Compile stuff]
Create code files via StreamWriter
Build code files using the Microsoft.Build.Execution.BuildManager class
->Produces an error saying that the DLL file is locked.
The process cannot access the file 'DLL\generatedflexform.dll' because
it is being used by another process
The problem doesn't occur when I leave out step 2 because then the code files are the same. Therefore the BuildManager doesn't recreate/copy the dll.
I need to figure out how to release the DLL after the BuildManager has done his job. This is because the code files are likely to change very often and otherwise i have to close and re-open the application for every code change.
EDIT: My first thought was that the BuildManager causes the locking but this is not the case.
I rather think the locking happens when I try to load the DLL. I will try the Shadow Copy think (as mentioned by #granadaCoder).
private Window LoadWindowFromDll(string filePathToDll)
{
var assembly = Assembly.LoadFrom(filePathToDll);
var window = assembly.CreateInstance(NamespaceName + "." + ClassName) as Window;
return window;
}
Solution:
I had to change the LoadWindowFromDllmethod to avoid the DLL locking
private Window LoadWindowFromDll(string filePathToDll)
{
byte[] readAllBytes = File.ReadAllBytes(filePathToDll);
Assembly assembly = Assembly.Load(readAllBytes);
var window = assembly.CreateInstance(NamespaceName + "." + ClassName) as Window;
return window;
}
But somehow the pdb file was locked which causes the build to failed when I try to execute it twice.
I fixed this behavior by adding one line to my build file:
<DebugType>none</DebugType>
Here is the complete build file:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<AssemblyName>generatedflexform</AssemblyName>
<OutputPath>DLL\</OutputPath>
<OutputType>Library</OutputType>
<DebugType>none</DebugType>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Page Include="MyForm.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="MyForm.xaml.cs">
<DependentUpon>MyForm.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
And here comes the method which does the compile magic>
public Window BuildXamlWindowFromStrings(string xaml, string codeBehind)
{
//Erstellen der Codefiles (XAML und CodeBehind)
this.CreateCodeFile(codeBehind);
this.CreateXamlFile(xaml);
//Erstellen der project file
this.CreateProjectFile();
//Build der DLL
//using (var buildManager = BuildManager.DefaultBuildManager)
using (var buildManager = new BuildManager())
{
var result = buildManager.Build(this.CreateBuildParameters(), this.CreateBuildRequest());
if (result.OverallResult == BuildResultCode.Success)
{
return this.LoadWindowFromDll(FolderPath + DllRelativeFilePath + NamespaceName + DllFileExtension);
}
}
//Error handling
var stringbuilder = new StringBuilder();
using (var reader = new StreamReader(DebuggerLogFileName))
{
stringbuilder.Append(reader.ReadToEnd());
}
throw new CompilerException(stringbuilder.ToString());
}
Helper Methods:
private BuildParameters CreateBuildParameters()
{
var projectCollection = new ProjectCollection();
var buildLogger = new FileLogger { Verbosity = LoggerVerbosity.Detailed, Parameters = "logfile=" + DebuggerLogFileName };
var buildParameters = new BuildParameters(projectCollection) { Loggers = new List<ILogger>() { buildLogger } };
return buildParameters;
}
private BuildRequestData CreateBuildRequest()
{
var globalProperties = new Dictionary<string, string>();
var buildRequest = new BuildRequestData(FolderPath + ProjectFileName, globalProperties, null,
new string[] { "Build" }, null, BuildRequestDataFlags.ReplaceExistingProjectInstance);
return buildRequest;
}
Check out "Shadow Copying".
Shadow copying enables assemblies that are used in an application domain to be updated without unloading the application domain. This is particularly useful for applications that must be available continuously.
http://msdn.microsoft.com/en-us/library/ms404279.aspx
See also:
http://gotchahunter.net/2010/12/net-how-do-you-load-an-assembly-programmatically-and-avoid-a-file-lock/
and
http://blogs.msdn.com/b/junfeng/archive/2004/02/09/69919.aspx
Related
I'm trying to make a function with c# with fn project, I did this job with an Dockerized linux containerized API with Visual Studio 2022 and it works great.
The problem is, when I try to make the same with fn on a VM with Centos 9 it doesn't work and I dont know how to view the errors/exceptions. The only message I get is:
{"message": "error receiving function response"}
I modify my Dockerfile like next
FROM fnproject/dotnet:6.0-1.0.9-dev as build-stage
WORKDIR /function
COPY . .
RUN dotnet sln add src/Function/Function.csproj
RUN dotnet build -c Release
RUN dotnet publish src/Function/Function.csproj -c Release -o out
FROM fnproject/dotnet:6.0-1.0.9
WORKDIR /function
COPY --from=build-stage /function/out/ /function/
ENTRYPOINT ["dotnet", "Function.dll"]
CMD ["Function:Encoder:encode"]
And I show my func.yaml
schema_version: 20180708
name: encode-image
version: 0.0.133
runtime: dotnet6.0
build_image: fnproject/dotnet:6.0-1.0.9-dev
run_image: fnproject/dotnet:6.0-1.0.9
cmd: Function:Encoder:encode
entrypoint: dotnet Function.dll
memory: 2048
timeout: 300
triggers:
- name: encode-image
type: http
source: /api/v1/encode-image
And my Function.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<!--Nullable>enable</Nullable-->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fnproject.Fn.Fdk" Version="1.0.9" />
<PackageReference Include="DlibDotNet" Version="19.21.0.20220724" />
<PackageReference Include="DlibDotNet.Extensions" Version="19.18.0.20200428" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="12.2.2" />
<PackageReference Include="Magick.NET.Core" Version="12.2.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
<ItemGroup>
<None Update="Assets\dlib\dlib_face_recognition_resnet_model_v1.dat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Assets\dlib\shape_predictor_5_face_landmarks.dat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
I was trying to focus the code which generates the problem and I think is related with next:
detector = Dlib.GetFrontalFaceDetector();
shapePredictor = ShapePredictor.Deserialize("Assets/dlib/shape_predictor_5_face_landmarks.dat");
net = DlibDotNet.Dnn.LossMetric.Deserialize("Assets/dlib/dlib_face_recognition_resnet_model_v1.dat");
And when I call the function with Postman I get:
Dont know how to discover the problem. I think is realted to the load of .dat files or some time the fn framework waits or I dont know.
At last, my method which is called:
public string encode(string strRequest)
{
// Lets generate basic response
Dictionary<string, object> mapResponse = new Dictionary<string, object>();
//List<string> lstErrors = new List<string>();
try
{
JObject jsonRequest = new JObject();
jsonRequest = JObject.Parse(strRequest);
// Lets validate options from request
Dictionary<string, object> mapRequest = validateParameters(jsonRequest);
// Lets get valid parameters
Dictionary<string, object> mapParameters = (Dictionary<string, object>)mapRequest["parameters"];
// If debug mode is true, then add debug info
if (((bool)mapParameters["debug"]) == true) mapResponse["debugging"] = mapParameters;
// Lets form the response
mapResponse["errors"] = mapRequest["errors"];
mapResponse["result"] = string.Empty;
// If there are not errors with parameters and their values then calculate encoding
if (((List<string>) mapRequest["errors"]).Count == 0)
{
// Thinking in future updates, we are going to use a factory
IEncoder encoder = EncoderFactory.createEncoder((string)mapParameters["algorithm"]);
// Lets add the resulting object to the response
mapResponse["result"] = encoder.Encode(mapParameters);
//if(((Encoder_FaceRecognition) encoder).resourceLoaded==true) return "OK";
//return "NOK";
}
}
catch (System.Exception exception)
{
((List<string>)mapResponse["errors"]).Insert(0, "<ERROR> Some exception has occurred." + exception.StackTrace);
((List<string>)mapResponse["errors"]).Insert(0, "<ERROR> Some exception has occurred." + exception.Message);
}
return "<EXITO>";
//return JsonConvert.SerializeObject( mapResponse );
}
I am quite new to the gRPC world and I can't understand and build the project because it is saying that file is not found and the import is not correct. I want to make the models reusable.
greet.proto file here:
syntax = "proto3";
option csharp_namespace = "DemoAppGrpc";
package greet;
import "Enums/gameType.proto";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1;
GameType gameType = 2;
}
message HelloReply {
string message = 1;
}
gameType.proto file type here:
syntax = "proto3";
option csharp_namespace = "DemoAppGrpc";
package greet;
enum GameType {
None = 0;
Open = 1;
Free = 2;
}
My project/folder structure that I want to have:
And my project solution file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<Protobuf Include="Protos\*.proto" GrpcServices="Server">
<GrpcServices>Server</GrpcServices>
<Access>Public</Access>
<ProtoCompile>True</ProtoCompile>
<CompileOutputs>True</CompileOutputs>
<OutputDir>obj\Debug\net6.0\</OutputDir>
<Generator>MSBuild:Compile</Generator>
</Protobuf>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.40.0" />
</ItemGroup>
</Project>
So how can I achieve to import nested files?
I have the following which was working fine in .NetCore2.1 with SDKs
Microsoft.AspNetCore.App(2.1.1)
Microsoft.NetCore.App(2.1.0)
My code is
public static int TransitTime(string postcode, ApiDbContext con)
{
var query = "SELECT top 1 Mins from Transit where postcode = #Postcode order by mins desc;";
var p1 = new SqlParameter("#Postcode",postcode);
var result = 0;
using (var dr = con.Database.ExecuteSqlQuery(query,p1))
{
var reader = dr.DbDataReader;
while (reader.Read()) result = (int)reader[0];
}
return Convert.ToInt32(result);
}
Hovering over the word Database I could see it was in
Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade
I don't see a specific reference to
Microsoft.EntityFrameworkCore.Infrastructure
It is documented as being part of Entity Framework Core 2.1
inside either SDK so I wonder how it is referenced.
However I needed to add a reference to a Framework 4.7.2 dll
So I switched to the following project file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="2.2.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.2.6" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\SBD.Common.csproj" />
</ItemGroup>
</Project>
Now I get an error
CS1061 'DatabaseFacade' does not contain a definition for
'ExecuteSqlQuery'
I tried looking form Microsoft.EntityFrameworkCore.Infrastructure in Nuget Manage Packages for Solution but it does not show.
Looking at this question I decided to try using .FromSQl instead.
I found help in the docs but it does not explain how return non entity types.
How do I do that?
After reading the end of this link I am trying
public static int TransitTime(string postcode, ApiDbContext con)
{
var query = "SELECT top 1 Mins from Transit where postcode = #Postcode order by mins desc;";
var p1 = new SqlParameter("#Postcode", postcode);
var result = 0;
using (var command = con.Database.GetDbConnection().CreateCommand())
{
command.CommandText = query;
command.Parameters.Add(p1);
con.Database.OpenConnection();
using (var reader = command.ExecuteReader())
{
while (reader.Read()) result = (int)reader[0];
}
}
return Convert.ToInt32(result);
}
I see that this is using an extension method in Microsoft.EntityFrameworkCore.Infrastructure
I have been trying to write an MsBuild task to automatically get Nuget packages from a feed url and automatically update the packages.config to update to the latest version.
// ---- Download and install a package at a desired path ----
var sourceUri = new Uri("FEED URL");
// ---- Update the ‘packages.config’ file ----
var packageReferenceFile = new PackageReferenceFile("../../packages.config");
string packagesPath = "../../packages";
IPackageRepository sourceRepository = PackageRepositoryFactory.Default.CreateRepository(sourceUri.ToString());
PackageManager packageManager = new PackageManager(sourceRepository, packagesPath);
foreach (var sourcePackage in sourceRepository.GetPackages().Where(x => x.IsLatestVersion))
{
if (!packageReferenceFile.EntryExists(sourcePackage.Id + " " + sourcePackage.Version, sourcePackage.Version))
{
var oldPackage = packageReferenceFile.GetPackageReferences().FirstOrDefault(x => x.Id.Contains(sourcePackage.Id));
if (oldPackage != null)
{
packageReferenceFile.DeleteEntry(oldPackage.Id, oldPackage.Version);
}
packageManager.InstallPackage(sourcePackage.Id, SemanticVersion.Parse(sourcePackage.Version.ToFullString()));
// Get the target framework of the current project to add --> targetframework="net452" attribute in the package.config file
var currentTargetFw = Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(TargetFrameworkAttribute), false);
var targetFrameworkAttribute = ((TargetFrameworkAttribute[]) currentTargetFw).FirstOrDefault();
// Update the packages.config file
packageReferenceFile.AddEntry(sourcePackage.GetFullName(),
SemanticVersion.Parse(sourcePackage.Version.ToFullString()), false,
new FrameworkName(targetFrameworkAttribute.FrameworkName));
}
}
This is working fine as a console app and is automatically reading the file correctly and updating the necessary references.
When i try to run this as an MsBuild task I keep running into errors.
An error has occurred during compilation. c:\Users\user\AppData\Local\Temp\dkkg20ya.0.cs(22,11) : error CS0246: The type or namespace name 'NuGet' could not be found (are you missing a using directive or an assembly reference?)
The task factory "CodeTaskFactory" could not be loaded from the assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Build.Tasks.v15.0.dll". The task factory must return a value for the "TaskType" property.
This is the code I have put in the csproj (also moved to the nuget.targets to test)
<Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
<UpdateNugetFiles />
</Target>
<UsingTask TaskName="UpdateNugetFiles" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v$(MSBuildToolsVersion).dll" >
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.Linq" />
<Using Namespace="System.Reflection" />
<Using Namespace="System.Runtime.Versioning" />
<Using Namespace="NuGet" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try {
// ---- Download and install a package at a desired path ----
var sourceUri = new Uri("FEED URL");
// ---- Update the ‘packages.config’ file ----
var packageReferenceFile = new PackageReferenceFile("../../packages.config");
string packagesPath = "../../packages";
IPackageRepository sourceRepository = PackageRepositoryFactory.Default.CreateRepository(sourceUri.ToString());
PackageManager packageManager = new PackageManager(sourceRepository, packagesPath);
foreach (var sourcePackage in sourceRepository.GetPackages().Where(x => x.IsLatestVersion))
{
if (!packageReferenceFile.EntryExists(sourcePackage.Id + " " + sourcePackage.Version, sourcePackage.Version))
{
var oldPackage = packageReferenceFile.GetPackageReferences().FirstOrDefault(x => x.Id.Contains(sourcePackage.Id));
if (oldPackage != null)
{
packageReferenceFile.DeleteEntry(oldPackage.Id, oldPackage.Version);
}
packageManager.InstallPackage(sourcePackage.Id, SemanticVersion.Parse(sourcePackage.Version.ToFullString()));
// Get the target framework of the current project to add targetframework="net452" attribute in the package.config file
currentTargetFw = Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(TargetFrameworkAttribute), false);
var targetFrameworkAttribute = ((TargetFrameworkAttribute[]) currentTargetFw).FirstOrDefault();
// Update the packages.config file
packageReferenceFile.AddEntry(sourcePackage.GetFullName(),
SemanticVersion.Parse(sourcePackage.Version.ToFullString()), false,
new FrameworkName(targetFrameworkAttribute.FrameworkName));
}
}
return true;
}
catch (Exception ex) {
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>
Any ideas on how to resolve this as cannot seem to find a solution.
Overall what to run this a pre step on a CI build to keep nugets up to date.
Thanks
Tim
Just call
nuget restore "your_solution.sln"
Don't reinvent the wheel by writing it in C# code.
Nuget Update Packages.config in MSBuild BeforeBuild Step
Not sure where your code issue comes from. It may be simpler just use NuGet.exe to restore and update the solution instead of trying to use C# code.
So you could add following nuget command line in the MSBuild BeforeBuild Step
<Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
<Exec Command="$(YourNuGetPath)\nuget.exe restore "$(YouSolutionPath)\YourSolution.sln" -PackagesDirectory "$(YouPackagePath)\packages"" />
<Exec Command="$(YourNuGetPath)\nuget.exe update "$(YouSolutionPath)\YourSolution.sln"" />
</Target>
Note: If you are using Visual Studio, Visual Studio will automatically check the missing packages during the build and restore them: Package Restore.
Hope this helps.
I have the following C# project targetting .NET 4.0 that takes a source code file, compiles it into an assembly on the fly and then executes a static method of a type contained in that assembly.
This works as expected, as long as I don't start the program with a debugger attached. In that case I get an exception on the call to xmlSerializer.Serialize(sw, family);, more precisely a System.NullReferenceException inside a System.TypeInitializationException inside a System.InvalidOperationException.
If I take the same program, include the source code file in the project and compile it directly into the main program assembly, I will not get an exception regardless of whether or not a debugger is attached.
Please note that I my project references the exact same assemblies as those listed when compiling on the fly.
Why does it matter to the code compiled on the fly whether or not a debugger is attached? What am I missing?
Main file Program.cs:
using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.Linq;
namespace DebugSerializeCompiler
{
class Program
{
static void Main()
{
if (!Environment.GetCommandLineArgs().Contains("Compile"))
{
DebugSerializeCompiler.SerializerTest.Run();
}
else
{
Assembly assembly;
if (TryCompile("..\\..\\SerializerTest.cs", new[]{ "Microsoft.CSharp.dll",
"System.dll", "System.Core.dll", "System.Data.dll", "System.Xml.dll" },
out assembly))
{
Type type = assembly.GetType("DebugSerializeCompiler.SerializerTest");
MethodInfo methodInfo = type.GetMethod("Run");
methodInfo.Invoke(null, null);
}
}
Console.ReadKey();
}
static bool TryCompile(string fileName, string[] referencedAssemblies,
out Assembly assembly)
{
bool result;
CodeDomProvider compiler = CodeDomProvider.CreateProvider("CSharp");
var compilerparams = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};
foreach (var referencedAssembly in referencedAssemblies)
{
compilerparams.ReferencedAssemblies.Add(referencedAssembly);
}
using (var reader = new StreamReader(fileName))
{
CompilerResults compilerResults =
compiler.CompileAssemblyFromSource(compilerparams, reader.ReadToEnd());
assembly = compilerResults.CompiledAssembly;
result = !compilerResults.Errors.HasErrors;
if (!result)
{
Console.Out.WriteLine("Compiler Errors:");
foreach (CompilerError error in compilerResults.Errors)
{
Console.Out.WriteLine("Position {0}.{1}: {2}",
error.Line, error.Column, error.ErrorText);
}
}
}
return result;
}
}
}
File compiled into separate assembly SerializerTest.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace DebugSerializeCompiler
{
public class SerializerTest
{
public static void Run()
{
Console.WriteLine("Executing Run()");
var family = new Family();
var xmlSerializer = new XmlSerializer(typeof(Family));
TextWriter sw = new StringWriter();
try
{
if (sw == null) Console.WriteLine("sw == null");
if (family == null) Console.WriteLine("family == null");
if (xmlSerializer == null) Console.WriteLine("xmlSerializer == null");
xmlSerializer.Serialize(sw, family);
}
catch (Exception e)
{
Console.WriteLine("Exception caught:");
Console.WriteLine(e);
}
Console.WriteLine(sw);
}
}
[Serializable]
public class Family
{
public string LastName { get; set; }
public List<FamilyMember> FamilyMembers { get; set; }
}
[Serializable]
public class FamilyMember
{
public string FirstName { get; set; }
}
}
This is the csproj file used to compile the project using Visual C# 2010 Express on Windows 7:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{7B8D2187-4C58-4310-AC69-9F87107C25AA}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>DebugSerializeCompiler</RootNamespace>
<AssemblyName>DebugSerializeCompiler</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SerializerTest.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
It worked fine for me.
But if I had to guess what's going on for you it would be that because you're compiling the class in with your main project and dynamically compiling it the serializer is getting confused about which assembly to use and is failing. You could try attaching an event to AppDomain.CurrentDomain.AssemblyResolve and see if there are any assemblies failing to resolve there.