WCF:使用 jQuery 和 Windows 应用程序使用 WCF 服务

发布于 2024-11-06 14:29:23 字数 2619 浏览 0 评论 0原文

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

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

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

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

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

这是我的 webconfig

<?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();
}

I've successfully consumed a WCF Service using jQuery by following this link: http://www.codeproject.com/KB/aspnet/WCF_JQUERY_ASMX.aspx

I just made a few modifications for my POCO entities to serialize properly. Everything works fine if consumed by jQuery or when viewed using the browser (changed verb to get).

Now I've created a windows application and added a service reference to this service. It successfully completes and I can see the classes/methods and all. However, when I try to run the application, I get the following error:

"Could not find default endpoint element that references contract [ContractName] in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."

Based on this error, I guess I should create another endpoint to cater to non-http applications? I'm not really sure how it works though..

Here's my webconfig

<?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="Data Source=bdowrmg01;Initial Catalog=ORMU_Prototype;user=sa;password=Password1;MultipleActiveResultSets=True"" 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>

Also, here's the contract:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蔚蓝源自深海 2024-11-13 14:29:23

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

因此,您需要使用 HttpWebRequest 类来使用您的服务,或者向 wsHttpBinding 类型的服务添加另一个绑定。

Service references only work with SOAP web-services (via a WSDL definition), not with Web HTTP (aka REST) services which is what you've got.

So you either need to use the HttpWebRequest class to consume your service, or add another binding to your service of type wsHttpBinding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文