does it need to release MAPIOBJECT in outlook object model? - outlook-redemption

here i am running separate task using redemption inside a outlook plugin.
Does mapi MAPIOBJECT is com object and does it needs to release as following ?
what happen if i release mapi object ?
public async Task SimpleTask(string entryId)
{
RDOMail rdoMail = null;
RDOSession rdoSession = null;
try
{
var outlookApp = addinModule.OutlookApp;
var outlookNamespace = outlookApp.GetNamespace("MAPI");
var mapiObject = outlookNamespace.MAPIOBJECT;
rdoSession = RedemptionLoader.new_RDOSession();
if (rdoSession != null)
{
rdoSession.MAPIOBJECT = mapiObject;
bool loggedOn = rdoSession.LoggedOn;
if(!string.IsNullOrEmpty(entryId))
{
rdoMail = rdoSession.GetMessageFromID(entryId);
//...;
rdoMail.Save();
}
}
}
catch (System.Exception ex)
{
}
finally
{
rdoMail.ReleaseComObject()
mapiObject.ReleaseComObject();
rdoSession.ReleaseComObject();
outlookNamespace.ReleaseComObject();
}
}

Yes, it is a COM object. OOM represents it as IUnknown. It is IMAPISession (derived from IUnknown).

Related

Dapper Dynamic Parameter wirth optional return value asp.net mvc

Hello I have a common function which looks like below,
public async Task<SPResponse> ExecuteAsync(string spName, DynamicParameters p)
{
SPResponse response = new SPResponse();
using (SqlConnection conn = new SqlConnection(_connStr))
{
conn.Open();
using (SqlTransaction transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted))
{
try
{
p.Add("#SP_MESSAGE", dbType: DbType.String, direction: ParameterDirection.Output, size: 4000);
p.Add("#RETURNSTATUS", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
await conn.ExecuteAsync(sql: spName, param: p, commandType: CommandType.StoredProcedure, transaction: transaction);
response.ReturnMessage = p.Get<string>("#SP_MESSAGE");
response.ReturnStatus = Convert.ToString(p.Get<int>("#RETURNSTATUS"));
if (response.ReturnStatus == "0")
{
response.Ref1 = Convert.ToString(p.Get<int>("#SP_ID"));
transaction.Commit();
}
else
{
transaction.Rollback();
}
}
catch (Exception ex)
{
Utils.Logger.Instance.LogException(ex);
transaction.Rollback();
}
conn.Close();
}
}
return response;
}
now on response.Ref1 = Convert.ToString(p.Get<int>("#SP_ID")); line in some of my procedure I am getting SP_ID as output parameter and in some I am not getting SP_ID as output parameter
but the problem is when I am not returning SP_ID as output parameter I am getting error of
The given key was not present in the dictionary.
I want to check the key before execution of p.get<int>()
how can I do this?
So I fixed this by myself and thanks to #MarcGravell.
I declared a parameter in my DapperClass where I am using common ExecuteAsync method.
private DynamicParameters _Param;
public DapperClass()
{
_Param = new DynamicParameters();
}
now before transaction.Commit() line I am assigning the value to my parameter _Param = p;
and I created a public method with return type of DynamicParameters like below
public DynamicParameters GetDynamicParameters()
{
return _Param;
}
and also added a code like below from where I am executing my common dapper class
SPResponse response = await _Dapper.ExecuteAsync("[dbo].[TemplateAdd]", _DynamicParameter);
if (response.ReturnStatus == "0")
{
DynamicParameters dp = _Dapper.GetDynamicParameters();
response.Ref1 = Convert.ToString(dp.Get<int>("#SP_ID"));
response.Ref2 = request.FileServerId;
}

Generic Data Reader causing memory leak

