【发布时间】: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 错误)。