创建WCF服务时出错:wcf测试客户端不支持此操作,因为它使用类型System.Threading

时间:2014-09-22 18:25:31

标签: .net wcf runtime-error

我对WCF服务很新。我想写WCF Web服务,我正在学习教程。我似乎无法写任何东西,因为我一直都会收到这个错误:

  

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

我创建了一个基本的WCF库项目,它带有一个默认代码,如下所示:

IService1.cs

namespace WCFandEFService
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

    [DataContract]
    public class CompositeType
    {
        [DataMember]
        public bool BoolValue { get; set; }

        [DataMember]
        public string StringValue { get; set; }
    }
}

Service1.cs

namespace WCFandEFService
{
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }

            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }

            return composite;
        }
    }
}

app.config档案:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.serviceModel>
    <services>
      <service name="WCFandEFService.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WCFandEFService/Service1/" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="WCFandEFService.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Ctrl-F5 之后,它说您的服务已托管。但我遇到了错误。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

将键值设置为false,因为它在内部使用线程。

<add key="aspnet:UseTaskFriendlySynchronizationContext" value="false" />

它会解决您的问题。

你也可以删除上面的键并尝试 (供参考)

What's the meaning of "UseTaskFriendlySynchronizationContext"?