none
creating a async WCF Rest service method. RRS feed

  • Question

  • HI, 

      I am creating a wcf rest service. I dont have any issues calling a synchronous method. But I am not sure how to call an async method. Kindly help.

     
    //works fine.
    [OperationContract]
            [WebGet(UriTemplate = "GetValue/{word}", 
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle =WebMessageBodyStyle.Bare)]
            public string GetValue(string word)
            {
               return "HI" + word;
            }
    
    //doesnt work
            [OperationContract]
            [WebGet(UriTemplate = "GetValueAsync/{word}",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Bare)]
            public async Task<string> GetValueAsync(string word)
            {
    //getting compilation error in the below line.
               return await Task.Factory.StartNew(() => { return "HI" + word; });
            }

    My config file.

    <system.serviceModel>
    	<services>
       		<service behaviorConfiguration="servicebehaviour" name="WCFLib.Calculator">
        
       		</service>
    			
      	</services>
    	<behaviors>
    		<serviceBehaviors>
    			<behavior name="servicebehaviour">
    				<serviceMetadata httpGetEnabled="true"/>
    				<serviceDebug includeExceptionDetailInFaults="false"/>
    			</behavior>
    		</serviceBehaviors>
          
    		<endpointBehaviors >
            		<behavior name="webhttp">
              			<webHttp/>
            		</behavior>
          		</endpointBehaviors>
    	</behaviors>
        
    </system.serviceModel>

    I have given very limited info here. let me know if you need further information. Kindly help me in creating an async method in WCF rest Service.

    Thanks,

    Sanjay

    Thursday, July 28, 2016 7:02 PM

Answers

  • Hi Sanjay,

    >> //getting compilation error in the below line.

    What is the complication error? It would be helpful if you could share us your detailed steps to create rest service. I follow below steps, it return the correct result when I call the url from Fiddler.

    1. Create a wcf service application

    2. Modify the service code

    //IService1.cs
        [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            [WebGet(UriTemplate="GetValue/{word}")]
            string GetData(string word);
            [OperationContract]
            [WebGet(UriTemplate = "GetValueAsync/{word}",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Bare)]
    
            Task<string> GetValueAsync(string word);
        }
    
    //Service1.cs    
    public class Service1 : IService1
        {
            public string GetData(string word)
            {
                return string.Format("You entered: {0}", word);
            }
            public async Task<string> GetValueAsync(string word)
            {
                return await Task.Factory.StartNew(() => { return "HI" + word; });   
    
            }        
        }
    

    3. Modify web.config

    <system.serviceModel>
    <services>
    <service name="WCFRestAysnc.Service1" behaviorConfiguration="service">
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="rest" contract="WCFRestAysnc.IService1"></endpoint>
    </service>
    </services>
    <behaviors>
    <serviceBehaviors>
    <behavior name="service">
    <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
    <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
    <behavior name="rest">
    <webHttp/>
    </behavior>
    </endpointBehaviors>
    </behaviors>
    <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    </system.serviceModel>

    4. Call the rest service from Fiddler by Get http://localhost:17824/Service1.svc/GetValueasync/test2 and get “JSON=HItest2”

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, July 29, 2016 9:56 AM

All replies

  • Hello Sanjay,

    Here are some resources that should help:

    https://msdn.microsoft.com/en-us/library/ms734701(v=vs.110).aspx

    http://stackoverflow.com/questions/18284998/pattern-for-calling-wcf-service-using-async-await

    http://stackoverflow.com/questions/20442649/simple-async-task-example-for-an-wcf-service-in-c-sharp

    Cheers


    Jeff

    Thursday, July 28, 2016 10:46 PM
  • Hi Sanjay,

    >> //getting compilation error in the below line.

    What is the complication error? It would be helpful if you could share us your detailed steps to create rest service. I follow below steps, it return the correct result when I call the url from Fiddler.

    1. Create a wcf service application

    2. Modify the service code

    //IService1.cs
        [ServiceContract]
        public interface IService1
        {
    
            [OperationContract]
            [WebGet(UriTemplate="GetValue/{word}")]
            string GetData(string word);
            [OperationContract]
            [WebGet(UriTemplate = "GetValueAsync/{word}",
                RequestFormat = WebMessageFormat.Json,
                ResponseFormat = WebMessageFormat.Json,
                BodyStyle = WebMessageBodyStyle.Bare)]
    
            Task<string> GetValueAsync(string word);
        }
    
    //Service1.cs    
    public class Service1 : IService1
        {
            public string GetData(string word)
            {
                return string.Format("You entered: {0}", word);
            }
            public async Task<string> GetValueAsync(string word)
            {
                return await Task.Factory.StartNew(() => { return "HI" + word; });   
    
            }        
        }
    

    3. Modify web.config

    <system.serviceModel>
    <services>
    <service name="WCFRestAysnc.Service1" behaviorConfiguration="service">
    <endpoint address="" binding="webHttpBinding" behaviorConfiguration="rest" contract="WCFRestAysnc.IService1"></endpoint>
    </service>
    </services>
    <behaviors>
    <serviceBehaviors>
    <behavior name="service">
    <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
    <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
    <behavior name="rest">
    <webHttp/>
    </behavior>
    </endpointBehaviors>
    </behaviors>
    <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    </system.serviceModel>

    4. Call the rest service from Fiddler by Get http://localhost:17824/Service1.svc/GetValueasync/test2 and get “JSON=HItest2”

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, July 29, 2016 9:56 AM
  • Can you please show me how you call the service method using C# code HttpWebRequest. I was excited about your reply until I saw call service using fiddler...
    Friday, February 15, 2019 10:29 PM