Conditionally execute code at run time.
example
An application that shows a view in a workspace only if the current user is associated with a specific role.
The action is the code that shows the view.
The condition is the code that determines the user's role.
Developers register an action catalog as a service.
Thursday, May 31, 2007
Endpoint Catalog Application Block
add a <section> node to the <configSections>
<configSections>
<section name="Endpoints" type="Microsoft.Practices.SmartClient.EndpointCatalog.Configuration.EndpointSection, Microsoft.Practices.SmartClient.EndpointCatalog" />
</configSections>
Create the application block configuration section
<Endpoints>
<EndpointItems>
<add Name="WebService1" Address="http://default-host.com/WebService1.asmx" UserName="default-user-name" Password="default-password" Domain="default-domain">
</add>
</EndpointItems>
</Endpoints>
create a new EndpointCatalog and add it to a WorkItem
Method 1:
Listing and Getting Information about Endpoints
To check whether an endpoint exists
To get information about an endpoint
<configSections>
<section name="Endpoints" type="Microsoft.Practices.SmartClient.EndpointCatalog.Configuration.EndpointSection, Microsoft.Practices.SmartClient.EndpointCatalog" />
</configSections>
Create the application block configuration section
<Endpoints>
<EndpointItems>
<add Name="WebService1" Address="http://default-host.com/WebService1.asmx" UserName="default-user-name" Password="default-password" Domain="default-domain">
</add>
</EndpointItems>
</Endpoints>
create a new EndpointCatalog and add it to a WorkItem
using Microsoft.Practices.SmartClient.EndpointCatalog;Getting an Endpoint Catalog Instance from a WorkItem
using System.Net;
// Specify the section name in the configuration file.
IEndpointCatalogFactory catalogFactory = new EndpointCatalogFactory("Endpoints");
IEndpointCatalog catalog = catalogFactory.CreateCatalog();
myWorkItem.Services.Add<IEndpointCatalog>(catalog);
Method 1:
public class MyNewClassMethod 2:
{
private EndpointCatalog catalog;
public MyNewClass([ServiceDependency] EndpointCatalog catalog)
{
this.catalog = catalog;
}
...
}
public class MyNewClassMethod 3:
{
private EndpointCatalog catalog;
[ServiceDependency]
public EndpointCatalog EndpointCatalog
{
get { return this.catalog; }
set { this.catalog = value; }
}
...
}
public class MyNewClass
{
private EndpointCatalog catalog;
public MyNewClass()
{
this.catalog = myWorkItem.Services.Get<EndpointCatalog>();
}
...
}
Listing and Getting Information about Endpoints
To check whether an endpoint exists
String epName = "MyWebServiceEndpoint";
if (catalog.EndpointExists(epName))
{
// Endpoint named MyWebServiceEndpoint does exist in the catalog.
}
To get information about an endpoint
// Get the number of endpoints in the catalog.
int endpointCount = catalog.Count;
// Get the address for an endpoint if it exists.
String epName = "MyWebServiceEndpoint";
String epNetworkName = "MyHost";
if (catalog.AddressExistsForEndpoint(epName, epNetworkName))
{
String epAddress
= catalog.GetAddressForEndpoint(epName, epNetworkName);
// Get the credentials for this endpoint.
NetworkCredential epCredentials
= catalog.GetCredentialForEndpoint(epName, epNetworkName);
String epUsername = epCredentials.UserName;
String epPassword = epCredentials.Password;
String epDomain = epCredentials.Domain;
}
Endpoint Catalog Application Block - Config
<Endpoints>
<EndpointItems>
<add Name="WebService1" Address="http://default-host.com/WebService1.asmx" UserName="default-user-name" Password="default-password" Domain="default-domain">
<add Name="WebService2" Address="http://default-host.com/WebService2.asmx" UserName="default-user-name" Password="default-password" Domain="default-domain">
</EndpointItems>
</Endpoints>
<EndpointItems>
<add Name="WebService1" Address="http://default-host.com/WebService1.asmx" UserName="default-user-name" Password="default-password" Domain="default-domain">
<NetworkItems></add>
<add Name="Internet" Address="http://internet-host.com/WebService1.asmx"
UserName="internet-user-name" Password="internet-password" />
<add Name="Work" Address="http://work-host.com/WebService1.asmx"
UserName="work-user-name" Password="work-password" />
</NetworkItems>
<add Name="WebService2" Address="http://default-host.com/WebService2.asmx" UserName="default-user-name" Password="default-password" Domain="default-domain">
<NetworkItems></add>
<add Name="Internet" Address="http://internet-host.com/WebService2.asmx"
UserName="internet-user-name" Password="internet-password" />
</NetworkItems>
</EndpointItems>
</Endpoints>
Disconnected Service Agent Application Block
add references to the following assemblies:
Initializing the Request Manager
Creating a Simple Request
Adding Parameters to a Request
Adding a Request to a Queue
To remove a request from the queue, pass a reference to that request to the Remove method of the queue.
Microsoft.Practices.SmartClient.DisconnectedAgent
Microsoft.Practices.SmartClient.EnterpriseLibrary
Initializing the Request Manager
- 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"); - To full control over the objects used to initialize the request manager, manually construct the objects and call the Initialize method
- 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();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.MethodName = "DeliveryRouteUpdate";
req.Endpoint = "MyWebServiceHost";
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;Handling Callbacks for a Request
string comment = "New value for comment";
req.CallParameters = CallParameters.ToArray(customerCode, comment);
req.Behavior.ReturnCallbackmethod 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.
= new CommandCallback(typeof(MyDisconnectedServiceAgentCallbackClass),
"MyReturnCallbackMethod");
req.Behavior.ExceptionCallback
= new CommandCallback(typeof(MyDisconnectedServiceAgentCallbackClass),
"MyExceptionCallbackMethod");
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);Dispatching All Pending Requests
requestManager.DispatchRequest(req);
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);
Wednesday, May 30, 2007
Connection Monitor Application Block
Configure the Connection Monitor Application Block
add a <section> node to the <configSections>
Add configuration information to the application configuration file.
It defines the logical networks and physical network adapters that you want to monitor.
Instantiate the ConnectionMonitor service and store it in a WorkItem
Call the static CreateFromConfiguration method of the ConnectionMonitorFactory class.
Add the ConnectionMonitor instance to the Services collection of your WorkItem
Get a reference to the Connection Monitor service from another class
Detect and handle changes to networks and connectivity
1. Connect your event handler to the StateChanged event of the Connection or Network
3. The Connection class exposes a method named UpdateStatus that you can call to raise the StateChanged event and obtain information through your event handler on the connectivity status
Creating Connections and Networks.
Create a new connection instance
add a <section> node to the <configSections>
<configSections>To use the Connection manager Application Block,
<section name="ConnectionMonitor" type="Microsoft.Practices.SmartClient.ConnectionMonitor.Configuration.ConnectionSettingsSection, Microsoft.Practices.SmartClient.ConnectionMonitor" />
</configSections>
Add configuration information to the application configuration file.
It defines the logical networks and physical network adapters that you want to monitor.
<ConnectionMonitor>
<Networks>
<add Name="Intranet" Address="http://contoso"/>
<add Name="Internet" Address="http://www.microsoft.com"/>
</Networks>
<Connections>
<add Type="WiredConnection" Price="1"/>
</Connections>
</ConnectionMonitor>
Instantiate the ConnectionMonitor service and store it in a WorkItem
Call the static CreateFromConfiguration method of the ConnectionMonitorFactory class.
// Use the default section name "ConnectionMonitor."
ConnectionMonitor sampleMonitor
= ConnectionMonitorFactory.CreateFromConfiguration();
// Instead, you can specify the section name in the configuration file.
String configurationName = "SampleConnectionSection";
ConnectionMonitor sampleMonitor
= ConnectionMonitorFactory.CreateFromConfiguration(configurationName);
Add the ConnectionMonitor instance to the Services collection of your WorkItem
myWorkItem.Services.Add(sampleMonitor);
if (sampleMonitor.Connections.Contains("Internet"))
{
// Check the connection status.
Connection conn = sampleMonitor.Connections["Internet"];
if (!conn.IsConnected)
{
}
Get a reference to the Connection Monitor service from another class
- use a [ServiceDependency] attribute on a parameter of the class constructor to accept an injected reference
public class MyNewClass
{
private ConnectionMonitor sampleMonitor;
public MyNewClass([ServiceDependency] ConnectionMonitor cm)
{
this.sampleMonitor = cm;
}
...
} - Expose a public property for the ConnectionMonitor
public class MyNewClass
{
private ConnectionMonitor sampleMonitor;
[ServiceDependency]
public ConnectionMonitor ConnectionMonitor
{
get { return this.sampleMonitor; }
set { this.sampleMonitor = value; }
}
...
} - Query the Services collection of the WorkItem to obtain a reference to the service
public class MyNewClass
{
private ConnectionMonitor sampleMonitor;
public MyNewClass();
{
this.sampleMonitor = myWorkItem.Services.Get<ConnectionMonitor>();
}
...
}
Detect and handle changes to networks and connectivity
1. Connect your event handler to the StateChanged event of the Connection or Network
conn.StateChanged += StateChangeHandler;2. Network and the Connection classes both raise a StateChanged event when their state changes.
netwk.StateChanged += StateChangeHandler;
public void StateChangeHandler(object sender, StateChangedEventArgs e)
{
if (e.IsConnected)
{ MessageBox.Show("Now connected"); }
}
3. The Connection class exposes a method named UpdateStatus that you can call to raise the StateChanged event and obtain information through your event handler on the connectivity status
conn.UpdateStatus();
Creating Connections and Networks.
Create a new connection instance
// Create a new NicConnection instance.Use the Add method of the ConnectionCollection class
NicConnection conn = new NicConnection("NicConnection", 6);
// Add it to the ConnectionMonitor Connections collection.It can also use the other methods of the KeyedCollection and Collection classes to check for the presence of a specific connection
sampleMonitor.Connections.Add(conn);
// See if a connection named Internet exists.
if (sampleMonitor.Connections.Contains("Internet"))
{
// Check the connection status.
Connection conn = sampleMonitor.Connections["Internet"];
if (!conn.IsConnected)
{
// Display the price and remove it from the Connections collection.
MessageBox.Show("Removing connection with price "
+ conn.Price.ToString());
sampleMonitor.Connections.Remove(conn);
}
}
Tuesday, May 29, 2007
Stage 4: Creating and Showing the SmartPart
create an interface for the view and the presenter to communicate
Create a Interface named IMyView.cs in the MyModule project and change as .
Create and show the view
Create a Interface named IMyView.cs in the MyModule project and change as .
public interface IMyViewCreate a SmartPart user control
{
event EventHandler Load;
string Message { get; set; }
}
- add a User Control named MyView.cs
- Drag a Label control.
- Implement the IMyView interface
- add IMyView interface t othe class MyView
public partial class MyView : UserControl, IMyView
- right-click IMyView and click Implement Interface, then click Implement Interface in the fly-out menu.
- modify property for Message in Interface.
public string Massage
{
get { return this.label1.Text;}
set { this.label1.Text = value;}
}
- Create a class named MyPresenter.cs
- add a variable of type IMyView
public class MyPresenter
{
IMyView view; - Create a contructor for the class
public MyPresenter(IMyView view)
{
this.view = view;
view.Load += new EventHandler(view_Load);
} - Create a event handler for the Load event
get a reference to the WorkItemvoid view_Load(object sender, EventArgs e)
{
view.Message = "Hello World from a Module";
}
- Open the MyModuleInit.cs add the following after the myCatalogService declaration.
The variable will contain a reference to the root ShellWorkItemprivate WorkItem parentWorkItem;
[ServiceDependency]
public WorkItem ParentWorkItem
{
set { parentWorkItem = value; }
} - Modify the load method.
public override void Load()
{
base.Load();
MyWorkItem myWorkItem =
parentWorkItem.WorkItems.AddNew<MyWorkItem>();
myWorkItem.Run(parentWorkItem.Workspaces["tabWorkspace1"]);
}
Create and show the view
- In MyWorkItem.cs, add
using Microsoft.Practices.CompositeUI.SmartParts;
- Create a public Run method that accepts as a parameter a reference to the TabWorkspace.
public void Run(IWorkspace TabWorkspace)
{
IMyView view = this.Items.AddNew<MyView>();
MyPresenter presenter = new MyPresenter(view);
this.Items.Add(presenter);
TabWorkspace.Show(view);
}
Stage 3: Adding the TabWorkspace
- Drag a SplitContainer control onto the form ShellForm.cs .
- Right-click the Toolbox, click Items..., and then click Browse.....
In the \Src\CS\CompositeUI.WinForms\bin\Debug\ subfolder of the folder where you installed the CAB files, select the file Microsoft.Practices.CompositeUI.WinForms.dll. - Drag a TabWorkspace onto the form and drop it onto the left-hand panel of the SplitContainer control.
Stage 2: Creating and Loading the Module
- Add new Class Library project named MyModule to the existing solution
- In Properties->Build->Output select \bin\Debug folder of ShellApplication project
- Save and reopen then, Properties->Outputpath to ..\ShellApplication\bin\Debug
- Add Reference
Microsoft.Practices.CompositeUI.dll
Microsoft.Practices.CompositeUI.WinForms.dll
Microsoft.Practices.ObjectBuilder.dll - Create a class named MyWorkItem.cs derived from WorItem and include namespace
using Microsoft.Practices.CompositeUI;
public class MyWorkItem : WorkItem
{
} - Add a module Initialiser
- add class named MyModuleInit.cs derived from ModuleInit and include namespaces
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Services;
using System.Windows.Forms;
public class MyModuleInit : ModuleInit
{
} - Add the following variable to reference the WorkItemTypeCatalogService so that you can access it to register your WorkItem:
private IWorkItemTypeCatalogService myCatalogService;
[ServiceDependency]
public IWorkItemTypeCatalogService myWorkItemCatalog
{
set { myCatalogService = value; }
} - Override the Load method of the ModuleInit class so that it registers your module's WorkItem.
public override void Load()
{
base.Load();
myCatalogService.RegisterWorkItem<MyWorkItem>();
} - instruct the CAB to load the new module in ProfileCatalog.xml.
<?xml version="1.0" encoding="utf-8" ?>
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile">
<Modules>
<ModuleInfo AssemblyFile="MyModule.dll" />
</Modules>
</SolutionProfile>
right-click the file ProfileCatalog.xml and click Properties. Change the Copy To Output Directory property to Copy Always.
Stage 1: Creating the Shell and the Form
- Create a new Windows Forms application named ShellApplication
- rename Form1.cs to ShellForm.cs
- Add Reference
Microsoft.Practices.CompositeUI.dll
Microsoft.Practices.CompositeUI.WinForms.dll
Microsoft.Practices.ObjectBuilder.dll - create a custom workitem
- create a new class named ShellWorkItem.cs
- Inherit class from WorkItem and include namespace
using Microsoft.Practices.CompositeUI;
public class ShellWorkItem : WorkItem
{
} - modification in program.cs
- rename Program.cs to ShellApplication.cs
- include namespace
using Microsoft.Practices.CompositeUI.WinForms;
- Replace the static class ShellApplication with a public class that inherits from FormShellApplication
public class ShellApplication :
FormShellApplication<shellworkitem,shellform>
{
} [STAThread]
static void Main()
{
new ShellApplication().Run();
}
Basic Commands...
Creating a shell.
override the AfterShellCreated method
display a SmartPart in a Workspace
WorkItems.
create a workitem
Invoke a WorkItem
Inject state into child SmartParts in a child WorkItem
steps1: In the parent WorkItem, set the state so that adding a child WorkItem to the container injects the state into it
// in child WorkItem
UIElements.
Commands.
Services.
add a service by specifying it in the shell application configuration file
to add a service programmatically
1. to use an existing service instance already created.
2. to create a new instance of a service
Register a class as a service using attribute.
declare a class to be registered as a lazy-loaded service.
Module.
steps1 : Create a module
steps2 : Create a module initializer
steps3 : add dependencies to the module
steps4 : load a module
Create a module
Create a module Initializer
put following in either in the ModuleInit or AssemblyInfo.cs
creating reference to services
programmatically
creating Custom Services
steps1: add a new interface to an appropriate module.
steps2: add members that the interface will define.
steps3: add a new class to the module or the shell.
steps4: inherit from the interface and add required functionality to the members.
steps5: add the attribute like following
SmartParts.
steps1: add a user control to project
steps2: add a reference to the Microsoft.Practices.CompositeUI.SmartParts
steps3: add the attribute as following
run an event on a background thread
public class MyApplication : FormShellApplication<WorkItem, MyShellForm>
{
}
[STAThread]
public static void Main()
{
new MyApplication.Run();
}
override the AfterShellCreated method
protected override void AfterShellCreated()
{
base.AfterShellCreated();
... your start-up code here ...
}
display a SmartPart in a Workspace
Form1 mainForm = new Form1();
CustomerSmartPart sp = myWorkItem.Items.AddNew<CustomerSmartPart>();
mainForm.deckWorkspace1.Show(sp);
WorkItems.
create a workitem
- create a class inherit from Microsoft.Practices.CompositeUI.WorkItem.
- override the OnRunStarted method. In this method, add code to perform an initialization required and to display the appropriate view.
protected override void OnRunStarted()
{
base.OnRunStarted();
SummaryView view = this.Items.AddNew<SummaryView>();
workspace.Show(view);
}
Invoke a WorkItem
myWorkItem.Run();
Inject state into child SmartParts in a child WorkItem
steps1: In the parent WorkItem, set the state so that adding a child WorkItem to the container injects the state into it
public void ShowCustomerDetails(Customer custmr)steps2: In the child WorkItem, use the State attribute to indicate that a parent WorkItem should inject the property into the child WorkItem.
{
// set state for injection into child WorkItem
State["Customer"] = custmr;
ChildWorkItem myChild = this.WorkItems.AddNew<ChildWorkItem>();
myChild.Run();
}
// in child WorkItem
[State("Customer")]
public Customer TheCustomer
{
get { return (Customer)State["Customer"]; }
set { State["Customer"] = value; }
}
UIElements.
RootWorkItem.UIExtensionSites.RegisterSite("MainMenu", Shell.MainMenuStrip);
ToolStripMenuItem item = null;
item = new ToolStripMenuItem("Name");
MyWorkItem.UIExtensionSites["MainMenu"].Add(item);
Commands.
[CommandHandler("ShowName")]associate a Command with a UIElement
public void ShowName(object sender, EventArgs e)
{
MessageBox.Show("My name is Joe");
}
MyWorkItem.Commands["ShowName"].AddInvoker(item, "Click");
Services.
add a service by specifying it in the shell application configuration file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CompositeUI"
type="Microsoft.Practices.CompositeUI.Configuration.SettingsSection,
Microsoft.Practices.CompositeUI"
allowExeDefinition="MachineToLocalUser" />
</configSections>
<CompositeUI>
<services>
<!-- Other services -->
<add serviceType="MyApp.Services.IMyService, MyApp"
instanceType="MyApp.Services.MyService, MyApp"/>
</services>
</CompositeUI>
</configuration>
to add a service programmatically
1. to use an existing service instance already created.
RootWorkItem.Services.Add<CustomerService>(myServiceInstance);
2. to create a new instance of a service
RootWorkItem.Services.AddNew<CustomerService>();
Register a class as a service using attribute.
[Service(typeof(IMyService))]Services that do not provide different implementations may not implement an interface
public class MyService : IMyService
{
}
[Service]
public class MyService
{
}
declare a class to be registered as a lazy-loaded service.
[Service(typeof(IMyService), AddOnDemand=true)]
public class MyService : IMyService
{
}
Module.
steps1 : Create a module
steps2 : Create a module initializer
steps3 : add dependencies to the module
steps4 : load a module
Create a module
- create a new class library or Windows control library project
- add reference to Microsoft.Practices.CompositeUI and Microsoft.Practices.ObjectBuilder
- add a module attribute to identify this as a module.
- [assembly: Microsoft.Practices.CompositeUI.Module("mainMod")] in AssemblyInfo.cs
Create a module Initializer
- create a new public class
- inherit from Microsoft.Practices.CompositeUI.ModuleInit class.
- you can override the AddServices method
- you can override the Load method
put following in either in the ModuleInit or AssemblyInfo.cs
[assembly: Microsoft.Practices.CompositeUI.ModuleDependency(load a module
"TheModuleYouDependOn")]
<?xml version="1.0" encoding="utf-8" ?>
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile">
<Modules>
<ModuleInfo AssemblyFile="Mod1.dll"/>
<ModuleInfo AssemblyFile="Mod2.dll"/>
</Modules>
</SolutionProfile>
creating reference to services
programmatically
IMyService myServ = (IMyService)myWorkItem.Services.Get(typeof(IMyService));declaratively
// or using generics
IMyService myServ = myWorkItem.Services.Get<IMyService>();
private IMyService service;
[ServiceDependency]
public IMyService MyService
{
set { service = value; }
}
creating Custom Services
steps1: add a new interface to an appropriate module.
steps2: add members that the interface will define.
steps3: add a new class to the module or the shell.
steps4: inherit from the interface and add required functionality to the members.
steps5: add the attribute like following
[Service(Type=typeof(IMyService))]
public class MyService : IMyService
SmartParts.
steps1: add a user control to project
steps2: add a reference to the Microsoft.Practices.CompositeUI.SmartParts
steps3: add the attribute as following
[SmartPart]display a SmartPart in a Workspace
public partial class MySmartPart : UserControl
public class MyWorkItem : WorkItemimplement the MVC pattern
{
protected override void OnRunStarted()
{
base.OnRunStarted();
CustomerSmartPart csp = this.SmartParts.AddNew<CustomerSmartPart>();
Workspaces["tabbedArea"].Show(csp);
}
}
protected override void OnRunStarted()Publishing Events
{
base.OnRunStarted();
SampleView view = this.Items.AddNew<SampleView>();
workspace.Show(view);
}
[EventPublication("event://UpdatesAvailable", PublicationScope.Global)]Subscribing to Events
public event SomeEventHandler UpdatesAvailable;
[EventSubscription("event://UpdatesAvailable")]
public void NewUpdates(object sender, SomeEventArgs numUpdates)
{
MessageBox.Show(numUpdates.ToString(), "Updates available");
}
run an event on a background thread
[EventSubscription("event://UpdatesAvailable",
Thread=ThreadOption.Background)]
Monday, May 28, 2007
Basic Commands
UI Elements.
adding services to a workitem. ie creates an instance
EVents.
publishing
Module.
Module Dependencies
Registering a service
Locating a Service.
RootWorkItem.UIExtensionSites.RegisterSite(“FileMenu”, Shell.MainMenuStrip);WorkItems.
ToolStripMenuItem printItem = new ToolStripMenuItem("Print");
RootWorkItem.UIExtensionSites[“FileMenu”].Add(printItem);
adding services to a workitem. ie creates an instance
WorkItem.Services.AddNew<CustomerFinderService, ICustomerFinderService>();creates an instance of the OfficerView class
WorkItem.SmartParts.AddNew<OfficerView>();get another component in a workitem
ICustomerFinderService customerFinderServcie =
WorkItem.Services.Get<ICustomerFinderService>();
EVents.
publishing
[EventPublication("topic://UpdatesAvailable", PublicationScope.Global)]subscription
public event EventHandler<DataEventArgs<UpdateData>> UpdatesAvailable;
// or
[EventPublication("topic://UpdatesAvailable", PublicationScope.Global)]
public event EventHandler UpdatesAvailable;
[EventSubscription("topic://UpdatesAvailable")]
public void SomethingHappened(object sender, DataEventArgs<UpdateData> e)
{ ... }
Module.
Module Dependencies
[assembly: ModuleDependency("BranchSystems.Module")]Loading Modules- default file is ProfileCatalog.xml
services.<?xml version="1.0" encoding="utf-8" ?>
<SolutionProfile xmlns="http://schemas.microsoft.com/pag/cab-profile" >
<Modules>
<ModuleInfo
AssemblyFile="GlobalBank.AppraiserWorkbench.AppraiserWorkbenchModule.dll" /></Modules></SolutionProfile>
Registering a service
[Service(typeof(IMyService))]
public class MyService : IMyService
{
}
Locating a Service.
private IMyService service;
[ServiceDependency]
public IMyService MyService
{
set { service = value; }
}
Thursday, May 24, 2007
Object Builder.
create objects and perform dependency injection.
It manages the processes performed on objects during construction and disposal.
attributes - [CreateNew] and [ServiceDependency]
CreateNew - This attribute tells the dependency injection system to always create a new one of whatever it is you need.
Dependency - This is a general-purpose attribute with four optional parameters: Name, NotPresentBehavior, Createype, SearchMode
methods - BuildUp and TearDown.
BuildUp(locator, type, id, instance, policies[] );
BuildUp(locator, id, instance, policies[] );
Creating New Objects.
When the property is set, the View property of the presenter is used to connect this implementation of the INewTransferView interface to the presenter.
Locating a Service.
You can add the ServiceDependency attribute to a property.
The property specifies the type of service or interface you require.
When this attribute is present, ObjectBuilder locates an instance of the service and passes back a reference to it.
To locate the service, ObjectBuilder first looks in the current WorkItem. If the service is not found, ObjectBuilder then looks at the services in the parent WorkItem.
To programmatically register a service, call the Add method or AddNew method of the Services collection of the WorkItem within which you want to use the service.
Registering a Constructor.
A class can contain more than one constructor. ObjectBuilder first looks for any constructor decorated with the [InjectionConstructor] attribute.
It manages the processes performed on objects during construction and disposal.
attributes - [CreateNew] and [ServiceDependency]
CreateNew - This attribute tells the dependency injection system to always create a new one of whatever it is you need.
Dependency - This is a general-purpose attribute with four optional parameters: Name, NotPresentBehavior, Createype, SearchMode
methods - BuildUp and TearDown.
BuildUp(locator, type, id, instance, policies[] );
BuildUp
Creating New Objects.
[CreateNew]The CreateNew attribute instructs ObjectBuilder to instantiate and initialize an instance of a NewTransferViewPresenter when the NewTransferView is created.
public NewTransferViewPresenter Presenter
{
get { return _presenter; }
set
{
_presenter = value;
_presenter.View = this;
}
}
When the property is set, the View property of the presenter is used to connect this implementation of the INewTransferView interface to the presenter.
Locating a Service.
You can add the ServiceDependency attribute to a property.
The property specifies the type of service or interface you require.
When this attribute is present, ObjectBuilder locates an instance of the service and passes back a reference to it.
To locate the service, ObjectBuilder first looks in the current WorkItem. If the service is not found, ObjectBuilder then looks at the services in the parent WorkItem.
private IAccountServices _accountServices;Frequently, the ServiceDependency attribute is used for the arguments in a constructor. This means that ObjectBuilder will instantiate the required services when it creates the dependent object.
[ServiceDependency]
public IAccountServices AccountServices
{
set { _accountServices = value; }
}
public class ElectronicFundsTransferControllerRegistering a Service.
{
private IAccountServices _accountServices;
public ElectronicFundsTransferController( [ServiceDependency] IAccountServices accountServices )
{
_accountServices = accountServices;
}
...
}
To programmatically register a service, call the Add method or AddNew method of the Services collection of the WorkItem within which you want to use the service.
moduleWorkItem.Services.AddNew<AccountServices, IAccountServices>();
Registering a Constructor.
A class can contain more than one constructor. ObjectBuilder first looks for any constructor decorated with the [InjectionConstructor] attribute.
public class CustomersListViewPresenter
{
private CustomersController _controller;
[InjectionConstructor]
public CustomersListViewPresenter ( [ServiceDependency] CustomersController controller )
{ _controller = controller;
}
...
}
Tuesday, May 22, 2007
The Disconnected Service Agent Application Block
Provides management features for executing Web services from occasionally connected smart client applications.
Application can maintain a queue of Web service requests when offline (disconnected) and then replay them when a connection to the server application becomes available.
RequestManager
class manages the request queues and uses the services of the RequestDispatcher class to dispatch these requests. stores the queues of pending and failed requests in a SQL Server 2005 Compact Edition database. It takes the messages from the queue and dispatches them when the application is online. Requests remain in the database until successful submission to the remote server or until the request expires.
Request
class is the store for a Web service request, including the arguments or parameters required by the Web service method.
ConnectionMonitorAdapter
class provides information about the connection used by the request, such as the price and network details, and it raises events when status of the connection changes.
EndpointCatalog
class that contains the endpoints to use for the request. Requests to a remote service require details of the endpoint, such as the network type, credentials, and URL.
OfflineBehavior
class exposes properties that provide information about the request, such as the date and time it was queued. You also use this class to specify features of the request, such as the expiration, maximum number of retries, the number of "stamps", and the Tag value.
Application can maintain a queue of Web service requests when offline (disconnected) and then replay them when a connection to the server application becomes available.
RequestManager
class manages the request queues and uses the services of the RequestDispatcher class to dispatch these requests. stores the queues of pending and failed requests in a SQL Server 2005 Compact Edition database. It takes the messages from the queue and dispatches them when the application is online. Requests remain in the database until successful submission to the remote server or until the request expires.
Request
class is the store for a Web service request, including the arguments or parameters required by the Web service method.
ConnectionMonitorAdapter
class provides information about the connection used by the request, such as the price and network details, and it raises events when status of the connection changes.
EndpointCatalog
class that contains the endpoints to use for the request. Requests to a remote service require details of the endpoint, such as the network type, credentials, and URL.
OfflineBehavior
class exposes properties that provide information about the request, such as the date and time it was queued. You also use this class to specify features of the request, such as the expiration, maximum number of retries, the number of "stamps", and the Tag value.
Creating Connections and Networks
To create a new connection instance, call the constructor of the appropriate concrete class and specify the name and the price for this connection.
You can also use the other methods of the KeyedCollection and Collection classes (from which the ConnectionCollection class inherits) to check for the presence of a specific connection, get a reference to it, and remove it from the ConnectionCollection.
// Create a new NicConnection instance.Use the Add method of the ConnectionCollection class
NicConnection conn = new NicConnection("NicConnection", 6);
// Add it to the ConnectionMonitor Connections collection.
sampleMonitor.Connections.Add(conn);
You can also use the other methods of the KeyedCollection and Collection classes (from which the ConnectionCollection class inherits) to check for the presence of a specific connection, get a reference to it, and remove it from the ConnectionCollection.
The same principles apply to the Network and NetworkCollection classes. You create a new Network instance by specifying the name in a call to the constructor.
// See if a connection named Internet exists.
if (sampleMonitor.Connections.Contains("Internet"))
{
// Check the connection status.
Connection conn = sampleMonitor.Connections["Internet"];
if (!conn.IsConnected)
{
// Display the price and remove it from the Connections collection.
MessageBox.Show("Removing connection with price "
+ conn.Price.ToString());
sampleMonitor.Connections.Remove(conn);
}
}
Network network = new Network("Intranet", "http://intranet");Then you add it to the NetworkCollection using the Add method.
sampleMonitor.Networks.Add(network);You also use the same methods and properties of the underlying KeyedCollection and Collection classes to manipulate the collection of Network instances.
// See if a network named Internet exists.
if (sampleMonitor.Networks.Contains("Intranet"))
{
// Check connection status.
Network netwk = sampleMonitor.Networks["Intranet"];
if (!netwk.Connected)
{
// Remove the network from the Networks collection.
sampleMonitor.Networks.Remove(netwk);
}
}
Monday, May 21, 2007
Handling Connectivity Change Events
detect and handle changes to networks and connectivity
The Network and the Connection classes both raise a StateChanged event when their state changes. Create a handler for this event that accepts an instance of the StateChangedEventArgs class, which exposes the IsConnected property
Connect your event handler to the StateChanged event of the Connection or Network you want to monitor.
The Network and the Connection classes both raise a StateChanged event when their state changes. Create a handler for this event that accepts an instance of the StateChangedEventArgs class, which exposes the IsConnected property
public void StateChangeHandler(object sender, StateChangedEventArgs e)
{
if (e.IsConnected)
{
MessageBox.Show("Now connected");
}
else
{
MessageBox.Show("Now disconnected");
}
}
Connect your event handler to the StateChanged event of the Connection or Network you want to monitor.
conn.StateChanged += StateChangeHandler;The Connection class exposes a method named UpdateStatus that you can call to raise the StateChanged event and obtain information through your event handler on the connectivity status.
netwk.StateChanged += StateChangeHandler;
conn.UpdateStatus();
Listing the Configured Networks
You can obtain a list of the configured networks from the Networks property of the ConnectionMonitor.
NetworkCollection netList = sampleMonitor.Networks;The NetworkCollection class inherits from the generic collection class KeyedCollection, and implements the IEnumerable interface.
StringBuilder builder = new StringBuilder();To obtain a list of the connected networks only, use the ActiveNetworks property of the NetworkCollection class.
foreach (Network netwk in netList)
{
builder.Append(String.Format("Name: {0}, Connected: {1}",
netwk.Name, netwk.Connected.ToString()));
builder.Append("\n");
}
ReadOnlyCollection<network> activeNetworks = sampleMonitor.Networks.ActiveNetworks;
Get a list of the connections configured in an application
<Connections>You can obtain a list of the configured connections from the Connections property of the ConnectionMonitor.
<add Type="WiredConnection" Price="1" />
<add Type="WirelessConnection" Price="2" />
</Connections>
ConnectionCollection connList = sampleMonitor.Connections;The ConnectionCollection class inherits from the generic collection class KeyedCollection and implements the IEnumerable interface.
StringBuilder builder = new StringBuilder();
foreach (Connection conn in connList)
{
builder.Append(String.Format("Type: {0}, Price: {1}, "
+ "IsConnected: {2}",
conn.ConnectionTypeName,
conn.Price.ToString(),
conn.IsConnected.ToString()));
builder.Append("\n");
}
Referencing the Connection Monitor Service
Get a reference to the Connection Monitor service from another class
If you have stored an instance of the ConnectionMonitor class in your WorkItem, you can reference it from any other class within the scope of that WorkItem. You can use a [ServiceDependency] attribute on a parameter of the class constructor to accept an injected reference and store it in a class-level variable.
If you have stored an instance of the ConnectionMonitor class in your WorkItem, you can reference it from any other class within the scope of that WorkItem. You can use a [ServiceDependency] attribute on a parameter of the class constructor to accept an injected reference and store it in a class-level variable.
public class MyNewClassAlternatively, you can expose a public property for the ConnectionMonitor and have ObjectBuilder set it to the ConnectionMonitor instance in the WorkItem.
{
private ConnectionMonitor sampleMonitor;
public MyNewClass([ServiceDependency] ConnectionMonitor cm)
{
this.sampleMonitor = cm;
}
...
}
public class MyNewClassAnother approach is to directly query the Services collection of the WorkItem to obtain a reference to the service you want.
{
private ConnectionMonitor sampleMonitor;
[ServiceDependency]
public ConnectionMonitor ConnectionMonitor
{
get { return this.sampleMonitor; }
set { this.sampleMonitor = value; }
}
...
}
public class MyNewClass
{
private ConnectionMonitor sampleMonitor;
public MyNewClass();
{
this.sampleMonitor = myWorkItem.Services.Get();
}
...
}
Creating a Connection Monitor Instance
Instantiate the ConnectionMonitor service and store it in a WorkItem
Call the static CreateFromConfiguration method of the ConnectionMonitorFactory class. You can specify the name of the section in the application's configuration file that contains the definitions of the connections and networks configured for the application, or you can omit this parameter to use the default section name "ConnectionMonitor."
Call the static CreateFromConfiguration method of the ConnectionMonitorFactory class. You can specify the name of the section in the application's configuration file that contains the definitions of the connections and networks configured for the application, or you can omit this parameter to use the default section name "ConnectionMonitor."
If you are building a Composite UI Application Block application, you can add the ConnectionMonitor instance to the Services collection of your WorkItem so that it is available by using ObjectBuilder injection techniques.
// Use the default section name "ConnectionMonitor."
ConnectionMonitor sampleMonitor
= ConnectionMonitorFactory.CreateFromConfiguration();
// Instead, you can specify the section name in the configuration file.
String configurationName = "SampleConnectionSection";
ConnectionMonitor sampleMonitor
= ConnectionMonitorFactory.CreateFromConfiguration(configurationName);
myWorkItem.Services.Add(sampleMonitor);
Configuring the Connection Monitor Application Block
This configuration information defines the logical networks and physical network adapters that you want to monitor
Define a configuration section for the application block in the application configuration file. To do this, add a <section> node to the <configSections> node of your App.config file
node using an element.
<ConnectionMonitor>Following values can use for the Type attribute
<Networks>
<add Name="Intranet" Address="http://contoso"/>
<add Name="Internet" Address="http://www.microsoft.com"/>
</Networks>
<Connections>
<add Type="WiredConnection" Price="1"/>
</Connections>
</ConnectionMonitor>
- NicConnection. The application block uses the NicConnection class to monitor any network interface connection—for example, an Ethernet card or wireless adapter. This class provides the capability to detect if the application has a connection to a network.
- WiredConnection. The application block uses the WiredConnection class to monitor connectivity to Ethernet adapters.
- WirelessConnection. The application block uses the WirelessConnection class to monitor connectivity to wireless network adapters.
- DesktopConnection. The application block uses the DesktopConnection class to monitor connectivity to local services (you can manually set the connection state of this class).
Define a configuration section for the application block in the application configuration file. To do this, add a <section> node to the <configSections> node of your App.config file
<configSections>Create the application block configuration section and define your logical networks. Configure each logical network inside a
<section name="ConnectionMonitor" type="Microsoft.Practices.SmartClient.ConnectionMonitor.Configuration.ConnectionSettingsSection, Microsoft.Practices.SmartClient.ConnectionMonitor" />
</configSections>
<ConnectionMonitor>If you want to monitor physical network adapters, add the configuration information for the network adapters inside a <connections> element.
<Networks>
<add Name="Intranet" Address="http://intranet"/>
<add Name="Internet" Address="http://www.microsoft.com"/>
</Networks>
</ConnectionMonitor>
<ConnectionMonitor>Optionally, specify the type name of the network status strategy to use to monitor the connection status of the logical network.
<Connections>
<add Type="WiredConnection" Price="1"/>
<add Type="WirelessConnection" Price="2"/>
</Connections>
</ConnectionMonitor>
<ConnectionMonitor>
<Networks StrategyType="ConnectionModule.CustomNetworkStatusStrategy, ConnectionModule">
<add Name="Intranet" Address="http://intranet"/>
<add Name="Internet" Address="http://www.microsoft.com"/>
</Networks>
</ConnectionMonitor>
Subscribe to:
Posts (Atom)