AspNetCompatibilityRequirementsMode 枚举
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指定 Windows Communication Foundation (WCF) 服务是否以与 ASP.NET 兼容的模式运行或是否可以运行。
public enum class AspNetCompatibilityRequirementsMode
public enum AspNetCompatibilityRequirementsMode
type AspNetCompatibilityRequirementsMode =
Public Enum AspNetCompatibilityRequirementsMode
- 继承
字段
Allowed | 1 | WCF 服务可以在应用程序域中运行,ASP.NET 兼容模式设置为 |
NotAllowed | 0 | WCF 服务必须在应用程序域中运行,ASP.NET 兼容模式设置为 |
Required | 2 | WCF 服务必须在应用程序域中运行,ASP.NET 兼容模式设置为 |
示例
服务开发人员可以通过将AspNetCompatibilityRequirementsAttribute.RequirementsMode属性设置为 Required
来确保其服务仅在 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>。