gpt4 book ai didi

c# - 如何从 Services .NET 中过滤掉 WCF 异步方法

转载 作者:行者123 更新时间:2023-11-30 16:48:37 25 4
gpt4 key购买 nike

我有带有 WCF 的 .NET C♯ 应用程序。我已经实现了简单的 hello World 并从数据库中读取了简单的操作,但我正在自动获取 wcf Async 方法以及我的方法。

我有两个问题

1- 如果我不想从 WCF 服务中实现,如何删除

2- 如果我愿意,我如何实现 WCF 异步方法

我有两种 HeloWorld 方法,但还有其他方法

标准方法

namespace App.Services.Contracts
{
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string GetMessage(string name);
}
}

异步方法

namespace App.Services.Contracts
{
[ServiceContract]
public interface IHelloWorldServiceAsyn
{
[OperationContract]
Task<string> GetMessageAsyn(string name);
}
}

HelloWorld 服务的实现

 public class HelloWorldManager : IHelloWorldService, IHelloWorldServiceAsyn
{
public String GetMessage(string name)
{
return "This is test Service Flow, Hello world from " + name + "!";
}

public async Task<string> GetMessageAsyn(string name)
{
var task = Task.Factory.StartNew(() =>
{
Thread.Sleep(1000);
return "This is test Service Flow, Hello world from " + name + "!";
});

return await task.ConfigureAwait(false);
}
}

服务模型配置

  <service name="App.Services.Managers.HelloWorldManager" behaviorConfiguration="DefaultServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8087/CreditUnionServices/HelloWorldServices" />
</baseAddresses>
</host>
<endpoint address="HelloWorld" binding="wsHttpBinding" bindingConfiguration="DefaultBindingConfiguration" contract="App.Services.Contracts.IHelloWorldService"></endpoint>
<endpoint address="HelloWorldAsyn" binding="wsHttpBinding" bindingConfiguration="DefaultBindingConfiguration" contract="App.Services.Contracts.IHelloWorldServiceAsyn"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="DefaultMexBindingConfiguration" contract="IMetadataExchange"></endpoint>
</service>

您可以在下面的屏幕截图中看到我有重复的 Async 方法,我如何确保它不会自动生成以便在我需要时自己生成

enter image description here

最佳答案

有线协议(protocol)中没有异步。客户端和服务器无法判断远程端是以异步还是同步方式实现的。每侧只需要一个,它们可以不同。

只执行一次服务器。不要有两种方法或两种接口(interface)。

在客户端上,您可以在同步和异步方法之间自由选择。您可以在服务引用设置中禁用异步方法。

I have explained this here.

关于c# - 如何从 Services .NET 中过滤掉 WCF 异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37917463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com