【问题标题】:WCF service hosed in application can't be accessed from javascript?无法从 javascript 访问应用程序中的 WCF 服务?
【发布时间】:2014-04-10 17:04:47
【问题描述】:

我有一个启动 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);
     }
  }

我想使用 javascript 从单个 html 文件访问服务,例如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);}});

但它们不起作用。

“POST”方法得到“404 Not Found” 和 “GET”方法得到“405 Method Not Allowed”

有什么建议吗?

非常感谢~~

【问题讨论】:

  • 您能出示一下 web.config 文件吗?必须了解您的行为和端点配置,以便为您提供有用的答案。
  • jQuery.get("localhost:8080/SayHello?name=kii", function(ret){alert(ret);}});
  • @PatchesTheClown,我没有 web.config 文件,因为我要将服务嵌入到应用程序插件中,不知道如果使用 web.config 是否仍然有效。
  • @yyou,不,这不起作用,仍然得到“405 Method Not Allowed”

标签: javascript wcf


【解决方案1】:

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

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 端排错。

【讨论】:

  • 太棒了!!!!非常感谢!!!我注意到我需要为这家伙 添加相应的代码,但是找不到入口点,因为它在 behavior>endpointBehaviors
  • 我又回来了.. @yyou,我发现我们可以使用浏览器获取响应文本,但没有像我上面显示的示例那样来自 ajax 的响应。任何想法?谢谢。
  • 我认为这是一个跨域问题,因为在“localhost:xxx”网页中,我可以得到正确的结果。但在磁盘中的单个 html 文件中,我不能
  • 最后,我使用 JSONP 方法 jQuery.ajax({ url: "http://localhost:8083/wb/sayhello?name=sdfsfd", jsonp: "callback", dataType: "jsonp", success: function( response ) { console.log( response ); } }); 让它工作,在此之前,我需要将 CrossDomainScriptAccessEnabled 设置为 true:new WebHttpBinding(WebHttpSecurityMode.None) { CrossDomainScriptAccessEnabled = true }
猜你喜欢
  • 2015-12-18
  • 1970-01-01
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
相关资源
最近更新 更多