问题描述
我会发布这个问题,即使我看到有几个类似的问题.但是,我无法找到令人满意的解决方案,说明为什么会出现此错误或如何解决.
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.
因此,今天我不得不在本地计算机上托管一项服务以进行测试.该服务是一个单一的 WCF 服务解决方案,据我所知它已经工作了很长时间.但是,当我下载该项目并尝试在本地计算机上托管该服务时,我收到了标题中的错误:
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:
WCF 测试客户端不支持此操作,因为它使用 System.Threading 类型
所以当我回到家时,我决定使用一些异步方法制作一个服务并深入了解这个问题.然而,当我在几乎没有使用(或至少看起来如此)System.Threading.Tasks
任何地方的几乎空项目上遇到完全相同的错误时,我真的很惊讶.
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.
所以我做了什么:
- 使用 Visual Studio 2013 默认模板创建新的
WCF 服务
- 保留默认的
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);
}
}
将
Service1.svc
更改为:使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Runtime.Serialization;使用 System.ServiceModel;使用 System.ServiceModel.Web;使用 System.Text;
using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.ServiceModel.Web;using System.Text;
命名空间 WcfService{公共类 Service1 : IService1{公共 int GetEmpId(int id){返回标识;}}}
namespace WcfService{ public class Service1 : IService1 { public int GetEmpId(int id) { return id; } }}
并保留默认的
web.config
如下所示: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>
我什至没有尝试使用
Task
、Threading
或类似的东西,只是想看看我的服务是否已启动并正在运行,以便我可以开始添加内容看看我什么时候会收到错误,但令我惊讶的是,在将Service1.svc
设置为我的启动类并尝试运行该项目后,我遇到了同样的错误: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 settingService1.svc
as my startup class and trying to run the project I got the same error :WCF 测试客户端不支持此操作,因为它使用 System.Threading 类型
好的,现在我完全迷失了.多次尝试运行我的项目后,我收到此错误.就在发布这个问题之前,我再次尝试,这次我没有收到错误消息.事实上,我刚刚完成了我的客户端,我可以使用
GetEmpId()
方法.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:
我没有方法
GetEmpIdAsync()
.我没有尝试添加它.为什么它不会多次构建,现在突然间我可以使用我从一开始就实际实现的方法?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?推荐答案
WCF 将自动为您的每个方法公开同步和异步接口.这两种方法在服务器端同步调用
GetEmpId
,区别在于在客户端等待结果是同步的还是异步的.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.如果你让你的班级成为
public class Service1 : IService1 { public Task<int> GetEmpIdAsync(int id) { return Task.FromResult<int>(id); } }
您仍会在测试工具上看到
int GetEmpId(int id)
.WCF 足够聪明,可以将两个版本映射到同一个函数.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.您看到的错误"只是 Visual Studio 附带的测试工具的限制,两个函数调用相同的服务器端函数,因此没有强制力让 Microsoft 添加支持.没有实际错误.如果您真的想测试异步版本,则需要编写自己的调用该函数的测试客户端.
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.
这篇关于错误:WCF 测试客户端不支持,因为它使用 System.Threading.Tasks 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!