0

I am trying to create a unit test that will compile an interface that I created from a service reference. The problem is that when I use ICodeCompiler to compile the interface, it throws error:

Error: The type or namespace name 'RoleAssignment' could not be found (are you missing a using directive or an assembly reference?)

The RoleAssignment class comes from the service reference. Is there a way I can add to the ICodeCompiler as a parameter the service reference? thank you

this is the method I have

 CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
        ICodeCompiler compiler = codeProvider.CreateCompiler();
        CompilerParameters parametersForInterface = new CompilerParameters();           
        parametersForInterface.GenerateInMemory = true;
        parametersForInterface.ReferencedAssemblies.Add("System.dll");
        parametersForInterface.ReferencedAssemblies.Add("System.Data.Services.Client.dll");
        parametersForInterface.ReferencedAssemblies.Add("System.ComponentModel.dll");
        parametersForInterface.OutputAssembly = "inMemoryAssembly.dll";
        StringBuilder interfaceSC = new StringBuilder();
        CompilerResults results = compiler.CompileAssemblyFromFile(parametersForInterface, path+ @"a\IA.cs");
        StringWriter sw = new StringWriter();

        // If there were errors, raise an exception...
        foreach (CompilerError ce in results.Errors)
        {
            if (ce.IsWarning) continue;
            sw.WriteLine("{0}({1},{2}: error {3}: {4}", ce.FileName, ce.Line, ce.Column, ce.ErrorNumber, ce.ErrorText);
        }
        // If there were errors, raise an exception...
        string errorText = sw.ToString();

errorText is the error mentioned above. I have a service reference added to my unit test, and when I add the IA.cs file to my project I get no errors. IA.cs is an interface that I generated from the service reference reference.cs. I don't know if there is a way to add the service reference to the ICodeCompiler Thank you

  • 2
    You need to create a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) that illustrates what you're trying to do. From that, people can tell you what you're getting wrong. – vendettamit Dec 22 '15 at 22:04
  • 1
    So you're trying to compile the service reference code via code which already references `RoleAssignment`, which is in the not-yet-compiled code? Perhaps try explaining your problem, not how you're trying to solve it... Why do you need to compile code with ICodeCompiler? – Dark Falcon Dec 22 '15 at 22:05
  • I just added more information about the problem I am having. I hope it is more clear now. – user2585242 Dec 22 '15 at 23:00

1 Answers1

0

A service reference is simply a generated .cs file. Add the service reference to a project in Visual Studio, then either reference that project's output in ReferencedAssemblies or locate the .cs file and pass its path to CompileAssemblyFromFile.

If you wish, you could also generate the .cs file manually: https://stackoverflow.com/a/12710348/2101267

Community
  • 1
  • 1
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98