C# 创建wcf服务后,如何从wsdl判断它是restful服务还是soap服务?

C# 创建wcf服务后,如何从wsdl判断它是restful服务还是soap服务?,c#,wcf,rest,soap,C#,Wcf,Rest,Soap,我创建了一个服务,我看到一个页面,上面写着: 您已经创建了一个服务 要测试此服务,您需要 创建一个客户端并使用它调用 服务您可以使用 命令行中的svcutil.exe工具 使用以下语法: 但我如何从中分辨出它是SOAP还是REST服务呢?我将如何从wsdl等中辨别 服务配置: <services> <service name="VLSContentService" behaviorConfiguration="VLSContentServic

我创建了一个服务,我看到一个页面,上面写着:

您已经创建了一个服务

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

但我如何从中分辨出它是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>

如果您拥有一个WSDL,那么它就是一个SOAP服务

REST没有WSDL


REST有一个类似的概念,称为WADL-Web应用程序描述语言(),但它远不如SOAP的WSDL那样成熟和广泛使用

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

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

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

通过RESTful,一个根本没有WSDL的人!是的,显然您得到了一个WSDL——因为您在服务配置中定义了一个SOAP端点(
name=SOAP
)!在没有
basicHttpBinding
端点的情况下尝试它-您不会仅获得REST端点的WSDL。嗨,Marc_s,我想我们现在已经走到了十字路口。实际上,我已经去掉了SOAP配置,仍然得到了wsdl。Carlos在下面的帖子中说这是因为我有。你仍然有元数据端点!您启用了“元数据交换”——这将发布一个SOAP端点……您好,很抱歉,当我使用c#it dos创建REST WCF服务时,实际上创建了一个wsdl。它就在屏幕上。嗨,好的,这里是:@pete2k:嗯,是的-你的服务中有一个SOAP端点-所以很明显你会得到一个WSDL-用于该SOAP端点但是对于REST端点没有任何东西——如果您只有REST端点,那么就不会有WSDL。我的观点是:如果您只有一个REST端点,那么就没有WSDL,因为REST不处理WSDL。@Pete2k-
定义了一个SOAP端点,所以您有一个WSDL。马克是正确的,如果你有一个WSDL,那么WCF要么认为它是一个SOAP服务,要么它被告知发布SOAP WSDL(通过拥有一个MEX端点)。@Pete2k-如果你像你所说的那样“去掉了其余端点的绑定”,那么这意味着你只剩下SOAP端点,所以你仍然会有一个WSDL。你的意思是相反的吗;您删除了SOAP端点,只在配置中留下了其余的端点?carlos,我正在进行另一次聊天,讨论为什么在我删除SOAP绑定时wsdl仍然出现。应该吗?[我假设您的意思是删除了SOAP端点,而不仅仅是绑定]好吧,您启用了它(),因此它将为您的服务创建WSDL文档。这不太常见,但希望为仅REST服务使用WSDL的一个原因是WSDL包含数据契约定义(如果使用svcutil,将生成数据契约定义)。然而,服务契约可能是不正确的(因为诸如UriTemplate/WebGet/BodyStyle/etc之类的东西不会出现在WSDL中)。
<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>
<system.serviceModel>
   <services>
      <service name="VLSContentService">
          <endpoint name="rest" 
              address="" 
              binding="webHttpBinding" 
              contract="IVLSContentServiceREST" />
      </service>
   </services>
   <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
</system.serviceModel>
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();
    }
}