gpt4 book ai didi

c# - 创建 wcf 服务后,如何从 wsdl 判断它是 Restful 还是 SOAP 的?

转载 作者:太空狗 更新时间:2023-10-30 00:27:28 25 4
gpt4 key购买 nike

我创建了一个服务,我看到一个页面说:

You have created a service.

To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

但是我如何判断它是 SOAP 还是 REST 服务呢?我如何从 wsdl 等中分辨出来?

服务配置:

<services> 
<service name="VLSContentService"
behaviorConfiguration="VLSContentServiceBehaviour" >
<endpoint name="rest"
address=""
behaviorConfiguration="VLSContentServiceEndpointBehaviour"
binding="webHttpBinding"
contract="IVLSContentServiceREST" />
<endpoint name="soap"
address="soap"
binding="basicHttpBinding"
contract="IVLSContentServiceREST"/>
</service>
</services>

更新:

嗨,马克,

我的配置是:

 <services>
<service behaviorConfiguration="VLSContentServiceBehaviour" name="VLSContentService">
<endpoint name="rest" address="" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentServiceREST" />
<endpoint name="soap" address="soap" binding="basicHttpBinding" contract="IVLSContentServiceREST"/>
</service>
</services>

所以基本上我浏览到 .svc 文件,我看到了一个 wsdl 的链接。但是我怎么知道那是用于 SOAP 还是 REST 端点。我是否正确配置了它?

谢谢

更新:17:49(英国时间)

<system.serviceModel>

<!---Add the service-->
<services>
<service behaviorConfiguration="VLSContentServiceBehaviour" name="VLSContentService">
<endpoint name="rest"
address=""
behaviorConfiguration="VLSContentServiceEndpointBehaviour"
binding="webHttpBinding"
contract="IVLSContentServiceREST" />
</service>
</services>
<!---Add the behaviours-->
<behaviors>
<serviceBehaviors>
<behavior name="VLSContentServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="VLSContentServiceEndpointBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
</system.serviceModel>

ma​​rc_s 更新:18:22(英国时间)

Pete,试试这个 - 没有元数据发布,什么都没有 - 只是 webHttpBinding - 你应该不再看到任何 WSDL...

<system.serviceModel>
<services>
<service name="VLSContentService">
<endpoint name="rest"
address=""
binding="webHttpBinding"
contract="IVLSContentServiceREST" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
</system.serviceModel>

最佳答案

服务可以是 REST 和 SOAP,WCF 服务可以有多个端点,包括两者的混合 SOAP 和休息。在 WSDL 上,SOAP 端点将显示在 wsdl:definitions/wsdl:service/wsdl:port 元素中; REST 端点不会。因此,如果您在服务中只有一个端点,如果 WSDL 中有一个 wsdl:port 条目,那么它就是一个 SOAP 端点;否则就是 REST。

您可以运行下面的代码并查看 wsdl 以查看它只显示一个 wsdl:port 元素,用于 SOAP 端点。

public class StackOverflow_6414181
{
[ServiceContract]
public interface ITest
{
[OperationContract]
[WebGet]
string Echo(string text);
}
public class Service : ITest
{
public string Echo(string text)
{
return text;
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "soap");
host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "rest").Behaviors.Add(new WebHttpBehavior());
host.Open();
Console.WriteLine("Host opened");

Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}

关于c# - 创建 wcf 服务后,如何从 wsdl 判断它是 Restful 还是 SOAP 的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6414181/

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