I created a generic method in reading sql statement, but I am having a memory leak whenever I do a select query and using while read.
Sample Query:
public CMItemPackagingType GetItemPackagingType(int itemID)
{
try
{
List<CommandParameter> param = new List<CommandParameter>();
StringBuilder sb = new StringBuilder();
using (BaseConnection db = new BaseConnection())
{
sb.Append("SELECT RATIO, PACKAGING_TYPE_CODE FROM ITEM_PACKAGING_TYPE WHERE ROUND_UP = 0.01 AND ITEM_ID = #itemID");
param.Add(new CommandParameter("#itemID", itemID));
using (var rs = db.ExecSQL(sb.ToString(), param.ToArray()))
{
CMItemPackagingType cmItemInfo = new CMItemPackagingType();
while (rs.Read())
{
CMItemPackagingType list = new CMItemPackagingType();
if (!rs.IsDBNull(0))
list.Ratio = Convert.ToInt32(rs.GetValue(0));
if (!rs.IsDBNull(1))
list.PackagingTypeCode = rs.GetValue(1).ToString();
cmItemInfo.ItemPackagingTypeList.Add(list);
}
return cmItemInfo;
}
}
}
catch (Exception ex)
{
GlobalFramework.HandleException(ex);
}
return null;
}
Generic Reader:
public DbDataReader ExecSQL(string sqlStmt, CommandParameter[] param)
{
List<MySqlParameter> p = ParameterMySql(param);
_mySqlConn = new MySqlConnection(szConnect);
if (_mySqlConn.State == ConnectionState.Open)
{
_mySqlConn.Close();
}
_mySqlConn.Open();
_mySqlComm = new MySqlCommand(sqlStmt, _mySqlConn);
_mySqlComm.Parameters.AddRange(p.ToArray());
MySqlDataReader reader = _mySqlComm.ExecuteReader();
return reader;
}
I'm assuming the BaseConnection is a wrapper around a SqlConnection and _mySqlConn is an instance of BaseConnection. I suspect the issue is that you are opening and closing the connection in ExecSQL and at the same time have a using statement around BaseConnection creating this leak. I would refactor your code with proper placement of using statements to ensure correct disposal of the objects and freeing of resources.
Example
var query = "YOUR QUERY";
using (var connection = new SqlConnection("YOUR CONNECTION STRING"))
{
using (var command = new SqlCommand(query, connection))
{
await connection.OpenAsync();
using (var reader = await command.ExecuteReaderAsync())
{
if (reader != null)
{
while (await reader.ReadAsync())
{
// your logic
}
}
} // reader closed and disposed up here
} // command disposed here
} //connection closed and disposed here
}
Also notice how I'm using the async versions of the ADO.NET methods. Async commands are critical in achieving scale, throughput, and latency.
I recommend you use Dapper over trying to develop a generic data reader and writing all the boilerplate ADO.NET code yourself.

FileNet retrieving the GUID without SQL Query?

I am working on a program that upload PDF files to FileNet, and was wondering if there is a way to retrieve the GUID without running a SQL Query. I also tried Retrieving a Document (fetchInstance) from https://www.ibm.com/support/knowledgecenter/en/SSNW2F_5.5.0/com.ibm.p8.ce.dev.ce.doc/document_procedures.htm without the new Id part of the code and that did not work.
public class DocIdGenerator implements EventActionHandler {
private static final String DS = "ECMSvcsXA";
private static final String INSERT_SQL = "Insert into dbo.ICNLegalDocID_S (object_id) values (?)";
private static final String SELECT_SQL = "Select DocId From dbo.ICNLegalDocID_S Where object_id = ?";
#Override
public void onEvent(ObjectChangeEvent event, Id subscriptionId)
throws EngineRuntimeException {
// Get the Document object from the event
ObjectStore os = event.getObjectStore();
Id objectId = event.get_SourceObjectId();
PropertyFilter pf = new PropertyFilter();
pf.addIncludeProperty(new FilterElement(null, null, null, "DocumentID", null));
pf.addIncludeProperty(new FilterElement(null, null, null, PropertyNames.VERSION_SERIES, null));
Document sourceDoc = Factory.Document.fetchInstance(os, objectId, pf);
Properties props = sourceDoc.getProperties();
String documentId = props.getStringValue("DocumentID");
VersionSeries vs = sourceDoc.get_VersionSeries();
Id versionSeriesId = vs.get_Id();
if (documentId == null || documentId.isEmpty()) {
// Get the JNDI Context to lookup DataSource and Insert the objectId to get the auto generated docId
Context ctx;
DataSource ds;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
ctx = new InitialContext();
ds = (DataSource) ctx.lookup(DS);
con = ds.getConnection();
ps = con.prepareStatement(SELECT_SQL);
ps.setString(1, versionSeriesId.toString());
rs = ps.executeQuery();
BigDecimal docId = null;
if (rs.next()) {
// Document Id already exists
docId = rs.getBigDecimal(1);
} else {
// Document Id doesn't exist inert to get it
ps = con.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, versionSeriesId.toString());
ps.executeUpdate();
rs = ps.getGeneratedKeys();
if (rs.next()) {
docId = rs.getBigDecimal(1);
}
}
props.putValue("DocumentID", docId.toString());
sourceDoc.save(RefreshMode.REFRESH);
} catch (Exception e) {
e.printStackTrace();
if (e instanceof EngineRuntimeException) {
throw (EngineRuntimeException)e;
} else {
ErrorStack es = new ErrorStack("DocIdGeneratorSub", new ErrorRecord[] {new ErrorRecord(e)});
throw new EngineRuntimeException(es);
}
} finally {
close(con, ps, rs);
}
}
}
private void close(Connection con, PreparedStatement ps, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// Ignore
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
// Ignore
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// Ignore
}
}
}
}
I am not sure that I understand what to you trying to do.
When you create new document instance (Document doc = Factory.Document.newInstance()) and invoke doc.save(Refresh.REFRESH) options, FileNet inserts new DB record for document instance, generate new ID and populate it backward to the document instance on your side.
So, to get Id for new Document you just need to call doc.getId() to get it.
As I understand from your sample, you try to use event handler, If you invoke handler on the event before creation, you don't have any Id at this time, you cant try to handle the event after document creation.

