Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : not supported in WCF Test client because it uses type System.Threading.Tasks

Tags:

c#

wcf

I will post this question even though I see that there are few others similar to this one. However I am not able to find satisfying solution on either why I get this error nor how to solve it.

So, today I had to host a service on my local computer for testing purposes. The service is a single WCF service solution and it's been working as far as I know for a long time. However when I downloaded the project and tried to host the service on my local machine I got the error from the title:

This operation is not supported in the WCF Test Client because it uses type System.Threading

So when I got back home I decided to make a service using some async methods and get to the bottom of this. However I was really surprised when I get this exact same error on almost empty project which is not using (or at least it seems so) the System.Threading.Tasks anywhere.

So what I did:

  • Created new WCF Service using Visual Studio 2013 default template
  • Left the default IService1.cs file and Service1.svc
  • Changed the IService1.cs to :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    
    namespace WcfService
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            int GetEmpId(int id);
        }
    }
    
    • Changed the Service1.svc to:

      using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text;

      namespace WcfService { public class Service1 : IService1 { public int GetEmpId(int id) { return id; } } }

And leaving the default web.config which looks like this:

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 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>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

I haven't even tried to use Task, Threading or something like that, just wanted to see that my service is up and running so I can start adding things and see when exactly I'll get the error but for my surprise after setting Service1.svc as my startup class and trying to run the project I got the same error :

This operation is not supported in the WCF Test Client because it uses type System.Threading

Ok, now I'm completely lost. I was getting this error after several attempts to run my project. Just before posting this question I tried again and this time I didn't get the error. In fact I just finished my client and I'm able to consume the GetEmpId() method.

So what is going on here. This is screenshot when I build my project:

Service

I don't have method GetEmpIdAsync(). I haven't tried to add it. And how it comes it won't build several times and now all of a sudden I'm able to use the method that I actually have implemented from the very beginning?

like image 864
Leron_says_get_back_Monica Avatar asked Oct 28 '14 18:10

Leron_says_get_back_Monica


1 Answers

WCF will automatically expose both a synchronous and a asynchronous interface for each of your methods. Both of those methods call GetEmpId synchronously on the server side, the difference is will waiting for the result be synchronous or asynchronous on the client side.

If you made your class to be

public class Service1 : IService1
{
    public Task<int> GetEmpIdAsync(int id)
    {
        return Task.FromResult<int>(id);
    }
}

you would still see int GetEmpId(int id) on the test tool. WCF is smart enough to map the two versions to the same function.

The "error" you are seeing is just a limitation of the test tool that comes with visual studio, both functions call the same server side function so there is no compelling force to make Microsoft add support. There is no actual error. If you really want to test the async version you will need to write your own test client that calls the function.

like image 193
Scott Chamberlain Avatar answered Oct 24 '22 05:10

Scott Chamberlain