【问题标题】:Why there are Response return types in WCF async methods?为什么 WCF 异步方法中有响应返回类型?
【发布时间】:2013-11-22 12:26:08
【问题描述】:

我在 WCF 中并不是那么完美,并且仍然是一个学习者。我学习 WCF 服务的地方告诉我,WCF 服务异步方法也返回与原始方法相同的数据类型。它在我下载的项目上运行良好,但使用相同的配置我创建了一个新项目并且它没有返回原始方法的数据类型。而是像这样的响应类型:

如您所见,它显示了isEmailExistsResponse 类型。 如何让它返回 bool 类型?

我的配置

客户端:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService1"/>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/Service1.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1" contract="WCORE.IService1" name="BasicHttpBinding_IService1"/>
    </client>
  </system.serviceModel>

服务器端:

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

我在客户端和服务上都使用 .NET 4.5。但是我的服务器安装的是4.0。因此,如果服务在最终部署时遇到问题,我可能会切换到 4.0。

WCF 客户端设置:

【问题讨论】:

  • 你贴出图片的代码???哦,来吧!
  • 它不是“代码图片”,而是错误。以便人们更好地理解。

标签: c# asp.net .net wcf web-services


【解决方案1】:

你需要awaitAsync方法:

var isEmailExists = await client.isEmailsExistsAsync(email);
if (isEmailExists == false) {
...
}

【讨论】:

  • 我认为必须在 WCF 客户端设置窗口中进行设置,如上所示。我不记得那个设置了,这就是我问的原因。
  • if(await client.isEmailExistsAsync(email)) 实现此代码的函数前面有async 关键字。
【解决方案2】:

无论您在哪里看到Task&lt;SomeOtherType&gt; 的返回类型,您都必须使用新的await 关键字来处理它的属性。

http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx

【讨论】:

  • 是的,我知道@Knagis,但问题是当我使用await 时仍然会出现错误。
猜你喜欢
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 2017-08-16
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多