Skip to content
This repository was archived by the owner on Jan 29, 2022. It is now read-only.

Commit 8e72fac

Browse files
committed
v2.0.0.0 fixes after testing
1 parent f35ffb9 commit 8e72fac

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

AMSoftware.Crm.PowerShell.Commands/ConnectOrganizationCommand.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,27 @@ protected override void ExecuteCmdlet()
4242
{
4343
base.ExecuteCmdlet();
4444

45-
if (Connection == null)
45+
try
4646
{
47-
try
47+
if (!string.IsNullOrWhiteSpace(Connectionstring))
4848
{
4949
Connection = new CrmServiceClient(Connectionstring);
50+
}
5051

51-
if (Connection != null && Connection.IsReady)
52-
{
53-
CrmContext.Connect(Connection);
54-
WriteObject(Connection.OrganizationDetail);
55-
}
56-
else
57-
{
58-
throw new Exception(Connection.LastCrmError, Connection.LastCrmException);
59-
}
52+
if (Connection != null && Connection.IsReady)
53+
{
54+
CrmContext.Connect(Connection);
55+
WriteObject(Connection.OrganizationDetail);
6056
}
61-
catch {
62-
throw;
57+
else
58+
{
59+
throw new Exception(Connection.LastCrmError, Connection.LastCrmException);
6360
}
6461
}
65-
66-
62+
catch
63+
{
64+
throw;
65+
}
6766
}
6867
}
6968
}

AMSoftware.Crm.PowerShell.Common/CrmContext.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace AMSoftware.Crm.PowerShell.Common
2525
{
2626
internal static class CrmContext
2727
{
28+
private static CrmSession _session = null;
29+
2830
static CrmContext()
2931
{
3032
try
@@ -34,17 +36,24 @@ static CrmContext()
3436
accelerators.InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, null, new object[] { "crmoptionsetvalue", typeof(OptionSetValue) });
3537
accelerators.InvokeMember("Add", System.Reflection.BindingFlags.InvokeMethod, null, null, new object[] { "crmlabel", typeof(Label) });
3638
}
37-
catch {
38-
39+
catch
40+
{
41+
3942
}
4043

4144
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
4245
}
4346

4447
public static void Connect(CrmServiceClient connection)
4548
{
46-
47-
Session = new CrmSession(connection);
49+
if (connection != null && connection.IsReady)
50+
{
51+
Session = new CrmSession(connection);
52+
}
53+
else
54+
{
55+
throw new Exception("Invalid connection");
56+
}
4857
}
4958

5059
public static void Disconnect()
@@ -57,8 +66,21 @@ public static void Disconnect()
5766

5867
public static CrmSession Session
5968
{
60-
get;
61-
private set;
69+
get
70+
{
71+
if (_session != null && _session.Client != null && _session.Client.IsReady)
72+
{
73+
return _session;
74+
}
75+
else
76+
{
77+
throw new Exception("Session is not available or not connected. Use Connect-CrmOrganization to connect.");
78+
}
79+
}
80+
private set
81+
{
82+
_session = value;
83+
}
6284
}
6385
}
6486
}

0 commit comments

Comments
 (0)