【问题标题】:Weird error when running a console app with mono?使用单声道运行控制台应用程序时出现奇怪的错误?
【发布时间】:2014-11-16 03:46:22
【问题描述】:

我正在尝试使用单声道从我的 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

方法体为空是什么意思?

【问题讨论】:

  • 您的 Ubuntu 机器中的 System.ServiceModel.dll 库可能有问题。您是否尝试使用 .exe 文件上传所有必需的库。如果您使用MonoDevelop,这很容易,您只需选择Local copy 作为System.ServiceModel 引用,然后将System.ServiceModel.dll 库与您的.exe 文件一起使用。
  • 我使用 Visual Studio 2012 构建应用程序,并且我在 System.ServiceModel 参考上设置了“Copy Local = true”,我还尝试使用 xbuild ConsoleAppTest.csproj 在我的 Ubuntu 服务器上构建它,但是那也失败了。
  • 不要在本地复制 System.ServiceModel.dll,它已经在 GAC 中了。您使用的是哪个 Mono 版本(3.8 是最新的)? “无效的 IL 代码”是指编译器生成的代码无法执行(可能是 Mono 错误)。

标签: c# wcf mono


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-04-01
    • 1970-01-01
    • 2011-10-25
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多