0

我正在尝试运行托管在控制台应用程序中的简单 WCF 服务。这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        var serviceHost = new ServiceHost(
          typeof (MyService), new Uri("http://localhost:3000"));
        serviceHost.AddServiceEndpoint(
          typeof (IMyService), new WebHttpBinding(), "");

        serviceHost.Open();
        Console.ReadKey();
        serviceHost.Close();
    }
}

[ServiceContract]
public interface IMyService
{
    [WebGet]
    [OperationContract]
    string Hello(String s);
}

public class MyService : IMyService
{
    public string Hello(String s)
    {
        return "hello " + s;
    }
}

当我去的时候http://localhost:3000,它说

服务

这是 Windows© Communication Foundation 服务。

此服务的元数据发布当前已禁用。

如果您有权访问该服务,则可以通过完成以下步骤来修改您的 Web 或应用程序配置文件来启用元数据发布:

[更多文字在这里]

然后,当我去 时http://localhost:3000/Hello?s=John,它说:

由于 EndpointDispatcher 的 AddressFilter 不匹配,接收方无法处理带有 To 'http://localhost:3000/Hello?s=John' 的消息。检查发送方和接收方的 EndpointAddresses 是否一致。

我想知道我是否做错了。目标平台是 .NET 4。我没有app.config.

将不胜感激任何建议。

4

2 回答 2

1

在 .NET 4 或更高版本中,如果您定义基地址,WCF 将为您生成默认终结点。换句话说,只需:

var serviceHost = new ServiceHost(
      typeof (MyService), new Uri("http://localhost:3000")); 
serviceHost.Open(); 

阅读开发人员对 Windows Communication Foundation 4 的介绍中的默认端点部分

于 2012-08-05T03:12:01.370 回答
0

像这样固定:

var serviceHost = new ServiceHost(
  typeof(MyService), new Uri("http://localhost:3000"));
var serviceContractDescription = ContractDescription.GetContract(
  typeof (IMyService));
var serviceEndpoint = new WebHttpEndpoint(
  serviceContractDescription, new EndpointAddress("http://localhost:3000"));
serviceHost.AddServiceEndpoint(serviceEndpoint);

现在工作。

于 2012-08-04T17:37:20.690 回答