How to access wcf service with httpget?

Topics: Customizing Orchard, Writing modules
Mar 21, 2012 at 9:42 AM

Hi, i have wcf in a module:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class WcfService : IWcfService
    {
         public string  DoWork()
        {
            return "test from dowork";
        }
    }

[ServiceContract]
    public interface IWcfService : IDependency
    {
        [OperationContract(Name = "DoWork")]
        [WebInvoke(UriTemplate = "DoWork", Method = "GET")]
        string DoWork();
    }

  public IEnumerable<RouteDescriptor> GetRoutes() {

            ServiceRoute _route2 = new ServiceRoute("Services/WcfService",
                                              new OrchardServiceHostFactory(),                   
                                              typeof(IWcfService));

               return new[] {
                     new RouteDescriptor {   Priority = 20,
                                            Route = _route2

                    }
                };
        }

i can access http://localhost/orchardlocal/Services/WcfService

and can call the method "DoWork"  when i use .net application with adding service reference.

but how can access this mehtod use httpget? like this:

http://localhost/orchardlocal/Services/WcfService/DoWork

or with params like

http://localhost/orchardlocal/Services/WcfService/DoWork?args=1

thanks!

Mar 22, 2012 at 5:37 AM

Fixed it:

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CustomizationService:ICustomizationService
    {
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        public string DoWork(string workId)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "text/json";
            // Add your operation implementation here
            var obj = new { result = "ok", detail = "test from CustomizationService DoWork",id=workId };
            string rslt = jsSerializer.Serialize(obj);
            return rslt;
        }

        // Add more operations here and mark them with [OperationContract]
    }

 

and the webconfig is:

 

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="Orchard.Customization.WebServices.CustomizationServiceAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
    <services>
      <service name="Orchard.Customization.WebServices.ICustomizationService">
        <endpoint address="" behaviorConfiguration="Orchard.Customization.WebServices.CustomizationServiceAspNetAjaxBehavior"
          binding="webHttpBinding" contract="Orchard.Customization.WebServices.ICustomizationService" />
      </service>
      <service name="Orchard.Customization.WebServices.CustomizationService">
        <endpoint address="" behaviorConfiguration="Orchard.Customization.WebServices.CustomizationServiceAspNetAjaxBehavior"
          binding="webHttpBinding" contract="Orchard.Customization.WebServices.CustomizationService" />
      </service>
    </services>
  </system.serviceModel>