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

C# 使用mono运行控制台应用程序时出现奇怪的错误?,c#,wcf,mono,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

我正在尝试使用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);
        }
    }

}

方法体为空是什么意思?

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


修复方法是确保对System.dll的引用没有打开“复制本地”,并且System.dll没有包含在Mono上运行的构建中。

Ubuntu机器中的
System.ServiceModel.dll
库可能有问题。您是否尝试用.exe文件上载所有必需的库。如果您使用的是
MonoDevelop
这很简单,您必须为
System.ServiceModel
参考选择
Local copy
,然后将
System.ServiceModel.dll
库与.exe文件一起使用。我使用Visual Studio 2012构建应用程序,并且我已经设置了“copy Local=true”在
System.ServiceModel
参考中,我还尝试使用
xbuild ConsoleAppTest.csproj
使用我的Ubuntu服务器构建它,但也失败了。不要本地复制System.ServiceModel.dll,它已经在GAC中了。您使用的是哪种单声道版本(3.8是最新版本)?“无效IL代码”指编译器生成的无法执行的代码(可能是Mono bug)。
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