gpt4 book ai didi

c# - 如何将 WCF 服务从 SOAP 转换为 REST?

转载 作者:行者123 更新时间:2023-11-30 20:20:24 24 4
gpt4 key购买 nike

我是 WCF 的新手,对此我有疑问。

阅读一些文章后,我发现在 web.config 文件中,如果我将端点绑定(bind)从 basicHttpBinding 更改为 webHttpBinding,并将 httpGetEnabled 从 true 更改为 false,它会使用 REST。

我的问题是,为了制作服务 SOAP 或 REST,我需要更改的只有这些吗?或者我需要更改/添加任何其他内容吗?

最佳答案

您可以在两个不同的端点公开服务。 SOAP 可以使用支持 SOAP 的绑定(bind),例如basicHttpBindingRESTful 可以使用 webHttpBinding。我假设您的 REST 服务将在 JSON 中,在这种情况下,您需要使用以下行为配置来配置两个端点

<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>

您场景中的端点配置示例是

<services>
<service name="TestService">
<endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="ITestService"/>
</service>
</services>

[WebGet]应用于操作合约,使其成为RESTful。例如

public interface ITestService
{
[OperationContract]
[WebGet]
string HelloWorld(string text)
}

添加服务引用后,SOAP 请求 SOAP 服务的客户端端点配置,

<client>
<endpoint address="http://www.example.com/soap" binding="basicHttpBinding"
contract="ITestService" name="BasicHttpBinding_ITestService" />
</client>

在 C# 中

TestServiceClient client = new TestServiceClient();
client.GetAccount("A123");

关于c# - 如何将 WCF 服务从 SOAP 转换为 REST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36855681/

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