创建 wcf 服务后,如何从 wsdl 中判断它是restful 还是soap? [英] After creating a wcf service how do I tell whether its restful or soap from the wsdl?

查看:47
本文介绍了创建 wcf 服务后,如何从 wsdl 中判断它是restful 还是soap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a service and I'm presented with a page saying:

您已经创建了一个服务.

You have created a service.

要测试此服务,您需要创建一个客户端并使用它来调用服务.您可以使用来自命令行的 svcutil.exe 工具使用以下语法:

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 等中分辨出来?

But how do I tell if its a SOAP or a REST service from that? How would I tell from the wsdl etc?

服务配置:

<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 端点.我什至正确配置了吗?

So basically I browse to the .svc file and I see a link for a wsdl. But how do I know if thats for the SOAP or REST endpoint. Have I even configured it correctly?

谢谢

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

UPDATE: 17:49 (UK TIME)

<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...

Pete, try this - no metadata publishing, nothing - just webHttpBinding - you should not see any WSDL anymore...

<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 和 REST 的混合.在 WSDL 上,SOAP 端点将显示在 wsdl:definitions/wsdl:service/wsdl:port 元素中;REST 端点不会.因此,如果服务中只有一个端点,如果 WSDL 中有 wsdl:port 条目,那么它就是一个 SOAP 端点;否则就是 REST.

The service can be both REST and SOAP, in a way that a WCF service can have multiple endpoints, including a mix of both SOAP and REST. On the WSDL, the SOAP endpoints will show up in the wsdl:definitions/wsdl:service/wsdl:port element; the REST endpoints will not. So if you only have one endpoint in the service, if there is a wsdl:port entry in the WSDL, then it's a SOAP endpoint; otherwise it's REST.

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

You can run the code below and look at the wsdl to see that it only shows up one wsdl:port element, for the SOAP endpoint.

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();
    }
}

这篇关于创建 wcf 服务后,如何从 wsdl 中判断它是restful 还是soap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