共用方式為


遠端處理範例:在 Internet Information Services (IIS) 中進行裝載

下列範例實作有一些複雜度的基本 Web 服務。此處使用 BinaryFormatter,因為負載量較小,所以系統花費較少的時間來序列化和還原序列化資料流。除此之外,如果 Internet Information Services (IIS) 使用 Windows 整合式驗證 (也就是 NTLM 驗證),伺服器會驗證用戶端,然後將IIS 能夠驗證的識別傳回給用戶端。最後,如果您變更用戶端組態檔中的 URL 以使用 "https" 作為通訊協定配置,並設定 IIS 以取得虛擬目錄的 Secure Sockets Layer (SSL) 驗證 (本範例沒有示範這個處理序),您就已經為您的 Web 服務設定好安全性了。

**注意   **.NET 遠端處理依預設不會執行驗證或加密。因此,建議您最好採取所有必要步驟,在與用戶端或伺服器遠端互動前,先確定其識別。由於 .NET 遠端處理應用程式需要 FullTrust 使用權限才能執行,因此如果讓未授權用戶端存取您的伺服器,該用戶端便可以執行程式碼,如同它完全受信任一般。永遠驗證您的結束點並加密通訊資料流,方法是在 IIS 中裝載遠端型別,或是建置自訂通道接收組來完成這個工作。

編譯和執行這個範例

  1. 將所有檔案儲存在名為 RemoteIIS 的目錄。

  2. 在命令提示中輸入下列命令以編譯整個範例:

    csc /noconfig /t:library /r:System.Web.dll /out:ServiceClass.dll ServiceClass.cs

    csc /noconfig /r:System.Runtime.Remoting.dll /r:System.dll /r:ServiceClass.dll Client.cs

  3. 建立 \bin 子目錄並將 ServiceClass.dll 複製到那個目錄中。

  4. 在 IIS 中建立一個虛擬目錄。將虛擬目錄別名 (Alias) 命名為「HttpBinary」並將來源目錄設定為「RemoteIIS」目錄。

  5. 將這個虛擬目錄的驗證方法設定為整合式 Windows 驗證 (之前的 NTLM 驗證)。

  6. 確定 IIS 已啟動,並在「RemoteIIS」目錄的命令提示中輸入 client

這個應用程式可以在單台電腦或網路上執行。如果您希望在網路上執行這個應用程式,必須將用戶端組態中的**「localhost」**取代成遠端電腦的名稱。

ServiceClass.cs

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Threading;
using System.Web;

public interface IService{

   DateTime GetServerTime();
   string GetServerString();

}

// IService exists to demonstrate the possibility of publishing only the interface.
public class ServiceClass : MarshalByRefObject, IService{

   private int InstanceHash;

   public ServiceClass(){
      InstanceHash = this.GetHashCode();
   }

   public DateTime GetServerTime(){
      return DateTime.Now;
   }

   public string GetServerString(){
      // Use the HttpContext to acquire what IIS thinks the client's identity is.
      string temp = HttpContext.Current.User.Identity.Name;
      if (temp == null || temp.Equals(string.Empty))
         temp = "**unavailable**";
      return "Hi there. You are being served by instance number: " 
         + InstanceHash.ToString() 
         + ". Your alias is: " 
         + temp;
   }
}

Web.config

<configuration>
   <system.runtime.remoting>
      <application>
         <service>
            <wellknown 
               mode="SingleCall" objectUri="SAService.rem"
               type="ServiceClass, ServiceClass"/>
         </service>
         <channels>
            <channel ref="http"/>
         </channels>
      </application>
   </system.runtime.remoting>
</configuration>

Client.cs

using System;
using System.Collections;
using System.Diagnostics;
using System.Net;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Security.Principal;

public class Client{

   public static void Main(string[] Args){

         // Tells the system about the remote object and customizes the HttpChannel
         // to use the binary formatter (which understands that base64 encoding is needed).
         RemotingConfiguration.Configure("Client.exe.config");

         // New proxy for the ServiceClass.
         // If you publish only the IService interface, you must use Activator.GetObject.
         ServiceClass service = new ServiceClass(); 
            
         // Programmatically customizes the properties given to the channel. This sample uses the
         // application configuration file.
         // IDictionary Props = ChannelServices.GetChannelSinkProperties(service);
         // Props["credentials"] = CredentialCache.DefaultCredentials;

         // Reports the client identity name.
         Console.WriteLine("ConsoleIdentity: " + WindowsIdentity.GetCurrent().Name);

         // Writes what the server returned.
         Console.WriteLine("The server says : " + service.GetServerString());
         Console.WriteLine("Server time is: " + service.GetServerTime());      
   }
}

Client.exe.config

<configuration>
   <system.runtime.remoting>
      <application>
         <channels>
            <channel ref="http" useDefaultCredentials="true" port="0">
               <clientProviders>
                  <formatter 
                     ref="binary"
                  />
               </clientProviders>
            </channel>
         </channels>
         <client>
            <wellknown 
               url="https://localhost:80/HttpBinary/SAService.rem"
               type="ServiceClass, ServiceClass"
            />
         </client>
      </application>
   </system.runtime.remoting>
</configuration>

請參閱

遠端處理範例 | 組態 | 在 Internet Information Services (IIS) 中裝載遠端物件