共用方式為


AspNetCompatibilityRequirementsMode 列舉

定義

指定 Windows Communication Foundation (WCF) 服務是否在 (也就是可在) 與 ASP.NET 相容的模式中執行。

public enum class AspNetCompatibilityRequirementsMode
public enum AspNetCompatibilityRequirementsMode
type AspNetCompatibilityRequirementsMode = 
Public Enum AspNetCompatibilityRequirementsMode
繼承
AspNetCompatibilityRequirementsMode

欄位

Allowed 1

WCF 服務可以在應用程式域中執行,ASP.NET 相容性模式設定為 truefalse

NotAllowed 0

WCF 服務必須在應用程式域中執行,ASP.NET 相容性模式設定為 false

Required 2

WCF 服務必須在應用程式域中執行,ASP.NET 相容性模式設定為 true

範例

服務開發人員可以將 屬性設定 AspNetCompatibilityRequirementsAttribute.RequirementsModeRequired ,以確保其服務只在 ASP.NET 相容性模式中執行,如下列範例所示:

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculatorSession
{
    [OperationContract]
    void Clear();
    [OperationContract]
    void AddTo(double n);
    [OperationContract]
    void SubtractFrom(double n);
    [OperationContract]
    void MultiplyBy(double n);
    [OperationContract]
    void DivideBy(double n);
    [OperationContract]
    double Equals();
}
<ServiceContract(Namespace:="http://Microsoft.ServiceModel.Samples")> _
Public Interface ICalculatorSession

    <OperationContract()> _
    Sub Clear()
    <OperationContract()> _
    Sub AddTo(ByVal n As Double)
    <OperationContract()> _
    Sub SubtractFrom(ByVal n As Double)
    <OperationContract()> _
    Sub MultiplyBy(ByVal n As Double)
    <OperationContract()> _
    Sub DivideBy(ByVal n As Double)
    <OperationContract()> _
    Function Equal() As Double
End Interface
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class CalculatorService : ICalculatorSession
{
    double result
    {   // Store result in AspNet session.
        get
        {
            if (HttpContext.Current.Session["Result"] != null)
                return (double)HttpContext.Current.Session["Result"];
            return 0.0D;
        }
        set
        {
            HttpContext.Current.Session["Result"] = value;
        }
    }

    public void Clear()
    {
    }

    public void AddTo(double n)
    {
        result += n;
    }

    public void SubtractFrom(double n)
    {
        result -= n;
    }

    public void MultiplyBy(double n)
    {
        result *= n;
    }

    public void DivideBy(double n)
    {
        result /= n;
    }

    public double Equals()
    {
        return result;
    }
}
    <AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Required)> _
    Public Class CalculatorService
        Implements ICalculatorSession

        Property Result() As Double
            ' Store result in AspNet Session.
            Get
                If (HttpContext.Current.Session("Result") Is Nothing) Then
                    Return 0D
                End If
                Return HttpContext.Current.Session("Result")
            End Get
            Set(ByVal value As Double)
                HttpContext.Current.Session("Result") = value
            End Set
        End Property

        Public Sub Clear() _
 Implements ICalculatorSession.Clear
            Result = 0D
        End Sub

        Public Sub AddTo(ByVal n As Double) _
Implements ICalculatorSession.AddTo
            Result += n
        End Sub

        Public Sub SubtractFrom(ByVal n As Double) _
Implements ICalculatorSession.SubtractFrom

            Result -= n
        End Sub

        Public Sub MultiplyBy(ByVal n As Double) _
Implements ICalculatorSession.MultiplyBy

            Result *= n
        End Sub

        Public Sub DivideBy(ByVal n As Double) _
Implements ICalculatorSession.DivideBy

            Result /= n
        End Sub

        Public Function Equal() As Double _
Implements ICalculatorSession.Equal

            Return Result
        End Function
    End Class

備註

ASP.NET 相容性模式可讓 WCF 服務使用 ASP 功能,例如身分識別模擬。 這是透過 Web.config 檔案在應用程式層級啟用的,而且無法由應用程式中巢狀的 Web.config 檔案覆寫。 AspNetCompatibilityRequirementsMode未為服務指定值時,預設值為 Allowed 。 如需詳細資訊,請參閱 < serviceHostingEnvironment >

適用於