System.ArgumentException: 'Cannot bind to the property 'Value' on the target control

I have developped light UserControl with some properties that support binding Like that
public class EditDateTimeBox : UserControl,IKeyboardNavigation
{
private TextBox textBox;
private MonthCalendar monthCalendar = new MonthCalendar();
ToolStripDropDown popup = new ToolStripDropDown();
ToolStripControlHost host;
//.....
[Bindable(true)]
public DateTime Value{get;set;}
}
but whene i try to bind it like that:
bool _binded = false;
string _dataColumn ;
[Category("OverB")]
[Browsable(true)]
public string DataColumn
{
get
{
return _dataColumn;
}
set
{
_dataColumn = value;
if (!_binded && !string.IsNullOrEmpty(_dataColumn) && _dataSource != null)
{
this.DataBindings.Add("Value", DataSource, _dataColumn,true);
_binded = true;
}
}
}
an error thrown say:
System.ArgumentException: 'Cannot bind to the property 'Value' on the target control
when i debug with dotnet support, i found that the Binding class(System.Windows.Forms) in the ChechBinding() method cause the problem
here is the code with comment
// If the control is being inherited, then get the properties for
// the control's type rather than for the control itself. Getting
// properties for the control will merge the control's properties with
// those of its designer. Normally we want that, but for
// inherited controls we don't because an inherited control should
// "act" like a runtime control.
//
InheritanceAttribute attr = (InheritanceAttribute)TypeDescriptor.GetAttributes(control)[typeof(InheritanceAttribute)];
if (attr != null && attr.InheritanceLevel != InheritanceLevel.NotInherited) {
propInfos = TypeDescriptor.GetProperties(controlClass);
}
else {
propInfos = TypeDescriptor.GetProperties(control);
}
for (int i = 0; i < propInfos.Count; i++) {
if(tempPropInfo==null && String.Equals (propInfos[i].Name, propertyName, StringComparison.OrdinalIgnoreCase)) {
tempPropInfo = propInfos[i];
if (tempPropIsNullInfo != null)
break;
}
if(tempPropIsNullInfo == null && String.Equals (propInfos[i].Name, propertyNameIsNull, StringComparison.OrdinalIgnoreCase)) {
tempPropIsNullInfo = propInfos[i];
if (tempPropInfo != null)
break;
}
}
if (tempPropInfo == null) {
throw new ArgumentException(SR.GetString(SR.ListBindingBindProperty, propertyName), "PropertyName");
Any idea?
sorry, i found the problem, the error is purly mine, here is the zomby code
public new ControlBindingsCollection DataBindings { get { return textBox.DataBindings; } }
sorry

Unit testing a controller that depends on a session variable

I have a controller that depends on a Session variable. In order to unit test this controller, I came up with the following solution. It works but I'm wondering if there is a better/cleaner way. Thanks
Controller
public JsonResult UpdateStatus(ImageUpdateStatus imageUpdateStatus, SessionStateItemCollection sessionItems = null)
{
var data = new object();
string status = null;
ImageInfo imageInfo = new ImageInfo();
IImageInfoServices svcImageInfo = new ImageInfoServicesRepository();
imageInfo = svcImageInfo.GetImageByImageId(imageUpdateStatus.ImageId);
IDeviceControlServices svcDevice = new DeviceControlServicesRespository();
IPVSCommandServices svcPVSCmds = new PVSCommandServicesRespository();
if (imageUpdateStatus.Task == "prep")
{
List<UpdateReasonForm> updateReasonForms;
if (sessionItems != null)
{
updateReasonForms = sessionItems["UpdateReasonForms"] as List<UpdateReasonForm>;
}
else
{
updateReasonForms = Session["UpdateReasonForms"] as List<UpdateReasonForm>;
}
foreach (var item in updateReasonForms)
{
if (item.ImageId == imageInfo.ImageId)
{
status = svcPVSCmds.PrepImage(imageInfo, item.NewVersion);
}
}
data = new
{
status
};
}
if (imageUpdateStatus.Task == "boot")
{
status = svcDevice.Boot(imageInfo.ImageId);
data = new
{
status
};
}
return this.Json(data, JsonRequestBehavior.AllowGet);
}
Unit Test
[TestMethod()]
public void UpdateStatusTest()
{
BuildController target = new BuildController(); // TODO: Initialize to an appropriate value
ImageUpdateStatus imageUpdateStatus = new ImageUpdateStatus(); // TODO: Initialize to an appropriate value
imageUpdateStatus.ImageId = 3;
imageUpdateStatus.Task = "prep";
UpdateReasonForm updateReasonForm = new UpdateReasonForm();
updateReasonForm.ImageId = 3;
updateReasonForm.NewVersion = "TestThis";
List<UpdateReasonForm> updateReasonForms = new List<UpdateReasonForm>();
updateReasonForms.Add(updateReasonForm);
var sessionItems = new SessionStateItemCollection();
sessionItems["UpdateReasonForms"] = updateReasonForms;
JsonResult actual;
actual = target.UpdateStatus(imageUpdateStatus, sessionItems);
}
Instead of passing in the session values as a parameter you can mock the session state like here:
How do you mock the session object collection using Moq
You have a dependency on Session. You could move your code into a testable method where you inject the dependency at the method level. It looks like you are on this path I would just abstract the code into its own method allowing you to test the functionality regardless of the whether the data comes from session or not.
public JsonResult UpdateStatusDependencyInjection(ImageUpdateStatus imageUpdateStatus, Dictionary<string, object> sessionValues)
{
var data = new object();
string status = null;
ImageInfo imageInfo = new ImageInfo();
IImageInfoServices svcImageInfo = new ImageInfoServicesRepository();
imageInfo = svcImageInfo.GetImageByImageId(imageUpdateStatus.ImageId);
IDeviceControlServices svcDevice = new DeviceControlServicesRespository();
IPVSCommandServices svcPVSCmds = new PVSCommandServicesRespository();
if (imageUpdateStatus.Task == "prep")
{
List<UpdateReasonForm> updateReasonForms;
if (sessionItems != null)
{
updateReasonForms = sessionItems["UpdateReasonForms"] as List<UpdateReasonForm>;
}
else
{
updateReasonForms = Session["UpdateReasonForms"] as List<UpdateReasonForm>;
}
foreach (var item in updateReasonForms)
{
if (item.ImageId == imageInfo.ImageId)
{
status = svcPVSCmds.PrepImage(imageInfo, item.NewVersion);
}
}
data = new
{
status
};
}
if (imageUpdateStatus.Task == "boot")
{
status = svcDevice.Boot(imageInfo.ImageId);
data = new
{
status
};
}
return this.Json(data, JsonRequestBehavior.AllowGet);
}
http://codingsmith.co.za/a-better-way-of-working-with-httpcontext-session-in-mvc/
This is my implementation of an interface wrapper for Session.
Its currently in production and works fine, its injected into my controllers, but I can use one of the other implementations manually when testing

Resources