Files
spark/src/client/spark_api/api/ServiceContext.cs
T

56 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace bts.api.core
{
public class ServiceContext : IDisposable
{
IntPtr _qbus;
public ServiceContext() {
_qbus = ApiNativeMethods.api_create_quickbus(null, 0);
}
internal ServiceContext(IntPtr qbus) { _qbus = qbus; }
internal IntPtr native_context { get { return _qbus; } }
public T locate<T>(string qualifier="") where T : class,IApiClient {
ClientChannel channel = new QuickbusClientChannel(this, typeof(T).Name, qualifier);
Type[] arg_types = new[] { typeof(ClientChannel) };
ConstructorInfo ctor = typeof(T).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First(ci => ci.GetParameters().SingleOrDefault()?.ParameterType == typeof(ClientChannel));
return (T)ctor.Invoke(new object[] { channel });
//return (T)Activator.CreateInstance(typeof(T), channel);
}
internal ServerChannel publish<T>(T service, string service_type, string qualifier) where T : IApiServer {
var ch = new ServerChannel(this, service_type, qualifier??"");
ch.on_request += service.dispatch;
return ch;
}
#region IDisposable Support
protected virtual void Dispose(bool disposing) {
if (_qbus != IntPtr.Zero) {
ApiNativeMethods.api_destroy_quickbus(_qbus);
_qbus = IntPtr.Zero;
}
}
~ServiceContext() {
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
public void Dispose() {
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}