用于 AJAX 客户端的自托管 WCF
我正在尝试自行托管 WCF Web 服务并提供具有 ajax 支持的 HTTP 端点。我发现的有关 WCF 和 AJAX 的几乎所有内容都在谈论 IIS,而我不想使用 IIS。
我构建了一个简单的控制台应用程序来托管该服务。我的服务只有一个方法:
[ServiceContract]
interface IMyService
{
[OperationContract]
string TestConnection();
}
这是 app.config 代码:
<services>
<service name="Service.MyService" behaviorConfiguration="MyServiceBehavior" >
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="Service.IMyService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
我可以访问服务元数据端点并查看 WSDL,但无法从 ajax 客户端使用它。所以我的问题是: 1.可以这样做吗? 2. 我明显缺少的所需配置是什么?
注意 我没有使用 .svc 文件,
谢谢!
I'm trying to self-host a WCF web service and provide a HTTP endpoint with ajax support. Pretty much everything I've found about WCF and AJAX are talking about IIS, which I don't want to use.
I've build a simple Console App to host the service. My service only have a single method:
[ServiceContract]
interface IMyService
{
[OperationContract]
string TestConnection();
}
And here's the app.config code:
<services>
<service name="Service.MyService" behaviorConfiguration="MyServiceBehavior" >
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="Service.IMyService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior" >
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
I can access the service metadata endpoint and see the WSDL, but I'm unable to use it from my ajax client. So my question are :
1. Is it possible to do this?
2. What is the needed configuration that I'm obviously missing?
NOTE I'm not using a .svc file
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的客户是什么?通过enableWebScript,您将获得ASP.NET AJAX 支持(例如,修饰成员、类型以及所有其他暗示的内容)。如果您想要“原始”JSON,请使用 webHttp 行为而不是 enableWebScript,并使用 WebInvokeAttribute 或 WebGetAttribute 标记您的接口操作(根据需要将请求/响应类型设置为 JSON 或 XML)。看起来您还没有将您的接口归因于 ServiceContractAttribute,这是必需的。
What is your client? With enableWebScript, you're getting ASP.NET AJAX support (eg, decorated members, types, and all the othe goo that implies). If you want "raw" JSON, use the webHttp behavior instead of enableWebScript, and tag your interface operations with WebInvokeAttribute or WebGetAttribute (setting the request/response types to JSON or XML as you wish). It also looks like you haven't attributed your interface with ServiceContractAttribute, which is required.