語言

ServiceContractAttribute.Namespace 屬性

定義

取得或設定該元素的 <portType> 命名空間,並以網路服務描述語言(WSDL)為基礎。

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

屬性值

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

範例

以下程式碼範例說明如何使用 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
  }
}

以下程式碼範例展示了一個 Windows Communication Foundation(WCF)用戶端,該用戶端為前一服務使用 服務模型元資料工具工具(Svcutil.exe)匯入 WSDL。 此用戶端使用 HelloWorldClient 用戶端而非用戶端 SampleServiceClient (如範例部分 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();
    }
  }
}

適用於