-1

当我为 .NET3.5 而不是在 4.0 中编译它时,这个简单的 WebserviceHost 可以工作我使用 URL http://mycomputer:8081/SVC/HelloWorld 从我的浏览器访问它

3.5 版本返回“Hello World!@<time>”字符串,但 4.0 版本返回 - 405 Not Allowed。

有谁知道为什么?

我在 Win7 SP1 64 位机器上使用 .net4.0

using System;
using System.Web.Services;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Collections.Generic;
using System.Runtime.Serialization;

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    [WebGet(UriTemplate = "HelloWorld")]
    string HellWorld();
}

public class MyService : IMyService
{
    public string HellWorld()
    {
        return "Hello World! @ " + DateTime.Now.ToString("s");
    }
}

public class MyClass
{

    public static void Main()
    {
        string myComputer = "myComputer";
        string mySvcUri = "http://" + myComputer + ":8081/SVC";
        Uri[] baseAddresses = new Uri[] { new Uri(mySvcUri) };

        WebServiceHost hostVisits = new WebServiceHost(typeof(MyService), baseAddresses);
        BasicHttpBinding binding = new BasicHttpBinding();
        hostVisits.AddServiceEndpoint(typeof(IMyService), binding, "MyService");

        hostVisits.Open();

        Console.WriteLine("Service host started, press any key to exit");
        Console.ReadKey();
    }
}

没有任何内容写入事件日志。

4

1 回答 1

1

只需删除这两行。它会起作用的。

BasicHttpBinding binding = new BasicHttpBinding();
hostVisits.AddServiceEndpoint(typeof(IMyService), binding, "MyService");
于 2013-04-25T14:28:05.270 回答