IInstanceProvider.GetInstance Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Retourne un objet de service.
Surcharges
GetInstance(InstanceContext) |
Retourne un objet de service en fonction de l'objet InstanceContext spécifié. |
GetInstance(InstanceContext, Message) |
Retourne un objet de service en fonction de l'objet InstanceContext spécifié. |
GetInstance(InstanceContext)
Retourne un objet de service en fonction de l'objet InstanceContext spécifié.
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
Paramètres
- instanceContext
- InstanceContext
Objet InstanceContext actuel.
Retours
Objet de service défini par l'utilisateur.
Exemples
L'exemple de code suivant indique comment implémenter IInstanceProvider pour fournir un comportement singleton ; il retourne toujours la même instance de service et ne la recycle pas.
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
L'exemple de code suivant indique comment implémenter un attribut personnalisé qui implémente IContractBehavior pour insérer le fournisseur d'instances de service personnalisé. Il implémente également IContractBehaviorAttribute, qui lie son utilisation à un contrat spécifique.
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
Remarques
Utilisez la GetInstance(InstanceContext) méthode pour contrôler l’objet de service exact qu’un service WCF reçoit lorsqu’il tente de en créer un.
S’applique à
GetInstance(InstanceContext, Message)
Retourne un objet de service en fonction de l'objet InstanceContext spécifié.
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
Paramètres
- instanceContext
- InstanceContext
Objet InstanceContext actuel.
- message
- Message
Message qui a déclenché la création d'un objet de service.
Retours
Objet de service.
Exemples
L'exemple de code suivant indique comment implémenter IInstanceProvider pour fournir un comportement singleton ; il retourne toujours la même instance de service et ne la recycle pas.
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
L'exemple de code suivant indique comment implémenter un attribut personnalisé qui implémente IContractBehavior pour insérer le fournisseur d'instances de service personnalisé. Il implémente également IContractBehaviorAttribute, qui lie son utilisation à un contrat spécifique.
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
Remarques
Utilisez la GetInstance(InstanceContext, Message) méthode pour contrôler l’objet de service exact qu’un service WCF reçoit lorsqu’il tente de en créer un.