使用mono运行控制台应用程序时出现奇怪的错误?

时间:2014-09-22 08:28:18

标签: c# wcf mono

我正在尝试使用mono来从我的Ubuntu机器上的控制台应用程序托管WCF服务,我只是使用MDSN中的示例代码来创建示例服务,这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace ConsoleAppTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8080/hello");
            using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
            {
                // Enable metadata publishing.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);

                // Open the ServiceHost to start listening for messages. Since
                // no endpoints are explicitly configured, the runtime will create
                // one endpoint per base address for each service contract implemented
                // by the service.
                host.Open();

                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();

                // Close the ServiceHost.
                host.Close();
            }

        }
    }

    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello(string name);
    }

    public class HelloWorldService : IHelloWorldService
    {
        public string SayHello(string name)
        {
            return string.Format("Hello, {0}", name);
        }
    }

}

当我构建它并将其上传到我的Ubuntu机器并使用mono ConsoleAppTest.exe运行时,我收到以下错误:

Unhandled Exception:
System.InvalidProgramException: Invalid IL code in System.ServiceModel.ServiceHost:.ctor (System.Type,System.Uri[]): method body is empty.

  at ConsoleAppTest.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidProgramException: Invalid IL code in System.ServiceModel.ServiceHost:.ctor (System.Type,System.Uri[]): method body is empty.

  at ConsoleAppTest.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0

方法体是空的是什么意思?

1 个答案:

答案 0 :(得分:1)

我们遇到了这个问题,然后发现我们对System.dll的引用将Copy Local设置为True。我们在Windows上构建我们的应用程序,然后在Mono下运行它,因此我们的应用程序与Windows中的System.dll一起提供,它无法在Mono下加载。

修复是为了确保对System.dll的引用没有打开Copy Local,并且在Mono上运行的构建中没有包含System.dll。