Thursday, May 31, 2007

Disconnected Service Agent Application Block

add references to the following assemblies:
Microsoft.Practices.SmartClient.DisconnectedAgent
Microsoft.Practices.SmartClient.EnterpriseLibrary

Initializing the Request Manager
  1. Invoke any of the two overloads of the static method Initialize.

    // for default database that specified in the Data Access Application Block configuration.
    RequestManager requestManager = DatabaseRequestManagerIntializer.Initialize();

    OR

    RequestManager requestManager = DatabaseRequestManagerIntializer.Initialize("databaseName");


  2. To full control over the objects used to initialize the request manager, manually construct the objects and call the Initialize method

  1. EndpointCatalogFactory catalogFactory = new EndpointCatalogFactory("Endpoints");
    catalog = catalogFactory.CreateCatalog();

    SmartClientDatabase requestQueueDb = new
    SmartClientDatabase(ConfigurationManager.ConnectionStrings["QueueConnectionString"].ConnectionString);

    DatabaseRequestQueue requestQueue = new DatabaseRequestQueue(requestQueueDb, "RequestsQueue");

    SmartClientDatabase deadLetterQueueDb = new SmartClientDatabase(ConfigurationManager.ConnectionStrings["QueueConnectionString"].ConnectionString);

    DatabaseRequestQueue deadLetterQueue = new
    DatabaseRequestQueue(deadLetterQueueDb, "DeadLetterQueue");

    ConnectionMonitorAdapter adapter = new ConnectionMonitorAdapter(ConnectionMonitorFactory.CreateFromConfiguration());

    RequestManager manager = RequestManager.Instance;

    manager.Initialize(requestQueue, deadLetterQueue, adapter, catalog);

Creating a Simple Request
Request req = new Request();
req.MethodName = "DeliveryRouteUpdate";
req.Endpoint = "MyWebServiceHost";
specify the online proxy type for the request. The proxy class is the one that Visual Studio generates when you add a Web reference to your project.
req.OnlineProxyType = typeof(MyWebServiceProxy);
specify the behavior of the request
behavior.ProxyFactoryType = typeof(WebServiceProxyFactory);
other OfflineBehavior properties are Tag, MaxRetries, Stamps

Adding Parameters to a Request
int customerCode = 1234;
string comment = "New value for comment";
req.CallParameters = CallParameters.ToArray(customerCode, comment);
Handling Callbacks for a Request
req.Behavior.ReturnCallback
= new CommandCallback(typeof(MyDisconnectedServiceAgentCallbackClass),
"MyReturnCallbackMethod");

req.Behavior.ExceptionCallback
= new CommandCallback(typeof(MyDisconnectedServiceAgentCallbackClass),
"MyExceptionCallbackMethod");
method that takes references to the Request, an Object array for the parameters (arguments) you submitted to the service, and an Object for any value returned by the Web service.
public void MyReturnCallbackMethod(Request req, Object[] qParams,
Object result)
{
MessageBox.Show("Request succeeded. Return value is: "
+ result.ToString());
}


public OnExceptionAction MyExceptionCallbackMethod(Request req,
Exception ex)
{
MessageBox.Show("Your request failed with error: " + ex.Message);
return OnExceptionAction.Dismiss;
}



Adding a Request to a Queue
requestManager.RequestQueue.Enqueue(req);
Removing a Request from a Queue
requestManager.DeadLetterQueue.Remove(req)
Accessing Requests in a Queue
IRequestQueue theQueue = requestManager.RequestQueue;
// or:
IRequestQueue theQueue = requestManager.DeadLetterQueue;

// Count the number of requests in the queue.
Int32 queueCount = theQueue.GetCount();
// Get a reference to the next request in the queue.
Request nextRequest = theQueue.GetNextRequest();
// Get all the requests in the queue.
IEnumerable>Request> requests = theQueue.GetRequests();
// Get single request iterator by request ID.
Request requestById = theQueue.GetRequest(requestId);
// Get all the requests that have a specified Tag property value.
IEnumerable>Request> requestsByTag = theQueue.GetRequests("SalesOrder");
// Get all the requests that have a specified "stamps" value
// that is greater than or equal to a specified numerical value.
IEnumerable>Request> requestsByPrice = theQueue.GetRequests(5);

To remove a request from the queue, pass a reference to that request to the Remove method of the queue.
theQueue.Remove(req);
Dispatching a Single Request Immediately
requestManager.RequestQueue.Enqueue(req);
requestManager.DispatchRequest(req);

Dispatching All Pending Requests
requestManager.DispatchAllPendingRequests();
Dispatching Specific Pending Requests
requestManager.DispatchPendingRequestsByTag("Sales Order");
Starting and Stopping the Dispatcher Service
if (requestManager.Running)
{
reqManager.StopAutomaticDispatch();

OR

reqManager.StartAutomaticDispatch();
}




Conclustion

RequestManager requestManager = DatabaseRequestManagrerInitializer.Initialize();

Request req = new Request();
req.MethodName = "DeliveryRouteUpdate";
req.EndPoint = "MyWebServiceHost";
req.OnlineProxyType = typeof(MyWebServiceProxy);
behavior.ProxyFactoryType = typeof(WebServiceProxyFactory);

int customerCode =1234;
string comment = "New value for cooment";
req. Callparametars=CallParameters.ToArray(customerCode,Comment);

requestManager.RequestQueue.Enqueue(req);
requestManager.DispatchRequest(req);

No comments: