Close user sessions AXAPTA - asp.net

im making a website who uses the Dynamics AX Business Connector to connect with AX, it´s working fine but sometimes the users don´t logout.
Here is my code:
Microsoft.Dynamics.BusinessConnectorNet.Axapta DynAx = new Microsoft.Dynamics.BusinessConnectorNet.Axapta();
try
{
DynAx.Logon(null, null, null, null);
//Execute some methods
DynAx.Logoff();
}
catch (Exception ex)
{
DynAx.Logoff();
}
and in ax i can see the users logged in. Again this happens sometimes, thats why i don´t know who may be.
Maybe Dispose() Method it's better?.
Thank you for taking your time to read this.

Logon/logoff works for me correctly, but if you're saying sometimes it doesn't, then the reason is most likely one of the following:
The business connector can be flaky. It's wasn't a Microsoft priority and eventually was depreciated.
Whatever is happening in your //Execute some methods section could be locking or preventing the logoff.
You may need to update your kernel to get an updated version of the business connector
In my AX2012R3 environment I can run the below PowerShell code over and over with success. This points me towards one of the above as a cause.
Add-Type -Path "C:\Program Files\Microsoft Dynamics AX\60\BusinessConnector\Bin\Microsoft.Dynamics.BusinessConnectorNet.dll"
$ax = new-object Microsoft.Dynamics.BusinessConnectorNet.Axapta
 
$ax.logon($null, $null, $null, $null)
$b = $ax.CreateAxaptaRecord("userinfo")
$array = New-Object System.Collections.ArrayList
 
$b.ExecuteStmt("select id from %1")
while($b.found){
$array.add($b.get_field("id")) | out-null
$b.next() | out-null
}
 
$array | Format-Table -AutoSize
$ax.Logoff()
$ax.Dispose()

Related

Dynamics AX 2012 RegConfig does not work

I'm currently developping a failover service for an environment using Dynamics AX and 2 mirrored SQL servers, and I have some issues getting AX to work the way I expect it to.
I have developped a service which does the following :
- try to connect to the SQL servers instances
- start Dynamics AX using the reachable SQL server.
To do this, I have created 2 AX configuration files (.axc), each one pointing to a SQL server.
But when I try to start the service, no mater which way I use, AX always start using the configuration that is set using the AX server configuration tool.
Here are the command I've tried to start the AX service :
sc start AOS60$01 -regConfig=Config1
net start AOS60$01 /"-regConfig=Config1"
The service always start successfully, but doesn't care about the regConfig parameter.
As anybody an idea about how to solve this issue?
Regards,
Thomas T.
After looking for a while after a way to start the service with the -regConfig parameter, I finally gave up and developped a method which directly edit the Registry key holding the startup configuration value.
private void UpdateRegistry(string parameter)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\services\\Dynamics Server\\6.0\\01", true);
key.SetValue("Current", parameter, RegistryValueKind.String);
key.Close();
}
public void StartLocalServiceWithCLI(string serviceToStart, string parameter)
{
try
{
UpdateRegistry(parameter);
Process process = new System.Diagnostics.Process();
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = string.Format("/C sc start {0} ", serviceToStart);
process.StartInfo = startInfo;
process.Start();
logger.WriteInfo(string.Format("Process {0} starting, parameters [{1}]", serviceToStart, parameter));
}
catch (Exception e)
{
logger.WriteError(string.Format("Error starting process {0}, parameters [{1}]\nError details :{2}", serviceToStart, parameter, e.Message));
}
}

Thread aborting issue with Sharp Svn with C#.Net 4.0

