ServiceContractAttribute.Namespace 屬性

定義

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

C#
public string Namespace { get; set; }

屬性值

<portType> 元素的 WSDL 命名空間。 預設值是 "http://tempuri.org"

範例

下列程式碼範例示範如何使用 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) 用戶端。 此用戶端會使用 HelloWorldClient 用戶端,而不是 SampleServiceClient 用戶端 (,如同範例一節 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();
    }
  }
}

適用於

產品 版本
.NET Core 1.0, Core 1.1, 8
.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, 4.8.1
UWP 10.0