WCF:使用 jQuery 和 Windows 应用程序使用 WCF 服务
我已通过以下链接成功使用 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="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>
另外,这是合同:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
服务引用仅适用于 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 typewsHttpBinding
.