簡體   English   中英

使用Mono運行控制台應用程序時出現奇怪的錯誤?

[英]Weird error when running a console app with 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

方法主體為空意味着什么?

我們遇到了這個問題,然后發現對System.dll的引用將“本地復制”設置為True。 我們正在Windows上構建應用程序,然后在Mono下運行它,因此我們的應用程序隨Windows一起附帶了System.dll,但無法在Mono下加載。

解決方法是確保對System.dll的引用未打開“本地復制”,並且確保在Mono上運行的版本中不包含System.dll。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM