无法从javascript访问应用程序中的WCF服务?

时间:2014-04-10 17:04:48

标签: javascript wcf

我有一个启动WCF服务的控制台应用程序,我想使用javascript在html文件中访问它。

不想使用web.config,因为它看起来太复杂了。我想稍后在一个应用程序的插件中托管该服务。 (但如果web.config符合我的要求,也可以使用它。)

以下是服务代码:

  class Program
  {
     static void Main(string[] args)
     {
        Uri baseAddress = new Uri("http://localhost:8080");
        using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
        {
           host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
           host.AddServiceEndpoint(typeof(IHelloWorldService), new BasicHttpBinding(), "bh");
           host.AddServiceEndpoint(typeof(IHelloWorldService), new WebHttpBinding(WebHttpSecurityMode.None), "wb");
           host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
           host.Open();
           Console.WriteLine("The service is ready at {0}", baseAddress);
           Console.WriteLine("Press <Enter> to stop the service.");
           Console.ReadLine();
           host.Close();
        }
     }
  }

  [ServiceContract]
  public interface IHelloWorldService
  {
     [OperationContract]
     [WebGet(UriTemplate = "/SayHello?name={name}", ResponseFormat = WebMessageFormat.Json)]
     string SayHello(string name);
  }

  public class HelloWorldService : IHelloWorldService
  {
     public string SayHello(string name)
     {
        Console.WriteLine("called SayHello");
        return string.Format("Hello, {0}", name);
     }
  }

我想使用单个html文件中的javascript访问该服务,例如index.html像这样:

jQuery.post("http://localhost:8080/HelloWorldService.svc/wb/SayHello", {name:"kii"}, function(ret){alert(ret);}});

或者像这样:

jQuery.get("http://localhost:8080/HelloWorldService.svc/wb/SayHello?name=kii",  function(ret){alert(ret);}});

但他们没有工作。

&#34; POST&#34;得到的方法&#34; 404 Not Found&#34; 和 &#34; GET&#34;方法得到&#34; 405方法不允许&#34;

有什么建议吗?

非常感谢~~

1 个答案:

答案 0 :(得分:0)

这是修改后的程序供您参考。

class Program {
    static void Main(string[] args) {
        Uri baseAddress = new Uri("http://localhost:8080");
        using (ServiceHost host = new ServiceHost(
            typeof(HelloWorldService), baseAddress)) {

            host.Description.Behaviors.Add(
                new ServiceMetadataBehavior { HttpGetEnabled = true });
            host.AddServiceEndpoint(
                typeof(IHelloWorldService), new BasicHttpBinding(), "bh");

            var webEndPoint = host.AddServiceEndpoint(
                typeof(IHelloWorldService), 
                new WebHttpBinding(WebHttpSecurityMode.None), "wb");
            webEndPoint.Behaviors.Add(new WebHttpBehavior());

            host.AddServiceEndpoint(
                ServiceMetadataBehavior.MexContractName, 
                MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
            host.Open();

            var n = 0;
            foreach (var endPoint in host.Description.Endpoints) {
                Console.WriteLine("endpoint " + n);
                Console.WriteLine(" address: " + endPoint.Address);
                Console.WriteLine(" absolute path: " + endPoint.ListenUri.AbsolutePath);
                Console.WriteLine(" absolute uri: " + endPoint.ListenUri.AbsoluteUri);
                n++;
            }
            Console.WriteLine();
            Console.WriteLine("The service is ready at {0}", baseAddress);
            Console.WriteLine("Press <Enter> to stop the service.");

            Console.ReadLine();             
        }
    }
}

唯一的实际区别是将WebHttpBehavior添加到Web端点中。

运行此程序,并打开浏览器测试地址localhost:8080 / wb / sayhello?name = abc 如果浏览器返回“abc”,则表示Web端点正在运行。 如果通过jQuery调用这个地址仍然不起作用,那么就在jQuery方面进行故障排除。