开发者

Self-Hosting WCF for AJAX Client

开发者 https://www.devze.com 2022-12-12 04:31 出处:网络
I\'m trying to self-host a WCF web service and p开发者_如何学Crovide 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

I'm trying to self-host a WCF web service and p开发者_如何学Crovide 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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消