如何在ajax客户端使用作为windows服务运行的wcf服务

如何在ajax客户端使用作为windows服务运行的wcf服务,ajax,wcf,Ajax,Wcf,我已经创建了一个WCF服务,它托管在windows服务中。当我通过解决方案资源管理器中的右客户端菜单将该服务的web引用添加到asp.net web forms项目中时,我就能够访问该服务并添加对该服务的引用 现在我想通过AJAX客户端(即在ASP.NET项目中通过ScriptManager组件)访问该服务,并在计时器中调用该服务以获得连续的值流 我从来没有在AJAX或web上工作过那么多,我没有在网上找到一个合适的例子 我正在使用WSHttpBinding 我正在发布我的代码,这样你就可以知

我已经创建了一个WCF服务,它托管在windows服务中。当我通过解决方案资源管理器中的右客户端菜单将该服务的web引用添加到asp.net web forms项目中时,我就能够访问该服务并添加对该服务的引用

现在我想通过AJAX客户端(即在ASP.NET项目中通过ScriptManager组件)访问该服务,并在计时器中调用该服务以获得连续的值流

我从来没有在AJAX或web上工作过那么多,我没有在网上找到一个合适的例子

我正在使用WSHttpBinding


我正在发布我的代码,这样你就可以知道我哪里做错了

WCF服务库代码:

ITestService.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;

namespace TestServiceLibrary
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
    [ServiceContract(Namespace="TestServiceLibrary")]
    public interface ITestService
    {
        [OperationContract]
        [WebGet]
        double Add(double n1, double n2);

        // TODO: Add your service operations here
    }
}
TestService.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace TestServiceLibrary
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
    public class TestService : ITestService
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
    }
}
TestServiceHost.cs(控制台应用程序代码)

app.config的XML配置。。。wcf服务库和wcf服务主机中相同(本例中为控制台应用程序…)


Web客户端(asp.net客户端,default.aspx)代码


简单AJAX服务客户端页面
// 
简单AJAX服务客户端页面

第一个号码:

第二个号码:

结果:

在ajax中通过asp.net访问Web服务时,我遇到的错误是Microsoft JScript运行时错误:“TestServiceLibrary”未定义


请通读此代码并帮助我找到问题。谢谢大家的回复。

使用firebug之类的工具来确定请求发生了什么。默认情况下,WSHttpBinding是安全的。检查您的安全设置。首先尝试无安全性,以确保这不是安全问题。

您是否尝试从AJAX客户端连接到服务?如果是,您是否有任何错误

如果看不到代码,可能会有很多事情,正如钱德尔马尼所说

我还没有使用WCF实现AJAX,但是仔细阅读Preet推荐的文章,我建议检查(如果您还没有)您的AJAX客户机是否具有本文所述的必要代码

您的服务操作是否使用[WebGet]装饰


您是否正确设置了AJAX客户端的配置文件?服务的配置文件设置正确吗?

看来问题出在我的服务宿主和我使用的端点上

我应该在控制台应用程序中修改我的服务宿主,以使用WebServiceHost而不是ServiceHost,这样只有ajax客户端才能与我的服务对话。我应该使用webHttpBinding而不是wsHttpBinding

因此,webHosting的代码如下所示

using (var host = new WebServiceHost(
  typeof(TestService)))
{
    // Start listening for messages
    host.Open();

    Console.WriteLine("Press any key to stop the service.");
    Console.ReadKey();

    // Close the service
    host.Close();
}
我的控制台的xml配置是

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <services>
      <service
          name="TestServiceLibrary.TestService"
          behaviorConfiguration="">
        <endpoint address="http://localhost:8732/TestService"
           binding="webHttpBinding"
           bindingConfiguration=""
           name="TestService_WebHttp"
           contract="TestServiceLibrary.ITestService" />
      </service>
    </services>
  </system.serviceModel>
</configuration>

现在,当我进行这些更改时,我可以通过ie调用我的服务,使用ie中的以下urlhttp://localhost:8732/TestService/Add?n1=20&n2=20它返回的结果如下
40

最后我找到了解决我问题的办法。Im使用JSON作为数据通信方式,接收数据的脚本如下:

<script type="text/javascript">
    $("#mybutton").click(function () {


        $.getJSON("http://localhost:8732/TestService/Add", null, function (result) {


        });

    });     
</script>

$(“#我的按钮”)。单击(函数(){
$.getJSON(“http://localhost:8732/TestService/Add,null,函数(结果){
});
});     

MSDN上有一个简单的示例可能会有所帮助:Preet@感谢您的链接,在这个示例中,我了解到项目是iis中的主机,但我的服务是windows服务中的主机。@coolcake-服务的主机位置应该无关紧要。只要您的客户端具有适当的配置,它应该能够连接到服务并执行操作。Tim@我能找到任何使用windows服务中托管的wcf服务库和用asp.net ajax编写的客户端的示例吗?Chandermani@不幸的是,我没有那么多的web调试技能,因为我现在手头有点脏。我会试试你的建议,那样会更好。这在很大程度上是一个猜谜游戏,现在的信息可用。一旦你提供更多关于错误的详细信息,人们可以更好地帮助你。Tim@谢谢你的帮助。我已经在下面的页面上发布了代码。请帮助我确定问题。如果我在浏览器中打开,为什么会得到:Endpoint not found。
using (var host = new WebServiceHost(
  typeof(TestService)))
{
    // Start listening for messages
    host.Open();

    Console.WriteLine("Press any key to stop the service.");
    Console.ReadKey();

    // Close the service
    host.Close();
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <services>
      <service
          name="TestServiceLibrary.TestService"
          behaviorConfiguration="">
        <endpoint address="http://localhost:8732/TestService"
           binding="webHttpBinding"
           bindingConfiguration=""
           name="TestService_WebHttp"
           contract="TestServiceLibrary.ITestService" />
      </service>
    </services>
  </system.serviceModel>
</configuration>
<script type="text/javascript">
    $("#mybutton").click(function () {


        $.getJSON("http://localhost:8732/TestService/Add", null, function (result) {


        });

    });     
</script>