问题
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!
回答1:
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.
来源:https://stackoverflow.com/questions/1792208/self-hosting-wcf-for-ajax-client