语言

ServiceContractAttribute.Name 属性

定义

获取或设置 Web 服务描述语言(WSDL)中元素的名称 <portType> 。

public:
 property System::String ^ Name { System::String ^ get(); void set(System::String ^ value); };
public string Name { get; set; }
member this.Name : string with get, set
Public Property Name As String

属性值

默认值是应用其所 ServiceContractAttribute 应用到的类或接口的名称。

例外

该值为 null。

该值为空字符串。

示例

下面的代码示例演示如何使用 Name 和 Namespace 属性 ServiceContractAttribute 在 WSDL 中设置相应的值。

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;

namespace Microsoft.WCF.Documentation
{
  [ServiceContract(
    Name="HelloWorld",
    Namespace="http://Microsoft.WCF.Documentation"
  )]
  public interface ISampleService{
    [OperationContract]
    string SampleMethod(string msg);
  }

  class SampleService : ISampleService
  {
  #region ISampleService Members

  public string  SampleMethod(string msg)
  {
      return "The service greets you: " + msg;
  }

  #endregion
  }
}

下面的代码示例演示使用 ServiceModel 元数据实用工具工具(Svcutil.exe)导入 WSDL 的上述服务的 Windows Communication Foundation (WCF) 客户端。 此客户端使用 HelloWorldProxy 客户端而不是 SampleServiceProxy 客户端(如示例部分中的示例所示 ServiceContractAttribute)。

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

public class Client
{
  public static void Main()
  {
    // Picks up configuration from the config file.
    HelloWorldClient wcfClient = new HelloWorldClient();
    try
    {
      // Making calls.
      Console.WriteLine("Enter the greeting to send: ");
      string greeting = Console.ReadLine();
      Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting));

      // Done with service.
      wcfClient.Close();
      Console.WriteLine("Done!");
    }
    catch (TimeoutException timeProblem)
    {
      Console.WriteLine("The service operation timed out. " + timeProblem.Message);
      wcfClient.Abort();
    }
    catch (CommunicationException commProblem)
    {
      Console.WriteLine("There was a communication problem. " + commProblem.Message);
      wcfClient.Abort();
    }
    finally
    {
      Console.WriteLine("Press ENTER to exit:");
      Console.ReadLine();
    }
  }
}

注解

Name使用属性和Namespace属性控制 WSDL 中元素的名称和命名空间<portType>。

适用于