Search code examples
.netwcf

How to return message in the WCF endpoint?


I have an endpoint:

[ServiceContract]
public interface ICheck
{
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "check")]
        Task GetCheckAsync();
 }

I don't know how to return a string in the response to this endpoint. I try to return Task object but I can't instaniate it.

Question: How to return an object containing message to the requester(frontend)?


Solution

  • Try like this

    1) If you want to return an object

     [ServiceContract]
     public interface ICheck
     {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "check")]
        Task<objectname> GetCheckAsync();
     }
    

    and while defining

    public class HelloService : ICheck
    {
        public async Task<objectname> GetCheckAsync()
        {
           // do your operation and return the object
        }
    }
    

    2) If You Want to return a string

     [ServiceContract]
     public interface ICheck
     {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "check")]
        Task<string> GetCheckAsync();
     }
    

    and while defining

    public class HelloService : ICheck
    {
        public async Task<string> GetCheckAsync()
        {
           // do your operation and return the string
        }
    }
    

    For more clarification you can check the following Link for example

    Example