我自己写了个程序test,不过没成功,返回null.
Console application 用来一运行,就host 一个service 在 http://127.0.0.1/hellousing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Activation;namespace SelfHost
{
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
} [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class HelloWorldService : IHelloWorldService
{
public string SayHello(string name)
{
return string.Format("Hello, {0}", name);
}
} class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://127.0.0.1/hello"); // Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb); // Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
host.Open(); Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine(); // Close the ServiceHost.
host.Close();
}
}
}
}Jquery 调用self hosted service.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebAutomation.aspx.cs" Inherits="WebApplication2.WebAutomation" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript">
function invokeService() {
$(document).ready(function () {
var user = "[email protected]"; //Calling WCF Service using jQuery ajax method
$.ajax({
type: "GET",
async: "false",
url: "http://127.0.0.1/hello",
data: user,
contentType: "application/json",
dataType: "jsonp",
processData: true,
method: "SayHello",
success: function (result) {
AjaxSucceeded(result);
},
error: AjaxFailed
}); //Additional way of calling WCF service using getJSON() JQuery method
$.getJSON("http://127.0.0.1/hello/SayHello?name=user", {},
function (data) {
alert(data);
});
});
}
function AjaxSucceeded(result) {
alert(result);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="javascript: invokeService();" value="Call WCF Service" />
</div>
</form>
</body>
</html>
Console application 用来一运行,就host 一个service 在 http://127.0.0.1/hellousing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Activation;namespace SelfHost
{
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
} [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class HelloWorldService : IHelloWorldService
{
public string SayHello(string name)
{
return string.Format("Hello, {0}", name);
}
} class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://127.0.0.1/hello"); // Create the ServiceHost.
using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
{
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb); // Open the ServiceHost to start listening for messages. Since
// no endpoints are explicitly configured, the runtime will create
// one endpoint per base address for each service contract implemented
// by the service.
host.Open(); Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine(); // Close the ServiceHost.
host.Close();
}
}
}
}Jquery 调用self hosted service.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebAutomation.aspx.cs" Inherits="WebApplication2.WebAutomation" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript">
function invokeService() {
$(document).ready(function () {
var user = "[email protected]"; //Calling WCF Service using jQuery ajax method
$.ajax({
type: "GET",
async: "false",
url: "http://127.0.0.1/hello",
data: user,
contentType: "application/json",
dataType: "jsonp",
processData: true,
method: "SayHello",
success: function (result) {
AjaxSucceeded(result);
},
error: AjaxFailed
}); //Additional way of calling WCF service using getJSON() JQuery method
$.getJSON("http://127.0.0.1/hello/SayHello?name=user", {},
function (data) {
alert(data);
});
});
}
function AjaxSucceeded(result) {
alert(result);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="javascript: invokeService();" value="Call WCF Service" />
</div>
</form>
</body>
</html>
你打开谷歌浏览器或者firefox+firebug,看看network那里请求返回的是什么。
另外通过url调用需要使用webhttp的binding。
不指定这个binding是不可以的。你试试看。
如果把127.0.0.1/hello address 加到 service reference, web.config中会有binding 配置,可是jquery如何用呢?
如果把127.0.0.1/hello address 加到 service reference, web.config中会有binding 配置,可是jquery如何用呢?http://www.cnblogs.com/artech/archive/2012/02/15/wcf-rest.html
WCF很多经典系列