【问题标题】:WCF: Consume a WCF service using jQuery and also a Windows ApplicationWCF:使用 jQuery 和 Windows 应用程序使用 WCF 服务
【发布时间】:2011-05-13 09:58:56
【问题描述】:

我已通过以下链接使用 jQuery 成功使用 WCF 服务:http://www.codeproject.com/KB/aspnet/WCF_JQUERY_ASMX.aspx

我刚刚对我的 POCO 实体进行了一些修改以正确序列化。如果使用 jQuery 或使用浏览器查看(更改为获取的动词),一切正常。

现在我创建了一个 Windows 应用程序并添加了对该服务的服务引用。它成功完成,我可以看到类/方法和所有内容。但是,当我尝试运行应用程序时,出现以下错误:

“在 ServiceModel 客户端配置部分中找不到引用合约 [ContractName] 的默认端点元素。这可能是因为找不到您的应用程序的配置文件,或者因为在客户端中找不到与此合约匹配的端点元素元素。”

基于这个错误,我想我应该创建另一个端点来迎合非 http 应用程序?不过我不太确定它是如何工作的..

这是我的网络配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="EntityDataModelContainer" connectionString="metadata=res://*/EntityDataModel.csdl|res://*/EntityDataModel.ssdl|res://*/EntityDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=bdowrmg01;Initial Catalog=ORMU_Prototype;user=sa;password=Password1;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ORMDefaultServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="ORMDefaultServiceBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ORMDefaultServiceBehavior"
                 name="ORM.Business.KCSA">
        <endpoint address="" binding="webHttpBinding"
           contract="ORM.Business.IKCSA"
           behaviorConfiguration="ORMDefaultServiceBehavior"/>
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
</configuration>

另外,这是合同:

[ServiceContract]
public interface IKCSA
{
    [OperationContract]
    [ApplyDataContractResolver]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped,Method="GET",ResponseFormat=WebMessageFormat.Json)]
    JsonResponse<IEnumerable<KCSATopic>> GetTopics();
}

【问题讨论】:

  • 仅托管一个 Web 服务就需要付出巨大的努力。您应该查看servicestack.net,您可以在其中构建整个应用程序,从而减少配置 WCF 应用程序所需的工作量。查看完整的 TODO 应用程序 servicestack.net/Backbone.Todos 和整个 .cs 源代码 goo.gl/zA6hD

标签: jquery wcf json poco


【解决方案1】:

服务引用仅适用于 SOAP Web 服务(通过 WSDL 定义),不适用于您所拥有的 Web HTTP(又名 REST)服务。

因此,您要么需要使用 HttpWebRequest 类来使用您的服务,要么将另一个绑定添加到您的 wsHttpBinding 类型的服务中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-09
    • 2013-09-30
    • 2012-07-25
    • 1970-01-01
    • 2013-11-25
    • 2011-10-10
    • 2011-10-05
    • 2016-04-20
    相关资源
    最近更新 更多