ServiceContractAttribute.Name 屬性

定義

取得或設定 Web 服務描述語言 (WSDL) 中的 <portType> 項目名稱。

C#
public string Name { get; set; }

屬性值

String

預設值是套用 ServiceContractAttribute 之類別或介面的名稱。

例外狀況

值為 null。

值為空字串。

範例

下列程式碼範例示範如何使用 NameServiceContractAttributeNamespace 屬性,在 WSDL 中設定對應的值。

C#
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 中的範例) 。

C#
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>

適用於

產品 版本
.NET Core 1.0, Core 1.1
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
UWP 10.0