错误:WCF测试客户端不支持,因为它使用类型System.Threading.Tasks

Ler*_*ica 10 c# wcf

我会发布这个问题,即使我发现很少有其他类似的问题.但是,我无法找到令人满意的解决方案,无论是为什么我得到这个错误,也不知道如何解决它.

因此,今天我必须在我的本地计算机上托管服务以进行测试.该服务是一个单一的WCF服务解决方案,据我所知,它已经工作了很长时间.但是,当我下载项目并尝试在我的本地计算机上托管服务时,我从标题中得到错误:

WCF测试客户端不支持此操作,因为它使用类型System.Threading

所以当我回到家时,我决定使用一些异步方法进行服务,并深入了解这一点.然而,当我在几乎没有使用(或者至少看起来如此)System.Threading.Tasks任何地方的空项目上得到这个完全相同的错误时,我真的很惊讶.

所以我做了什么:

  • WCF Service使用Visual Studio 2013默认模板创建新的
  • 离开默认IService1.cs文件和Service1.svc
  • 改为IService1.cs:

    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);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
    • 改为Service1.svc:

      使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Runtime.Serialization; 使用System.ServiceModel; 使用System.ServiceModel.Web; 使用System.Text;

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

并保留默认情况web.config,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

我甚至没有尝试使用Task,Threading或类似的东西,只是想看到我的服务启动并运行所以我可以开始添加东西,看看我什么时候会得到错误但是在设置Service1.svc为我的启动后我感到惊讶类和尝试运行项目我得到了同样的错误:

WCF测试客户端不支持此操作,因为它使用类型System.Threading

好的,现在我完全迷失了.几次尝试运行我的项目后,我收到此错误.在发布此问题之前,我再次尝试,这次我没有收到错误.事实上,我刚刚完成了我的客户端,我可以使用该GetEmpId()方法.

那么这里发生了什么.这是我构建项目时的屏幕截图:

服务

我没有方法GetEmpIdAsync().我没有尝试添加它.它是怎么来的它不会多次构建,现在突然间我能够使用我从一开始实际实现的方法?

Sco*_*ain 11

WCF将自动为您的每个方法公开同步和异步接口.这两种方法都GetEmpId在服务器端同步调用,区别在于等待结果在客户端同步或异步.

如果你上课了

public class Service1 : IService1
{
    public Task<int> GetEmpIdAsync(int id)
    {
        return Task.FromResult<int>(id);
    }
}
Run Code Online (Sandbox Code Playgroud)

你仍会int GetEmpId(int id)在测试工具上看到.WCF足够聪明,可以将两个版本映射到同一个函数.

您所看到的"错误"只是visual studio附带的测试工具的限制,两个函数都调用相同的服务器端功能,因此没有强制力使Microsoft添加支持.没有实际错误.如果您真的想测试异步版本,则需要编写自己的调用该函数的测试客户端.

  • 那个测试工具,我不这么认为。但是,如果您使用 [SvcUtil](http://msdn.microsoft.com/en-us/library/aa347733(v=vs.110).aspx) 来生成您的界面,您只需要关闭 `/async`或者,如果您正在使用“添加服务引用”GUI,只需单击“高级”,然后取消选中“[允许生成异步操作](http://i.stack.imgur.com/GNsK0.png)”框 (2认同)