I have developed ASP.net application using VS-2010, C#.Net 4.0 with SharpSvn dll. When I'm working with dev server(don't have 3-Tier Architecture), it works fine. But when we are working with QA environment(have 3-Tier Architecture) it gives thread abort exception most of the time.Following shows the code and error log I have. Any help on this really appreciate.
public bool Checkout(string svnurl, string target)
{
try
{
using (_client = new SharpSvn.SvnClient())
{
_client.LoadConfiguration(Path.Combine(Path.GetTempPath(), "Svn"), true);
_client.Authentication.DefaultCredentials = new TNetworkCredential(_username, _password);
_client.Authentication.SslServerTrustHandlers += SvnSslOveride;
var targetsvn = new SvnUriTarget(svnurl);
if (_client.CheckOut(targetsvn, target))
{
Log.Info("Successfully checked out to following location : " );
Log.Info(target);
return true;
}
}
Log.Info("Unable to checkout "+ svnurl +" Svn location to target location : ");
Log.Info(target);
return false;
}
catch (Exception ee)
{
Log.Error("Error:SvnClient checkout....");
Log.Error(ee);
throw ee;
return false;
}
}
private static void SvnSslOveride(object sender, SvnSslServerTrustEventArgs e)
{
e.AcceptedFailures = e.Failures;
e.Save = true;
}
error log
ERROR 2013-08-12 12:13:37,714 3223821ms SvnClient Checkout -
Error:SvnClient checkout.... ERROR 2013-08-12 12:13:37,730 3223837ms
SvnClient Checkout - System.Threading.ThreadAbortException: Thread was
being aborted. at svn_client_checkout3(Int32* , SByte* , SByte* ,
svn_opt_revision_t* , svn_opt_revision_t* , svn_depth_t , Int32 ,
Int32 , svn_client_ctx_t* , apr_pool_t* ) at
SharpSvn.SvnClient.CheckOut(SvnUriTarget url, String path,
SvnCheckOutArgs args, SvnUpdateResult& result) at
SharpSvn.SvnClient.CheckOut(SvnUriTarget url, String path)
I missed <httpRuntime executionTimeout="(time in seconds)">
tag in web config and it automatically set by IIS server. The default is 110 seconds.
Note :In the .NET Framework 1.0 and 1.1, the default is 90 seconds.
After I added following line to web.config and it works fine.
<httpRuntime executionTimeout="600">
Previously it works sometimes because the time taken to SVN checkout in DEV server is less than in other environment because of the size of the repositories and network connections. Thanks all for your answers
As per my comment above, I've seen this issue. It appears to be related to plink authentication.
I resolved it by upgrading to the latest build of SharpSVN v1.7, at which point the error changed from a null-ref exception in the C++, to an SVN exception with the message "Can't create tunnel: The parameter is incorrect".
There are a few articles which explain how to resolve this, the best of which I've found here:
SVN+SSH and Sourceforge
In my case, changing the backslashes to forwardslashes in the SVN_SSH env var resolved the problem. Worth giving a shot.

Getting App_Start Code First Migrations to work with Miniprofiler

I am running code first migrations. (EF 4.3.1)
I am also running Miniprofiler.
I run my code first migrations through code on App_Start.
My code looks like this:
public static int IsMigrating = 0;
private static void UpdateDatabase()
{
try
{
if (0 == System.Threading.Interlocked.Exchange(ref IsMigrating, 1))
{
try
{
// Automatically migrate database to catch up.
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Checking db for pending migrations.")));
var dbMigrator = new DbMigrator(new Ninja.Data.Migrations.Configuration());
var pendingMigrations = string.Join(", ", dbMigrator.GetPendingMigrations().ToArray());
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("The database needs these code updates: " + pendingMigrations)));
dbMigrator.Update();
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("Done upgrading database.")));
}
finally
{
System.Threading.Interlocked.Exchange(ref IsMigrating, 0);
}
}
}
catch (System.Data.Entity.Migrations.Infrastructure.AutomaticDataLossException ex)
{
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
}
}
The problem is that my DbUpdate is about to get called and then my app throws an exception which I think comes from the app on the first web page request.
saying:
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
The problem is that I think my homepage is firing the dbcontext and this error before my dbupdate has finished.
How would you go about solving this?
Should I make the context wait using locks etc or is there an easier way?
More interestingly, If i start and stop the app a few times the db changes are pushed and the error goes away...
So I need to find a way to have the first request to the database on App_Start wait for the migrations to happen.
Thoughts?

SDL Tridion 2009: Creating components through TOM API (via Interop) fails

Am facing a problem, while creating components through TOM API using .NET/COM Interop.
Actual Issue:
I have 550 components to be created through custom page. I am able to create between 400 - 470 components but after that it is getting failed and through an error message saying that
Error: Thread was being aborted.
Any idea / suggestion, why it is getting failed?
OR
Is there any restriction on Tridion 2009?
UPDATE 1:
As per #user978511 request, below is error on Application event log:-
Event code: 3001
Event message: The request has been aborted.
...
...
Process information:
Process ID: 1016
Process name: w3wp.exe
Account name: NT AUTHORITY\NETWORK SERVICE
Exception information:
Exception type: HttpException
Exception message: Request timed out.
...
...
...
UPDATE 2:
#Chris: This is my common function, which is called in a loop by passing list of params. Here am using Interop dll's.
public static bool CreateFareComponent(.... list of params ...)
{
TDSE mTDSE = null;
Folder mFolder = null;
Component mComponent = null;
bool flag = false;
try
{
mTDSE = TDSEInitialize();
mComponent = (Component)mTDSE.GetNewObject(ItemType.ItemTypeComponent, folderID, null);
mComponent.Schema = (Schema)mTDSE.GetObject(constants.SCHEMA_ID, EnumOpenMode.OpenModeView, null, XMLReadFilter.XMLReadAll);
mComponent.Title = compTitle;
...
...
...
...
mComponent.Save(true);
flag = true;
}
catch (Exception ex)
{
CustomLogger.Error(String.Format("Logged User: {0} \r\n Error: {1}", GetRemoteUser(), ex.Message));
}
return flag;
}
Thanks in advance.
Sounds like a timeout, most likely in IIS which is hosting your custom page.
Are you creating them all in one synchronous request? Because that is indeed likely to time out.
You could instead create them in batches - or make sure your operations are done asynchronously and then polling the status regularly.
The easiest would just be to only create say 10 Components in one request, wait for it to finish, and then create another 10 (perhaps with a nice progress bar? :))
How you call TDSE object. I would like to mention here "Marshal.ReleaseComObject" procedure. Without releasing COMs objects can lead to enormous memory leaks.
Here is code for component creating:
private Component NewComponent(string componentName, string publicationID, string parentID, string schemaID)
{
Publication publication = (Publication)mTdse.GetObject(publicationID, EnumOpenMode.OpenModeView, null, XMLReadFilter.XMLReadContext);
Folder folder = (Folder)mTdse.GetObject(parentID, EnumOpenMode.OpenModeView, null, XMLReadFilter.XMLReadContext);
Schema schema = (Schema)mTdse.GetObject(schemaID, EnumOpenMode.OpenModeView, publicationID, XMLReadFilter.XMLReadContext);
Component component = (Component)mTdse.GetNewObject(ItemType.ItemTypeComponent, folder, publication);
component.Title = componentName;
component.Schema = schema;
return component;
}
After that please not forget to release mTdse ( in my case it is previously created TDSE object). Disposing "Components" object can be useful also after finish working with them.
For large Tridion batch operations I always use a Console Application and run it directly on the server.
Use Console.WriteLine to write to the output window and Console.ReadLine as the last line of code in the app (so the window stays open). I also use Log4Net as the logger.
This is by far the best approach if you have access to a remote session on the server - or can ask an admin to run it for you and give you access to the log folder via a network share.
As per #chris suggestions and part of immediate fix I have changed my web.config execution time out to 8000 seconds.
<httpRuntime executionTimeout="8000"/>
With this change, custom page is able to handle as of now.
Any more best suggestion, please post it.

