IInstanceProvider.GetInstance 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回服務物件。
多載
GetInstance(InstanceContext) |
傳回提供給指定的 InstanceContext 物件的服務物件。 |
GetInstance(InstanceContext, Message) |
傳回提供給指定的 InstanceContext 物件的服務物件。 |
GetInstance(InstanceContext)
傳回提供給指定的 InstanceContext 物件的服務物件。
public:
System::Object ^ GetInstance(System::ServiceModel::InstanceContext ^ instanceContext);
public object GetInstance (System.ServiceModel.InstanceContext instanceContext);
abstract member GetInstance : System.ServiceModel.InstanceContext -> obj
Public Function GetInstance (instanceContext As InstanceContext) As Object
參數
- instanceContext
- InstanceContext
目前的 InstanceContext 物件。
傳回
使用者定義的服務物件。
範例
下列程式碼範例會示範如何實作提供「單一」行為的 IInstanceProvider,它會一律傳回相同的服務執行個體,而且不會加以回收。
public class ObjectProviderBehavior : IInstanceProvider
{
string message;
SampleService service = null;
public ObjectProviderBehavior(string msg)
{
Console.WriteLine("The non-default constructor has been called.");
this.message = msg;
this.service = new SampleService("Singleton sample service.");
}
#region IInstanceProvider Members
public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
{
Console.WriteLine("GetInstance is called:");
return this.service;
}
public object GetInstance(InstanceContext instanceContext)
{
Console.WriteLine("GetInstance is called:");
return this.service;
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
Console.WriteLine("ReleaseInstance is called. The SingletonBehaviorAttribute never releases.");
}
#endregion
}
Public Class ObjectProviderBehavior
Implements IInstanceProvider
Private message As String
Private service As SampleService = Nothing
Public Sub New(ByVal msg As String)
Console.WriteLine("The non-default constructor has been called.")
Me.message = msg
Me.service = New SampleService("Singleton sample service.")
End Sub
#Region "IInstanceProvider Members"
Public Function GetInstance(ByVal instanceContext As InstanceContext, ByVal message As System.ServiceModel.Channels.Message) As Object Implements IInstanceProvider.GetInstance
Console.WriteLine("GetInstance is called:")
Return Me.service
End Function
Public Function GetInstance(ByVal instanceContext As InstanceContext) As Object Implements IInstanceProvider.GetInstance
Console.WriteLine("GetInstance is called:")
Return Me.service
End Function
Public Sub ReleaseInstance(ByVal instanceContext As InstanceContext, ByVal instance As Object) Implements IInstanceProvider.ReleaseInstance
Console.WriteLine("ReleaseInstance is called. The SingletonBehaviorAttribute never releases.")
End Sub
#End Region
End Class
下列程式碼範例會示範如何實作會實作 IContractBehavior 的自訂屬性,以插入自訂服務執行個體提供者。 它也會實作 IContractBehaviorAttribute,這個屬性會結合使用特定合約。
public class SingletonBehaviorAttribute : Attribute, IContractBehaviorAttribute, IContractBehavior
{
#region IContractBehaviorAttribute Members
public Type TargetContract
{
get { return typeof(ISampleService); }
}
#endregion
#region IContractBehavior Members
public void AddBindingParameters(ContractDescription description, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection parameters)
{
return;
}
public void ApplyClientBehavior(ContractDescription description, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
return;
}
public void ApplyDispatchBehavior(ContractDescription description, ServiceEndpoint endpoint, DispatchRuntime dispatch)
{
dispatch.InstanceProvider = new ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.");
}
public void Validate(ContractDescription description, ServiceEndpoint endpoint)
{
return;
}
#endregion
}
Public Class SingletonBehaviorAttribute
Inherits Attribute
Implements IContractBehaviorAttribute, IContractBehavior
#Region "IContractBehaviorAttribute Members"
Public ReadOnly Property TargetContract() As Type Implements IContractBehaviorAttribute.TargetContract
Get
Return GetType(ISampleService)
End Get
End Property
#End Region
#Region "IContractBehavior Members"
Public Sub AddBindingParameters(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal parameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IContractBehavior.AddBindingParameters
Return
End Sub
Public Sub ApplyClientBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) Implements IContractBehavior.ApplyClientBehavior
Return
End Sub
Public Sub ApplyDispatchBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal dispatch As DispatchRuntime) Implements IContractBehavior.ApplyDispatchBehavior
dispatch.InstanceProvider = New ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.")
End Sub
Public Sub Validate(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint) Implements IContractBehavior.Validate
Return
End Sub
#End Region
End Class
備註
GetInstance(InstanceContext)使用 方法來控制 WCF 服務嘗試建立新物件時所收到的確切服務物件。
適用於
GetInstance(InstanceContext, Message)
傳回提供給指定的 InstanceContext 物件的服務物件。
public:
System::Object ^ GetInstance(System::ServiceModel::InstanceContext ^ instanceContext, System::ServiceModel::Channels::Message ^ message);
public object GetInstance (System.ServiceModel.InstanceContext instanceContext, System.ServiceModel.Channels.Message message);
abstract member GetInstance : System.ServiceModel.InstanceContext * System.ServiceModel.Channels.Message -> obj
Public Function GetInstance (instanceContext As InstanceContext, message As Message) As Object
參數
- instanceContext
- InstanceContext
目前的 InstanceContext 物件。
- message
- Message
觸發建立服務物件的訊息。
傳回
服務物件。
範例
下列程式碼範例會示範如何實作提供「單一」行為的 IInstanceProvider,它會一律傳回相同的服務執行個體,而且不會加以回收。
public class ObjectProviderBehavior : IInstanceProvider
{
string message;
SampleService service = null;
public ObjectProviderBehavior(string msg)
{
Console.WriteLine("The non-default constructor has been called.");
this.message = msg;
this.service = new SampleService("Singleton sample service.");
}
#region IInstanceProvider Members
public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
{
Console.WriteLine("GetInstance is called:");
return this.service;
}
public object GetInstance(InstanceContext instanceContext)
{
Console.WriteLine("GetInstance is called:");
return this.service;
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
Console.WriteLine("ReleaseInstance is called. The SingletonBehaviorAttribute never releases.");
}
#endregion
}
Public Class ObjectProviderBehavior
Implements IInstanceProvider
Private message As String
Private service As SampleService = Nothing
Public Sub New(ByVal msg As String)
Console.WriteLine("The non-default constructor has been called.")
Me.message = msg
Me.service = New SampleService("Singleton sample service.")
End Sub
#Region "IInstanceProvider Members"
Public Function GetInstance(ByVal instanceContext As InstanceContext, ByVal message As System.ServiceModel.Channels.Message) As Object Implements IInstanceProvider.GetInstance
Console.WriteLine("GetInstance is called:")
Return Me.service
End Function
Public Function GetInstance(ByVal instanceContext As InstanceContext) As Object Implements IInstanceProvider.GetInstance
Console.WriteLine("GetInstance is called:")
Return Me.service
End Function
Public Sub ReleaseInstance(ByVal instanceContext As InstanceContext, ByVal instance As Object) Implements IInstanceProvider.ReleaseInstance
Console.WriteLine("ReleaseInstance is called. The SingletonBehaviorAttribute never releases.")
End Sub
#End Region
End Class
下列程式碼範例會示範如何實作會實作 IContractBehavior 的自訂屬性,以插入自訂服務執行個體提供者。 它也會實作 IContractBehaviorAttribute,這個屬性會結合使用特定合約。
public class SingletonBehaviorAttribute : Attribute, IContractBehaviorAttribute, IContractBehavior
{
#region IContractBehaviorAttribute Members
public Type TargetContract
{
get { return typeof(ISampleService); }
}
#endregion
#region IContractBehavior Members
public void AddBindingParameters(ContractDescription description, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection parameters)
{
return;
}
public void ApplyClientBehavior(ContractDescription description, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
return;
}
public void ApplyDispatchBehavior(ContractDescription description, ServiceEndpoint endpoint, DispatchRuntime dispatch)
{
dispatch.InstanceProvider = new ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.");
}
public void Validate(ContractDescription description, ServiceEndpoint endpoint)
{
return;
}
#endregion
}
Public Class SingletonBehaviorAttribute
Inherits Attribute
Implements IContractBehaviorAttribute, IContractBehavior
#Region "IContractBehaviorAttribute Members"
Public ReadOnly Property TargetContract() As Type Implements IContractBehaviorAttribute.TargetContract
Get
Return GetType(ISampleService)
End Get
End Property
#End Region
#Region "IContractBehavior Members"
Public Sub AddBindingParameters(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal parameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IContractBehavior.AddBindingParameters
Return
End Sub
Public Sub ApplyClientBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) Implements IContractBehavior.ApplyClientBehavior
Return
End Sub
Public Sub ApplyDispatchBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal dispatch As DispatchRuntime) Implements IContractBehavior.ApplyDispatchBehavior
dispatch.InstanceProvider = New ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.")
End Sub
Public Sub Validate(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint) Implements IContractBehavior.Validate
Return
End Sub
#End Region
End Class
備註
GetInstance(InstanceContext, Message)使用 方法來控制 WCF 服務嘗試建立新物件時所收到的確切服務物件。