无法从javascript访问应用程序中的WCF服务? [英] WCF service hosed in application can't be accessed from javascript?
问题描述
我有一个启动WCF服务的控制台应用程序,我想使用javascript在html文件中访问它。
I have an console application which starts a WCF service, and I want to access it in an html file using javascript.
不想使用web。配置,因为它看起来太复杂了。我想稍后在一个应用程序的插件中托管该服务。 (但如果web.config符合我的要求,也可以使用它。)
Don't want to use web.config because it seems too complicated. and I want to host the service in an addon of an application later. (but if web.config meets my requirement, it is ok to use it too).
以下是服务代码:
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是这样的:
And I want to access the service using javascript from a single html file, e.g. index.html like this:
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方法不允许
"POST" method got "404 Not Found" and "GET" method got "405 Method Not Allowed"
有任何建议吗?
非常感谢~~
推荐答案
这里是修改后的程序您的参考。
here is the modified program for your reference.
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端点。
The only actual difference is to add WebHttpBehavior into the web endpoint.
运行此程序,打开浏览器测试地址localhost:8080 / wb / sayhello?name = abc
如果浏览器返回abc,表示Web端点正常工作。
如果通过jQuery调用这个地址仍然无法正常工作,那么就在jQuery端进行故障排除。
running this program, and open brower to test address localhost:8080/wb/sayhello?name=abc if the brower returns "abc", that means the web end point is working. if calling this address by jQuery is still not working, then trouble-shooting on jQuery side.
这篇关于无法从javascript访问应用程序中的WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!