forked from huysentruitw/SapNwRfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SapConnection.cs
172 lines (141 loc) · 5.58 KB
/
SapConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Diagnostics.CodeAnalysis;
using SapNwRfc.Internal.Interop;
using SapNwRfc.Internal.Metadata;
namespace SapNwRfc
{
/// <summary>
/// Represents an RFC connection to the SAP application server.
/// </summary>
public sealed class SapConnection : ISapConnection
{
private readonly RfcInterop _interop;
private readonly SapConnectionParameters _parameters;
private IntPtr _rfcConnectionHandle = IntPtr.Zero;
/// <summary>
/// Initializes a new instance of the <see cref="SapConnection"/> class with the given connection parameters.
/// </summary>
/// <param name="parameters">The connection parameters.</param>
[ExcludeFromCodeCoverage]
public SapConnection(SapConnectionParameters parameters)
: this(new RfcInterop(), parameters)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SapConnection"/> class with the given connection string.
/// </summary>
/// <param name="connectionString">The connection string.</param>
[ExcludeFromCodeCoverage]
public SapConnection(string connectionString)
: this(new RfcInterop(), SapConnectionParameters.Parse(connectionString))
{
}
// Constructor for unit-testing
internal SapConnection(RfcInterop interop, SapConnectionParameters parameters)
{
_interop = interop;
_parameters = parameters;
}
/// <summary>
/// Disposes the connection. Disposing automatically disconnects from the SAP application server.
/// </summary>
public void Dispose()
{
Disconnect(disposing: true);
}
/// <inheritdoc cref="ISapConnection"/>
public void Connect()
{
RfcConnectionParameter[] interopParameters = _parameters.ToInterop();
_rfcConnectionHandle = _interop.OpenConnection(
connectionParams: interopParameters,
paramCount: (uint)interopParameters.Length,
errorInfo: out RfcErrorInfo errorInfo);
errorInfo.ThrowOnError(beforeThrow: Clear);
}
/// <inheritdoc cref="ISapConnection"/>
public void Disconnect()
{
Disconnect(disposing: false);
}
private void Disconnect(bool disposing)
{
if (_rfcConnectionHandle == IntPtr.Zero)
return;
RfcResultCode resultCode = _interop.CloseConnection(
rfcHandle: _rfcConnectionHandle,
errorInfo: out RfcErrorInfo errorInfo);
Clear();
if (!disposing)
resultCode.ThrowOnError(errorInfo);
}
/// <inheritdoc cref="ISapConnection"/>
public bool IsValid
{
get
{
if (_rfcConnectionHandle == IntPtr.Zero)
return false;
RfcResultCode resultCode = _interop.IsConnectionHandleValid(_rfcConnectionHandle, out int isValid, out _);
return resultCode == RfcResultCode.RFC_OK && isValid > 0;
}
}
/// <inheritdoc cref="ISapConnection"/>
public bool Ping()
{
if (_rfcConnectionHandle == IntPtr.Zero)
return false;
RfcResultCode resultCode = _interop.Ping(
rfcHandle: _rfcConnectionHandle,
errorInfo: out _);
return resultCode == RfcResultCode.RFC_OK;
}
/// <inheritdoc cref="ISapConnection"/>
public SapConnectionAttributes GetAttributes()
{
RfcResultCode resultCode = _interop.GetConnectionAttributes(
rfcHandle: _rfcConnectionHandle,
attributes: out RfcAttributes attributes,
errorInfo: out RfcErrorInfo errorInfo);
resultCode.ThrowOnError(errorInfo);
return new SapConnectionAttributes(attributes);
}
/// <inheritdoc cref="ISapConnection"/>
public ISapTypeMetadata GetTypeMetadata(string typeName)
{
IntPtr typeDescriptionHandle = _interop.GetTypeDesc(
rfcHandle: _rfcConnectionHandle,
typeName: typeName,
errorInfo: out RfcErrorInfo errorInfo);
errorInfo.ThrowOnError();
return new SapTypeMetadata(_interop, typeDescriptionHandle);
}
/// <inheritdoc cref="ISapConnection"/>
public ISapFunctionMetadata GetFunctionMetadata(string functionName)
{
IntPtr functionDescriptionHandle = _interop.GetFunctionDesc(
rfcHandle: _rfcConnectionHandle,
funcName: functionName,
errorInfo: out RfcErrorInfo errorInfo);
errorInfo.ThrowOnError();
return new SapFunctionMetadata(_interop, functionDescriptionHandle);
}
/// <inheritdoc cref="ISapConnection"/>
public ISapFunction CreateFunction(string name)
{
IntPtr functionDescriptionHandle = _interop.GetFunctionDesc(
rfcHandle: _rfcConnectionHandle,
funcName: name,
errorInfo: out RfcErrorInfo errorInfo);
errorInfo.ThrowOnError();
return SapFunction.CreateFromDescriptionHandle(
interop: _interop,
rfcConnectionHandle: _rfcConnectionHandle,
functionDescriptionHandle: functionDescriptionHandle);
}
private void Clear()
{
_rfcConnectionHandle = IntPtr.Zero;
}
}
}