遠端處理範例:在網際網路資訊服務 (IIS) 中進行裝載
下列範例實作具有一些複雜度的基本 Web 服務。因為承載較簡潔,系統只需要較少的時間來序列化並還原序列化該資料流,所以會使用 BinaryFormatter。此外,如果網際網路資訊服務 (IIS) 使用的是 Windows 整合式驗證 (也稱為 NTLM 驗證),則伺服器會驗證用戶端,然後將 IIS 可驗證的識別傳回給用戶端。最後,您可將用戶端組態檔中的 URL 變更為使用 "https" 做為通訊協定配置,並將 IIS 設定為需要該虛擬目錄的 Secure Sockets Layer (SSL) 加密 (這個範例未示範這個處理序),藉以協助保護 Web 服務。
警告
.NET Framework 遠端處理預設不會進行驗證或加密。因此,建議在與用戶端或伺服器進行遠端互動之前,先採取所有必要步驟以確定這些用戶端或伺服器的識別。因為 .NET Framework 遠端處理應用程式需要 FullTrust 權限才能執行,所以如果將伺服器的存取權授與未授權用戶端,則該用戶端會將程式碼當成已完全信任般地執行。請一律驗證您的端點並加密通訊資料流,方法是在 IIS 中裝載遠端處理的型別,或建置自訂通道接收組,以完成這項作業。
編譯和執行這個範例
將所有檔案儲存在名為 RemoteIIS 的目錄中。
在命令提示字元中輸入下列命令,以編譯整個範例:
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
建立 \bin 子目錄,並將
ServiceClass.dll
複製至該目錄。在 IIS 中建立虛擬目錄。產生虛擬目錄的別名 "HttpBinary",並將來源目錄設定為 "RemoteIIS" 目錄。
將這個虛擬目錄的驗證方法設定為整合式 Windows 驗證 (之前稱為 NTLM 驗證)。
確定已啟動 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="http://localhost:80/HttpBinary/SAService.rem"
type="ServiceClass, ServiceClass"
/>
</client>
</application>
</system.runtime.remoting>
</configuration>
請參閱
概念
遠端應用程式組態
在網際網路資訊服務 (IIS) 中裝載遠端物件