Powershell start-job, wait-job, host thread never exits when run from ASP.NET IIS

I am currently trying to build a threaded cleanup script with powershell, initiated from an IIS.
I have made a threaded "Kill process by owner" using powershell remoting, running from the same list of servers as my cleanup script, and that works no problem.
$jobs = #()
foreach ($comp in $comps) {
$jobs += start-job -FilePath ("CleanupJob.ps1") -ArgumentList $comp, $username
Write-Host "Started Job on: $comp"
}
foreach($job in $jobs)
{
$job | wait-job | out-null
receive-job $job.ID
}
Remove-Job -State Completed
When i run my script from the powershell console, the whole thing runs perfectly, main thread starts 28 new processes, which start a remoting connection to each server, and waits for all the jobs to finish. When they are finished, i get my output and the host thread exists. All running as planned.
Not so when I run it from my asp.net application, i get ""Started Job on: $comp"" for each of my 28 servers, but only a result from the first 14, and then the host thread just sits there. (untill i kill it with fire, and my output is returned to the asp.net webpage)
I have no way to see what happens in the script when i run it from the asp.net page.All i can see is cpu/ram usage drop to the same levels as when i run it from the PSconsole, but my main thread never closes. So I do believe the script works as supposed, but my webpage hangs untill the main thread closes(which it never does, as stated).
This is how I call my script (not the pretty .net <3 powershell way:)
public string RunProgramme(string scriptpath, string arguments)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"powershell.exe";
startInfo.Arguments = "& \'"+scriptpath+"\' "+ arguments;
//return startInfo.Arguments;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
string errors = process.StandardError.ReadToEnd();
return output;
}
The mystery deepens, added this line to my threaded jobs
Invoke-Command -Session $session -ScriptBlock {param($path) net send mymachine $path} -Args $msg
And when i run my script from the IIS, i receive message from each machine.
Just as my ram usage shows, all the jobs are run, but the output isnt returned properly, and my host thread just sits there...waiting...
As a note, you've got some unnecessary stuff going on.
foreach ($comp in $comps) {
start-job -FilePath ("CleanupJob.ps1") -ArgumentList $comp, $username
Write-Verbose "Started Job on: $comp"
}
Get-Job | Wait-Job | Out-Null
Remove-Job -State Completed
PowerShell already constructs a job list; there's no need to do so in a $jobs variable, and no need to enumerate them to do the wait.
Of course, you may use $jobs for something else in your code - but just wanted to make sure other folks see this alternative.
I found the solution. It's a deadlock caused by calling
string output = process.StandardOutput.ReadToEnd();
string errors = process.StandardError.ReadToEnd();
Right after each other.
Instead i followed http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandarderror.aspx#Y95 and did this instead:
public string RunProgramme(string scriptpath, string arguments)
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = #"powershell.exe";
startInfo.Arguments = "& \'"+scriptpath+"\' "+ arguments;
startInfo.ErrorDialog = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.BeginErrorReadLine();
//Use BeginErrorReadLine on one of the streams to avoid the deadlock
//(i didnt need my error stream, but I did need to filter the errors from my output, so i did this)
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit(1000 * 60);
return output;
}
No more hanging :)
you could use something like this to see the commands and the output...
$logfile = "C:\Documents and Settings\username\My Documents\WindowsPowerShell\logs\$(Get-Date -uformat "%Y%m%d - %H%M%S").log"
Start-Transcript -Path $logfile
I'm interested to know how you're calling it from ASP.Net as well?

Resources