1

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)?

Yoda
  • 17,363
  • 67
  • 204
  • 344

2 Answers2

1

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

Vinoth
  • 851
  • 7
  • 23
0

It seems that you want to get the string/json result from the server-side in the frontend. You could use the webhttpbinding to publish the WCF service and invoke the service with ajax method.
Besides, we should ensure that the operation method return the right type you want. You could refer to the following link.
How can I use a WCF Service?
If you want to return a strong-type object, you could use DataContract.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/using-data-contracts
Feel free to let me know if there is anything I can help with.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22