パーソナルツール
現在の場所: ホーム Memo 3 steps to add JsonP endpoint for WCF service

3 steps to add JsonP endpoint for WCF service

— カテゴリー: ,

  1. Download Webservices.Jsonp.dll and add it as a reference to the WCF service project.
  2. Add [WebGet] [JsonpBehavior] attributes to the WCF OperationContract functions.
        [ServiceContract]
        public interface ITestService
        {
            [OperationContract]
            string GetData(string input);
        }
        public class TestService : ITestService
        {
            [WebGet, JsonpBehavior]
            public string GetData(string input)
            {
                return string.Format("You entered: \"{0}\"", input);
            }
        }
  3. Add JsonP endpoint for the service.
      <system.serviceModel>
        <services>
          <service behaviorConfiguration="WebServices.TestService.Service1Behavior"
            name="WebServices.TestService.TestService">
            <endpoint address="" binding="wsHttpBinding" contract="WebServices.TestService.ITestService">
            </endpoint>
            <endpoint address="jsonp" behaviorConfiguration="WebHttpEndpointBehavior" binding="customBinding"
                      bindingConfiguration="jsonpBinding" contract="WebServices.TestService.ITestService"/>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
          </service>
        </services>
        <behaviors>
          <endpointBehaviors>
            <behavior name="WebHttpEndpointBehavior">
              <webHttp/>
              <enableWebScript/>
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="WebServices.TestService.Service1Behavior">
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <bindings>
          <customBinding>
            <binding name="jsonpBinding">
              <jsonpMessageEncoding/>
              <httpTransport manualAddressing="true"/>
            </binding>
          </customBinding>
        </bindings>
        <extensions>
          <bindingElementExtensions>
            <add name="jsonpMessageEncoding" type="WebServices.JsonpBindingExtension, WebServices.Jsonp"/>
          </bindingElementExtensions>
        </extensions>
      </system.serviceModel>

 Download source codes

Jia Kang

Jia Kang's photo