如何从asp.net客户端调用自宿主wcf服务
本文关键字:宿主 wcf 服务 调用 客户端 asp net | 更新日期: 2023-09-27 18:18:35
我有一个控制台应用程序,自我托管一个WCF服务。当用户访问asp.net应用程序并单击页面上的按钮时,如何编写一些脚本来调用这个自托管的WCF服务(托管在本地的服务)。我猜我的脚本有问题,请帮忙。
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();
}
}
}
调用服务脚本
<script type="text/javascript">
function invokeService() {
$(document).ready(function () {
var userName = " test";
$.ajax({
type: "POST",
async: "false",
url: "http://127.0.0.1:8080/hello",
data: "{'name':'" + userName + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
method: "SayHello",
success: function (result) {
AjaxSucceeded(result);
},
error: function (retult) {
AjaxFailed(result);
}
});
});
}
不希望以这种方式使用jquery调用WCF服务。
最大的问题是,您正在公开一个Soap服务,并试图使用一个rest-like客户机来调用它。从。net 3.5开始,WCF允许您将WCF服务操作公开给非soap端点。您必须以这种方式公开您的服务,以便能够使用Jquery使用它。
MSDN的起点在这里,几年前在MSDN杂志上有一个很好的介绍。
一个非常简单的例子是(没有任何配置)
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(BodyStyle=WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
string EchoWithGet(string s);
[OperationContract]
[WebInvoke(BodyStyle=WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
string EchoWithPost(string s);
}
public class Service : IService
{
public string EchoWithGet(string s)
{
return "You said " + s;
}
public string EchoWithPost(string s)
{
return "You said " + s;
}
}
class Program
{
static void Main(string[] args)
{
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8000/"));
ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.HttpHelpPageEnabled = false;
host.Open();
Console.WriteLine("Service is running");
Console.WriteLine("Press enter to quit...");
Console.ReadLine();
host.Close();
}
}
刚才发现脚本跨域调用是不允许的。另一种方法是使用命令行应用程序或控制台文件来调用wcf服务。然后在注册表中注册这个应用程序。