Notatka
Dostęp do tej strony wymaga autoryzacji. Może spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Dotyczy:programu SQL Server
Ten artykuł opisuje, jak zaimplementować obsługę logiki biznesowej dla artykułu o merge w SQL Server, korzystając z programowania replikacyjnego lub Obiektów Zarządzania Replikacją (RMO).
Przestrzeń nazw Microsoft.SqlServer.Replication.BusinessLogicSupport implementuje interfejs, którego można używać do tworzenia złożonej logiki biznesowej obsługującej zdarzenia, które występują podczas procesu synchronizacji Merge Replication. Proces replikacji wywołuje metody w obsłudze logiki biznesowej dla każdego zmienionego wiersza, który replikuje podczas synchronizacji.
Ogólny proces implementowania procedury obsługi logiki biznesowej to:
Utwórz zestaw obsługi logiki biznesowej.
Zarejestruj zestaw w dystrybutorze.
Wdróż zestaw na serwerze, na którym działa agent scalania. W przypadku subskrypcji pull, agent działa na Subskrybencie. W przypadku subskrypcji push agent działa na Dystrybutorze. Gdy używasz synchronizacji sieci, agent działa na serwerze WWW.
Utwórz artykuł, który używa procedury obsługi logiki biznesowej lub zmodyfikuj istniejący artykuł, aby użyć programu obsługi logiki biznesowej.
Określona procedura obsługi logiki biznesowej jest wykonywana dla każdego zsynchronizowanych wierszy. Złożona logika i wywołania do innych aplikacji lub usług sieciowych mogą wpływać na wydajność. Aby uzyskać więcej informacji na temat procedur obsługi logiki biznesowej, zobacz Execute Business Logic During Merge Synchronization (Wykonywanie logiki biznesowej podczas synchronizacji scalania).
Wykorzystanie programowania replikacyjnego
Aby stworzyć i wdrożyć obsługę logiki biznesowej
W programie Microsoft Visual Studio utwórz nowy projekt dla zestawu .NET zawierającego kod implementujący procedurę obsługi logiki biznesowej.
Dodaj odwołania do projektu dla następujących przestrzeni nazw.
Referencja do zgromadzenia Lokalizacja Microsoft.SqlServer.Replication.BusinessLogicSupport < drive>:\Program Files\Microsoft SQL Server\nnn\COM (domyślna instalacja) System.Data GAC (składnik środowiska .NET Framework) System.Data.Common GAC (składnik platformy .NET Framework) Dodaj klasę, która zastępuje klasę BusinessLogicModule.
Zaimplementuj właściwość HandledChangeStates, aby wskazać typy zmian, które obsługuje program obsługi.
Zastąpij co najmniej jedną z następujących metod klasy BusinessLogicModule:
CommitHandler — wywoływana, gdy zmiana danych zostanie zatwierdzona podczas synchronizacji.
DeleteErrorHandler — wywoływane, gdy wystąpi błąd podczas przesyłania lub pobierania instrukcji DELETE.
DeleteHandler — wywoływane, gdy polecenia DELETE są przesyłane lub pobierane.
InsertErrorHandler - wywoływane, gdy wystąpi błąd podczas przesyłania lub pobierania instrukcji INSERT.
InsertHandler — wywoływane, gdy polecenia INSERT są przesyłane lub pobierane.
UpdateConflictsHandler — wywoływane, gdy u wydawcy i subskrybenta wystąpią sprzeczne instrukcje UPDATE.
UpdateDeleteConflictHandler — wywoływane, gdy instrukcje UPDATE powodują konflikt z instrukcjami DELETE po stronie publikującego i subskrybującego.
UpdateErrorHandler - wywoływane, gdy wystąpi błąd podczas przesyłania lub pobierania instrukcji UPDATE.
UpdateHandler — wywoływane, gdy polecenia UPDATE są przesyłane lub pobierane.
Skompiluj projekt, aby utworzyć zestaw obsługi logiki biznesowej.
Umieść zestaw w katalogu zawierającym plik wykonywalny Agenta scalającego (replmerg.exe), który dla instalacji domyślnej znajduje się na <dysku>:\Program Files\Microsoft SQL Server\nnn\COM lub zainstaluj go w globalnej pamięci podręcznej zestawów platformy .NET (GAC). Instaluj asembler w GAC tylko wtedy, gdy aplikacje inne niż Merge Agent wymagają dostępu do asemblera. Użyj narzędzia Global Assembly Cache (Gacutil.exe) dostępnego w .NET Framework SDK, aby zainstalować asembler w GAC.
Notatka
Musisz wdrożyć handler logiki biznesowej na każdym serwerze, na którym działa Merge Agent. Ten wymóg obejmuje serwer IIS, który hostuje replisapi.dll podczas synchronizacji sieci.
Aby zarejestrować mechanizm logiki biznesowej
U wydawcy wykonaj polecenie sp_enumcustomresolvers (Transact-SQL), aby sprawdzić, czy zestaw nie jest już zarejestrowany jako program obsługi logiki biznesowej.
U dystrybutora wykonaj sp_registercustomresolver (Transact-SQL), określając przyjazną nazwę programu obsługi logiki biznesowej dla @article_resolver, wartość true dla @is_dotnet_assembly, nazwę zestawu dla @dotnet_assembly_name oraz w pełni kwalifikowaną nazwę klasy, która zastępuje BusinessLogicModule dla @dotnet_class_name.
Notatka
Jeśli nie wdrożysz zestawu w tym samym katalogu co plik wykonywalny Agenta scalania, w tym samym katalogu co aplikacja, która uruchamia Agenta scalania synchronicznie, ani w globalnej pamięci podręcznej zestawów (GAC), musisz podać pełną ścieżkę wraz z nazwą zestawu dla @dotnet_assembly_name. Podczas korzystania z synchronizacji sieci należy określić lokalizację asemblera na serwerze WWW.
Aby użyć procedury obsługi logiki biznesowej z nowym artykułem tabeli
- Wykonaj sp_addmergearticle (Transact-SQL), aby zdefiniować artykuł, określając przyjazną nazwę programu obsługi logiki biznesowej dla @article_resolver. Aby uzyskać więcej informacji, zobacz
Define an Article (Definiowanie artykułu).
Aby użyć mechanizmu obsługi logiki biznesowej z istniejącym artykułem tabeli
- Uruchom sp_changemergearticle (Transact-SQL) i określ @publication, @article, article_resolver dla @property oraz przyjazną nazwę obsługi logiki biznesowej dla @value.
Przykłady (programowanie replikacji)
W tym przykładzie przedstawiono procedurę obsługi logiki biznesowej, która tworzy dziennik inspekcji.
using System;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.SqlServer.Replication.BusinessLogicSupport;
using Microsoft.Samples.SqlServer.BusinessLogicHandler;
namespace Microsoft.Samples.SqlServer.BusinessLogicHandler
{
public class OrderEntryBusinessLogicHandler :
Microsoft.SqlServer.Replication.BusinessLogicSupport.BusinessLogicModule
{
// Variables to hold server names.
private string publisherName;
private string subscriberName;
public OrderEntryBusinessLogicHandler()
{
}
// Implement the Initialize method to get publication
// and subscription information.
public override void Initialize(
string publisher,
string subscriber,
string distributor,
string publisherDB,
string subscriberDB,
string articleName)
{
// Set the Publisher and Subscriber names.
publisherName = publisher;
subscriberName = subscriber;
}
// Declare what types of row changes, conflicts, or errors to handle.
override public ChangeStates HandledChangeStates
{
get
{
// Handle Subscriber inserts, updates and deletes.
return ChangeStates.SubscriberInserts |
ChangeStates.SubscriberUpdates | ChangeStates.SubscriberDeletes;
}
}
public override ActionOnDataChange InsertHandler(SourceIdentifier insertSource,
DataSet insertedDataSet, ref DataSet customDataSet, ref int historyLogLevel,
ref string historyLogMessage)
{
if (insertSource == SourceIdentifier.SourceIsSubscriber)
{
// Build a line item in the audit message to log the Subscriber insert.
StringBuilder AuditMessage = new StringBuilder();
AuditMessage.Append(String.Format("A new order was entered at {0}. " +
"The SalesOrderID for the order is :", subscriberName));
AuditMessage.Append(insertedDataSet.Tables[0].Rows[0]["SalesOrderID"].ToString());
AuditMessage.Append("The order must be shipped by :");
AuditMessage.Append(insertedDataSet.Tables[0].Rows[0]["DueDate"].ToString());
// Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString();
// Set the history log level to the default verbose level.
historyLogLevel = 1;
// Accept the inserted data in the Subscriber's data set and
// apply it to the Publisher.
return ActionOnDataChange.AcceptData;
}
else
{
return base.InsertHandler(insertSource, insertedDataSet, ref customDataSet,
ref historyLogLevel, ref historyLogMessage);
}
}
public override ActionOnDataChange UpdateHandler(SourceIdentifier updateSource,
DataSet updatedDataSet, ref DataSet customDataSet, ref int historyLogLevel,
ref string historyLogMessage)
{
if (updateSource == SourceIdentifier.SourceIsPublisher)
{
// Build a line item in the audit message to log the Subscriber update.
StringBuilder AuditMessage = new StringBuilder();
AuditMessage.Append(String.Format("An existing order was updated at {0}. " +
"The SalesOrderID for the order is ", subscriberName));
AuditMessage.Append(updatedDataSet.Tables[0].Rows[0]["SalesOrderID"].ToString());
AuditMessage.Append("The order must now be shipped by :");
AuditMessage.Append(updatedDataSet.Tables[0].Rows[0]["DueDate"].ToString());
// Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString();
// Set the history log level to the default verbose level.
historyLogLevel = 1;
// Accept the updated data in the Subscriber's data set and apply it to the Publisher.
return ActionOnDataChange.AcceptData;
}
else
{
return base.UpdateHandler(updateSource, updatedDataSet,
ref customDataSet, ref historyLogLevel, ref historyLogMessage);
}
}
public override ActionOnDataDelete DeleteHandler(SourceIdentifier deleteSource,
DataSet deletedDataSet, ref int historyLogLevel, ref string historyLogMessage)
{
if (deleteSource == SourceIdentifier.SourceIsSubscriber)
{
// Build a line item in the audit message to log the Subscriber deletes.
// Note that the rowguid is the only information that is
// available in the dataset.
StringBuilder AuditMessage = new StringBuilder();
AuditMessage.Append(String.Format("An existing order was deleted at {0}. " +
"The rowguid for the order is ", subscriberName));
AuditMessage.Append(deletedDataSet.Tables[0].Rows[0]["rowguid"].ToString());
// Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString();
// Set the history log level to the default verbose level.
historyLogLevel = 1;
// Accept the delete and apply it to the Publisher.
return ActionOnDataDelete.AcceptDelete;
}
else
{
return base.DeleteHandler(deleteSource, deletedDataSet,
ref historyLogLevel, ref historyLogMessage);
}
}
}
}
Imports System
Imports System.Text
Imports System.Data
Imports System.Data.Common
Imports Microsoft.SqlServer.Replication.BusinessLogicSupport
Namespace Microsoft.Samples.SqlServer.BusinessLogicHandler
Public Class OrderEntryBusinessLogicHandler
Inherits BusinessLogicModule
' Variables to hold server names.
Private publisherName As String
Private subscriberName As String
' Implement the Initialize method to get publication
' and subscription information.
Public Overrides Sub Initialize( _
ByVal publisher As String, _
ByVal subscriber As String, _
ByVal distributor As String, _
ByVal publisherDB As String, _
ByVal subscriberDB As String, _
ByVal articleName As String _
)
' Set the Publisher and Subscriber names.
publisherName = publisher
subscriberName = subscriber
End Sub
' Declare what types of row changes, conflicts, or errors to handle.
Public Overrides ReadOnly Property HandledChangeStates() As ChangeStates
Get
' Handle Subscriber inserts, updates and deletes.
Return (ChangeStates.SubscriberInserts Or _
ChangeStates.SubscriberUpdates Or ChangeStates.SubscriberDeletes)
End Get
End Property
Public Overrides Function InsertHandler(ByVal insertSource As SourceIdentifier, _
ByVal insertedDataSet As DataSet, ByRef customDataSet As DataSet, _
ByRef historyLogLevel As Integer, ByRef historyLogMessage As String) _
As ActionOnDataChange
If insertSource = SourceIdentifier.SourceIsSubscriber Then
' Build a line item in the audit message to log the Subscriber insert.
Dim AuditMessage As StringBuilder = New StringBuilder()
AuditMessage.Append(String.Format("A new order was entered at {0}. " + _
"The SalesOrderID for the order is :", subscriberName))
AuditMessage.Append(insertedDataSet.Tables(0).Rows(0)("SalesOrderID").ToString())
AuditMessage.Append("The order must be shipped by :")
AuditMessage.Append(insertedDataSet.Tables(0).Rows(0)("DueDate").ToString())
' Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString()
' Set the history log level to the default verbose level.
historyLogLevel = 1
' Accept the inserted data in the Subscriber's data set and
' apply it to the Publisher.
Return ActionOnDataChange.AcceptData
Else
Return MyBase.InsertHandler(insertSource, insertedDataSet, customDataSet, _
historyLogLevel, historyLogMessage)
End If
End Function
Public Overrides Function UpdateHandler(ByVal updateSource As SourceIdentifier, _
ByVal updatedDataSet As DataSet, ByRef customDataSet As DataSet, _
ByRef historyLogLevel As Integer, ByRef historyLogMessage As String) _
As ActionOnDataChange
If updateSource = SourceIdentifier.SourceIsPublisher Then
' Build a line item in the audit message to log the Subscriber update.
Dim AuditMessage As StringBuilder = New StringBuilder()
AuditMessage.Append(String.Format("An existing order was updated at {0}. " + _
"The SalesOrderID for the order is ", subscriberName))
AuditMessage.Append(updatedDataSet.Tables(0).Rows(0)("SalesOrderID").ToString())
AuditMessage.Append("The order must now be shipped by :")
AuditMessage.Append(updatedDataSet.Tables(0).Rows(0)("DueDate").ToString())
' Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString()
' Set the history log level to the default verbose level.
historyLogLevel = 1
' Accept the updated data in the Subscriber's data set and apply it to the Publisher.
Return ActionOnDataChange.AcceptData
Else
Return MyBase.UpdateHandler(updateSource, updatedDataSet, _
customDataSet, historyLogLevel, historyLogMessage)
End If
End Function
Public Overrides Function DeleteHandler(ByVal deleteSource As SourceIdentifier, _
ByVal deletedDataSet As DataSet, ByRef historyLogLevel As Integer, _
ByRef historyLogMessage As String) As ActionOnDataDelete
If deleteSource = SourceIdentifier.SourceIsSubscriber Then
' Build a line item in the audit message to log the Subscriber deletes.
' Note that the rowguid is the only information that is
' available in the dataset.
Dim AuditMessage As StringBuilder = New StringBuilder()
AuditMessage.Append(String.Format("An existing order was deleted at {0}. " + _
"The rowguid for the order is ", subscriberName))
AuditMessage.Append(deletedDataSet.Tables(0).Rows(0)("rowguid").ToString())
' Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString()
' Set the history log level to the default verbose level.
historyLogLevel = 1
' Accept the delete and apply it to the Publisher.
Return ActionOnDataDelete.AcceptDelete
Else
Return MyBase.DeleteHandler(deleteSource, deletedDataSet, _
historyLogLevel, historyLogMessage)
End If
End Function
End Class
End Namespace
Poniższy przykład rejestruje zestaw obsługi logiki biznesowej w dystrybutorze i zmienia istniejący artykuł scalania, aby użyć tej niestandardowej logiki biznesowej.
DECLARE @publication AS sysname;
DECLARE @article AS sysname;
DECLARE @friendlyname AS sysname;
DECLARE @assembly AS nvarchar(500);
DECLARE @class AS sysname;
SET @publication = N'AdvWorksCustomers';
SET @article = N'Customers';
SET @friendlyname = N'OrderEntryLogic';
SET @assembly = N'C:\Program Files\Microsoft SQL Server\120\COM\CustomLogic.dll';
SET @class = N'Microsoft.Samples.SqlServer.BusinessLogicHandler.OrderEntryBusinessLogicHandler';
-- Register the business logic handler at the Distributor.
EXEC sys.sp_registercustomresolver
@article_resolver = @friendlyname,
@resolver_clsid = NULL,
@is_dotnet_assembly = N'true',
@dotnet_assembly_name = @assembly,
@dotnet_class_name = @class;
-- Add an article that uses the business logic handler
-- at the Publisher.
EXEC sp_changemergearticle
@publication = @publication,
@article = @article,
@property = N'article_resolver',
@value = @friendlyname,
@force_invalidate_snapshot = 0,
@force_reinit_subscription = 0;
GO
Korzystanie z obiektów zarządzania replikacją (RMO)
Aby utworzyć obsługę logiki biznesowej
W programie Microsoft Visual Studio utwórz nowy projekt dla zestawu .NET zawierającego kod implementujący procedurę obsługi logiki biznesowej.
Dodaj odwołania do projektu dla następujących przestrzeni nazw.
Referencja do zgromadzenia Lokalizacja Microsoft.SqlServer.Replication.BusinessLogicSupport < drive>:\Program Files\Microsoft SQL Server\nnn\COM (domyślna instalacja) System.Data GAC (składnik platformy .NET Framework) System.Data.Common GAC (składnik platformy .NET Framework) Dodaj klasę, która zastępuje klasę BusinessLogicModule.
Zaimplementuj właściwość HandledChangeStates, aby wskazać typy zmian, które obsługuje program obsługi.
Zastąpij co najmniej jedną z następujących metod klasy BusinessLogicModule:
CommitHandler — wywoływana, gdy zmiana danych zostanie zatwierdzona podczas synchronizacji.
DeleteErrorHandler — wywoływane, jeśli podczas przesyłania lub pobierania instrukcji DELETE wystąpi błąd.
DeleteHandler — wywoływane, gdy polecenia DELETE są przesyłane lub pobierane.
InsertErrorHandler — wywoływane, jeśli wystąpi błąd podczas przesyłania lub pobierania instrukcji INSERT.
InsertHandler — wywoływane, gdy polecenia INSERT są przesyłane lub pobierane.
UpdateConflictsHandler — wywoływane, gdy u wydawcy i subskrybenta wystąpią sprzeczne instrukcje UPDATE.
UpdateDeleteConflictHandler — wywoływane, gdy instrukcje UPDATE powodują konflikt z instrukcjami DELETE po stronie publikującego i subskrybującego.
UpdateErrorHandler — wywoływane, jeśli wystąpi błąd podczas przesyłania lub pobierania instrukcji UPDATE.
UpdateHandler — wywoływane, gdy polecenia UPDATE są przesyłane lub pobierane.
Notatka
Domyślny mechanizm rozstrzygania dla artykułu obsługuje wszelkie konflikty dotyczące artykułów, których twoja niestandardowa logika biznesowa nie obsługuje jawnie.
Skompiluj projekt, aby utworzyć zestaw obsługi logiki biznesowej.
Aby zarejestrować mechanizm logiki biznesowej
Utwórz połączenie z dystrybutorem przy użyciu klasy ServerConnection.
Utwórz wystąpienie klasy ReplicationServer. Przekaż ServerConnection z kroku 1.
Wywołaj EnumBusinessLogicHandlers i sprawdź zwrócony obiekt ArrayList, aby upewnić się, że zestaw nie jest już zarejestrowany jako program obsługi logiki biznesowej.
Utwórz wystąpienie klasy BusinessLogicHandler. Określ następujące właściwości:
DotNetAssemblyName — nazwa zestawu .NET. Jeśli nie wdrożysz asembla w tym samym katalogu co plik wykonywalny Merge Agent, w tym samym katalogu co aplikacja, która synchronicznie uruchamia Merge Agent, lub w GAC, dołącz pełną ścieżkę z nazwą asemblera. Należy uwzględnić pełną ścieżkę wraz z nazwą zestawu podczas korzystania z modułu obsługi logiki biznesowej przy synchronizacji z siecią Web.
DotNetClassName - w pełni kwalifikowana nazwa klasy, która zastępuje BusinessLogicModule i implementuje program obsługi logiki biznesowej.
FriendlyName — przyjazna nazwa używana podczas uzyskiwania dostępu do programu obsługi logiki biznesowej.
IsDotNetAssembly — wartość prawda.
Aby wdrożyć obsługę logiki biznesowej
- Wdroż asembler na serwerze, na którym działa Merge Agent, w lokalizacji pliku podanej podczas rejestracji handlera logiki biznesowej u Dystrybutora. W przypadku subskrypcji pull agent działa na Subskrybencie. W przypadku subskrypcji push agent działa na Dystrybutorze. Gdy używasz synchronizacji sieci, agent działa na serwerze WWW. Jeśli nie dołączysz pełnej ścieżki z nazwą asemblera podczas rejestracji handlera logiki biznesowej, wdroż asembler w tym samym katalogu co wykonywalny plik Merge Agent lub w tym samym katalogu co aplikacja, która synchronicznie uruchamia Merge Agent. Jeśli wiele aplikacji korzysta z tego samego asemblera, zainstaluj asembler w GAC.
Aby użyć procedury obsługi logiki biznesowej z nowym artykułem tabeli
Utwórz połączenie z programem Publisher przy użyciu klasy ServerConnection.
Utwórz wystąpienie klasy MergeArticle. Ustaw następujące właściwości:
Nazwa artykułu dla Name.
Nazwa publikacji dla PublicationName.
Nazwa bazy danych publikacji dla DatabaseName.
Przyjazna nazwa programu obsługi logiki biznesowej (FriendlyName) dla ArticleResolver.
Wywołaj metodę Create. Aby uzyskać więcej informacji, zobacz
Define an Article (Definiowanie artykułu).
Aby użyć mechanizmu obsługi logiki biznesowej z istniejącym artykułem tabeli
Utwórz połączenie z programem Publisher przy użyciu klasy ServerConnection.
Utwórz wystąpienie klasy MergeArticle.
Ustaw właściwości Name, PublicationNamei DatabaseName.
Ustaw połączenie z kroku 1 dla właściwości ConnectionContext.
Wywołaj metodę LoadProperties, aby uzyskać właściwości obiektu. Jeśli ta metoda zwróci fałszywe, albo źle zdefiniowałeś właściwości artykułu w kroku 3, albo artykuł nie istnieje. Aby uzyskać więcej informacji, zobacz View and Modify Article Properties.
Ustaw przyjazną nazwę programu obsługi logiki biznesowej dla ArticleResolver. Jest to właściwość FriendlyName, którą określasz podczas rejestrowania programu obsługi logiki biznesowej.
Przykłady (RMO)
W tym przykładzie przedstawiono procedurę obsługi logiki biznesowej, która rejestruje informacje o dodaniach, aktualizacjach i usunięciach po stronie subskrybenta.
using System;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.SqlServer.Replication.BusinessLogicSupport;
using Microsoft.Samples.SqlServer.BusinessLogicHandler;
namespace Microsoft.Samples.SqlServer.BusinessLogicHandler
{
public class OrderEntryBusinessLogicHandler :
Microsoft.SqlServer.Replication.BusinessLogicSupport.BusinessLogicModule
{
// Variables to hold server names.
private string publisherName;
private string subscriberName;
public OrderEntryBusinessLogicHandler()
{
}
// Implement the Initialize method to get publication
// and subscription information.
public override void Initialize(
string publisher,
string subscriber,
string distributor,
string publisherDB,
string subscriberDB,
string articleName)
{
// Set the Publisher and Subscriber names.
publisherName = publisher;
subscriberName = subscriber;
}
// Declare what types of row changes, conflicts, or errors to handle.
override public ChangeStates HandledChangeStates
{
get
{
// Handle Subscriber inserts, updates and deletes.
return ChangeStates.SubscriberInserts |
ChangeStates.SubscriberUpdates | ChangeStates.SubscriberDeletes;
}
}
public override ActionOnDataChange InsertHandler(SourceIdentifier insertSource,
DataSet insertedDataSet, ref DataSet customDataSet, ref int historyLogLevel,
ref string historyLogMessage)
{
if (insertSource == SourceIdentifier.SourceIsSubscriber)
{
// Build a line item in the audit message to log the Subscriber insert.
StringBuilder AuditMessage = new StringBuilder();
AuditMessage.Append(String.Format("A new order was entered at {0}. " +
"The SalesOrderID for the order is :", subscriberName));
AuditMessage.Append(insertedDataSet.Tables[0].Rows[0]["SalesOrderID"].ToString());
AuditMessage.Append("The order must be shipped by :");
AuditMessage.Append(insertedDataSet.Tables[0].Rows[0]["DueDate"].ToString());
// Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString();
// Set the history log level to the default verbose level.
historyLogLevel = 1;
// Accept the inserted data in the Subscriber's data set and
// apply it to the Publisher.
return ActionOnDataChange.AcceptData;
}
else
{
return base.InsertHandler(insertSource, insertedDataSet, ref customDataSet,
ref historyLogLevel, ref historyLogMessage);
}
}
public override ActionOnDataChange UpdateHandler(SourceIdentifier updateSource,
DataSet updatedDataSet, ref DataSet customDataSet, ref int historyLogLevel,
ref string historyLogMessage)
{
if (updateSource == SourceIdentifier.SourceIsPublisher)
{
// Build a line item in the audit message to log the Subscriber update.
StringBuilder AuditMessage = new StringBuilder();
AuditMessage.Append(String.Format("An existing order was updated at {0}. " +
"The SalesOrderID for the order is ", subscriberName));
AuditMessage.Append(updatedDataSet.Tables[0].Rows[0]["SalesOrderID"].ToString());
AuditMessage.Append("The order must now be shipped by :");
AuditMessage.Append(updatedDataSet.Tables[0].Rows[0]["DueDate"].ToString());
// Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString();
// Set the history log level to the default verbose level.
historyLogLevel = 1;
// Accept the updated data in the Subscriber's data set and apply it to the Publisher.
return ActionOnDataChange.AcceptData;
}
else
{
return base.UpdateHandler(updateSource, updatedDataSet,
ref customDataSet, ref historyLogLevel, ref historyLogMessage);
}
}
public override ActionOnDataDelete DeleteHandler(SourceIdentifier deleteSource,
DataSet deletedDataSet, ref int historyLogLevel, ref string historyLogMessage)
{
if (deleteSource == SourceIdentifier.SourceIsSubscriber)
{
// Build a line item in the audit message to log the Subscriber deletes.
// Note that the rowguid is the only information that is
// available in the dataset.
StringBuilder AuditMessage = new StringBuilder();
AuditMessage.Append(String.Format("An existing order was deleted at {0}. " +
"The rowguid for the order is ", subscriberName));
AuditMessage.Append(deletedDataSet.Tables[0].Rows[0]["rowguid"].ToString());
// Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString();
// Set the history log level to the default verbose level.
historyLogLevel = 1;
// Accept the delete and apply it to the Publisher.
return ActionOnDataDelete.AcceptDelete;
}
else
{
return base.DeleteHandler(deleteSource, deletedDataSet,
ref historyLogLevel, ref historyLogMessage);
}
}
}
}
Imports System
Imports System.Text
Imports System.Data
Imports System.Data.Common
Imports Microsoft.SqlServer.Replication.BusinessLogicSupport
Namespace Microsoft.Samples.SqlServer.BusinessLogicHandler
Public Class OrderEntryBusinessLogicHandler
Inherits BusinessLogicModule
' Variables to hold server names.
Private publisherName As String
Private subscriberName As String
' Implement the Initialize method to get publication
' and subscription information.
Public Overrides Sub Initialize( _
ByVal publisher As String, _
ByVal subscriber As String, _
ByVal distributor As String, _
ByVal publisherDB As String, _
ByVal subscriberDB As String, _
ByVal articleName As String _
)
' Set the Publisher and Subscriber names.
publisherName = publisher
subscriberName = subscriber
End Sub
' Declare what types of row changes, conflicts, or errors to handle.
Public Overrides ReadOnly Property HandledChangeStates() As ChangeStates
Get
' Handle Subscriber inserts, updates and deletes.
Return (ChangeStates.SubscriberInserts Or _
ChangeStates.SubscriberUpdates Or ChangeStates.SubscriberDeletes)
End Get
End Property
Public Overrides Function InsertHandler(ByVal insertSource As SourceIdentifier, _
ByVal insertedDataSet As DataSet, ByRef customDataSet As DataSet, _
ByRef historyLogLevel As Integer, ByRef historyLogMessage As String) _
As ActionOnDataChange
If insertSource = SourceIdentifier.SourceIsSubscriber Then
' Build a line item in the audit message to log the Subscriber insert.
Dim AuditMessage As StringBuilder = New StringBuilder()
AuditMessage.Append(String.Format("A new order was entered at {0}. " + _
"The SalesOrderID for the order is :", subscriberName))
AuditMessage.Append(insertedDataSet.Tables(0).Rows(0)("SalesOrderID").ToString())
AuditMessage.Append("The order must be shipped by :")
AuditMessage.Append(insertedDataSet.Tables(0).Rows(0)("DueDate").ToString())
' Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString()
' Set the history log level to the default verbose level.
historyLogLevel = 1
' Accept the inserted data in the Subscriber's data set and
' apply it to the Publisher.
Return ActionOnDataChange.AcceptData
Else
Return MyBase.InsertHandler(insertSource, insertedDataSet, customDataSet, _
historyLogLevel, historyLogMessage)
End If
End Function
Public Overrides Function UpdateHandler(ByVal updateSource As SourceIdentifier, _
ByVal updatedDataSet As DataSet, ByRef customDataSet As DataSet, _
ByRef historyLogLevel As Integer, ByRef historyLogMessage As String) _
As ActionOnDataChange
If updateSource = SourceIdentifier.SourceIsPublisher Then
' Build a line item in the audit message to log the Subscriber update.
Dim AuditMessage As StringBuilder = New StringBuilder()
AuditMessage.Append(String.Format("An existing order was updated at {0}. " + _
"The SalesOrderID for the order is ", subscriberName))
AuditMessage.Append(updatedDataSet.Tables(0).Rows(0)("SalesOrderID").ToString())
AuditMessage.Append("The order must now be shipped by :")
AuditMessage.Append(updatedDataSet.Tables(0).Rows(0)("DueDate").ToString())
' Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString()
' Set the history log level to the default verbose level.
historyLogLevel = 1
' Accept the updated data in the Subscriber's data set and apply it to the Publisher.
Return ActionOnDataChange.AcceptData
Else
Return MyBase.UpdateHandler(updateSource, updatedDataSet, _
customDataSet, historyLogLevel, historyLogMessage)
End If
End Function
Public Overrides Function DeleteHandler(ByVal deleteSource As SourceIdentifier, _
ByVal deletedDataSet As DataSet, ByRef historyLogLevel As Integer, _
ByRef historyLogMessage As String) As ActionOnDataDelete
If deleteSource = SourceIdentifier.SourceIsSubscriber Then
' Build a line item in the audit message to log the Subscriber deletes.
' Note that the rowguid is the only information that is
' available in the dataset.
Dim AuditMessage As StringBuilder = New StringBuilder()
AuditMessage.Append(String.Format("An existing order was deleted at {0}. " + _
"The rowguid for the order is ", subscriberName))
AuditMessage.Append(deletedDataSet.Tables(0).Rows(0)("rowguid").ToString())
' Set the reference parameter to write the line to the log file.
historyLogMessage = AuditMessage.ToString()
' Set the history log level to the default verbose level.
historyLogLevel = 1
' Accept the delete and apply it to the Publisher.
Return ActionOnDataDelete.AcceptDelete
Else
Return MyBase.DeleteHandler(deleteSource, deletedDataSet, _
historyLogLevel, historyLogMessage)
End If
End Function
End Class
End Namespace
W tym przykładzie zarejestrowano procedurę obsługi logiki biznesowej w dystrybutorze.
// Specify the Distributor name and business logic properties.
string distributorName = publisherInstance;
string assemblyName = @"C:\Program Files\Microsoft SQL Server\110\COM\CustomLogic.dll";
string className = "Microsoft.Samples.SqlServer.BusinessLogicHandler.OrderEntryBusinessLogicHandler";
string friendlyName = "OrderEntryLogic";
ReplicationServer distributor;
BusinessLogicHandler customLogic;
// Create a connection to the Distributor.
ServerConnection distributorConn = new ServerConnection(distributorName);
try
{
// Connect to the Distributor.
distributorConn.Connect();
// Set the Distributor properties.
distributor = new ReplicationServer(distributorConn);
// Set the business logic handler properties.
customLogic = new BusinessLogicHandler();
customLogic.DotNetAssemblyName = assemblyName;
customLogic.DotNetClassName = className;
customLogic.FriendlyName = friendlyName;
customLogic.IsDotNetAssembly = true;
Boolean isRegistered = false;
// Check if the business logic handler is already registered at the Distributor.
foreach (BusinessLogicHandler registeredLogic
in distributor.EnumBusinessLogicHandlers())
{
if (registeredLogic == customLogic)
{
isRegistered = true;
}
}
// Register the custom logic.
if (!isRegistered)
{
distributor.RegisterBusinessLogicHandler(customLogic);
}
}
catch (Exception ex)
{
// Do error handling here.
throw new ApplicationException(string.Format(
"The {0} assembly could not be registered.",
assemblyName), ex);
}
finally
{
distributorConn.Disconnect();
}
' Specify the Distributor name and business logic properties.
Dim distributorName As String = publisherInstance
Dim assemblyName As String = "C:\Program Files\Microsoft SQL Server\110\COM\CustomLogic.dll"
Dim className As String = "Microsoft.Samples.SqlServer.BusinessLogicHandler.OrderEntryBusinessLogicHandler"
Dim friendlyName As String = "OrderEntryLogic"
Dim distributor As ReplicationServer
Dim customLogic As BusinessLogicHandler
' Create a connection to the Distributor.
Dim distributorConn As ServerConnection = New ServerConnection(distributorName)
Try
' Connect to the Distributor.
distributorConn.Connect()
' Set the Distributor properties.
distributor = New ReplicationServer(distributorConn)
' Set the business logic handler properties.
customLogic = New BusinessLogicHandler()
customLogic.DotNetAssemblyName = assemblyName
customLogic.DotNetClassName = className
customLogic.FriendlyName = friendlyName
customLogic.IsDotNetAssembly = True
Dim isRegistered As Boolean = False
' Check if the business logic handler is already registered at the Distributor.
For Each registeredLogic As BusinessLogicHandler _
In distributor.EnumBusinessLogicHandlers
If registeredLogic Is customLogic Then
isRegistered = True
End If
Next
' Register the custom logic.
If Not isRegistered Then
distributor.RegisterBusinessLogicHandler(customLogic)
End If
Catch ex As Exception
' Do error handling here.
Throw New ApplicationException(String.Format( _
"The {0} assembly could not be registered.", _
assemblyName), ex)
Finally
distributorConn.Disconnect()
End Try
W tym przykładzie wprowadzono zmiany w istniejącym artykule, aby użyć obsługi logiki biznesowej.
// Define the Publisher, publication, and article names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks2022";
string articleName = "SalesOrderHeader";
// Set the friendly name of the business logic handler.
string customLogic = "OrderEntryLogic";
MergeArticle article = new MergeArticle();
// Create a connection to the Publisher.
ServerConnection conn = new ServerConnection(publisherName);
try
{
// Connect to the Publisher.
conn.Connect();
// Set the required properties for the article.
article.ConnectionContext = conn;
article.Name = articleName;
article.DatabaseName = publicationDbName;
article.PublicationName = publicationName;
// Load the article properties.
if (article.LoadProperties())
{
article.ArticleResolver = customLogic;
}
else
{
// Throw an exception of the article does not exist.
throw new ApplicationException(String.Format(
"{0} is not published in {1}", articleName, publicationName));
}
}
catch (Exception ex)
{
// Do error handling here and rollback the transaction.
throw new ApplicationException(String.Format(
"The business logic handler {0} could not be associated with " +
" the {1} article.",customLogic,articleName), ex);
}
finally
{
conn.Disconnect();
}
' Define the Publisher, publication, and article names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks2022"
Dim articleName As String = "SalesOrderHeader"
' Set the friendly name of the business logic handler.
Dim customLogic As String = "OrderEntryLogic"
Dim article As MergeArticle = New MergeArticle()
' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)
Try
' Connect to the Publisher.
conn.Connect()
' Set the required properties for the article.
article.ConnectionContext = conn
article.Name = articleName
article.DatabaseName = publicationDbName
article.PublicationName = publicationName
' Load the article properties.
If article.LoadProperties() Then
article.ArticleResolver = customLogic
Else
' Throw an exception of the article does not exist.
Throw New ApplicationException(String.Format( _
"{0} is not published in {1}", articleName, publicationName))
End If
Catch ex As Exception
' Do error handling here and rollback the transaction.
Throw New ApplicationException(String.Format( _
"The business logic handler {0} could not be associated with " + _
" the {1} article.", customLogic, articleName), ex)
Finally
conn.Disconnect()
End Try
Powiązana zawartość
- Zaimplementuj niestandardowy rozwiązywacz konfliktów dla artykułu Merge
- Debugowanie Procedury Obsługi Logiki Biznesowej (Programowanie Replikacji)
- Najlepsze rozwiązania dotyczące zabezpieczeń replikacji
- Pojęcia dotyczące obiektów zarządzania replikacją