private static Compilation createCompilation(string text, List <Diagnostic> errors = null, IPersistentStorage storage = null, CompilationAnalysis analysis = null) { var injector = new CompositeInjector <SyntaxToken, SyntaxNode, SemanticModel>(new[] { new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>(compiler => compiler .Environment() .dependency <ExcessOwinMiddleware>("Excess.Server.Middleware") .dependency <IAppBuilder>("Owin") .dependency <IOwinRequest>("Microsoft.Owin") .dependency <__Scope>("Excess.Runtime")), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>( compiler => ConcurrentExtension.Apply((RoslynCompiler)compiler)), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>( compiler => ServerExtension.Apply(compiler, new Scope(null))), new DelegateInjector <SyntaxToken, SyntaxNode, SemanticModel>( compiler => Functions.Apply(compiler)) }); if (analysis != null) { ServerExtension.Compilation(analysis); } var compilation = new Compilation(storage, analysis: analysis); compilation.addDocument("test", text, injector); return(compilation); }
public static SyntaxTree Compile(string code, out string output) { RoslynCompiler compiler = new RoslynCompiler(environment: null); ServerExtension.Apply(compiler, compiler.Scope); ConcurrentExtension.Apply(compiler); return(compiler.ApplySemanticalPass(code, out output)); }
/// <summary> /// get current context callback /// </summary> /// <typeparam name="T">type of callback</typeparam> /// <param name="context">client context</param> /// <returns>list of callback context</returns> public static ClientContext <T> GetClientCallbackOfClientContext <T>(this OperationContext context) { #if (NETSTANDARD1_6 || NETCOREAPP1_1) if (typeof(T).GetTypeInfo().IsInterface) #else if (typeof(T).IsInterface) #endif { if (!context.ServerBase.Callbacks.ContainsKey(context.Client)) { context.ServerBase.RegisterCallbacksForClient(context.Client); if (!context.ServerBase.Callbacks.ContainsKey(context.Client)) { try { throw new Exception($"context client not exist! {context.Client.SessionId} {context.ServerBase.Callbacks.Count} {context.ServerBase.Services.Count} {DateTime.Now}"); } catch (Exception ex) { AutoLogger.LogError(ex, "GetClientCallbackOfClientContext"); } return(null); } else { var attribName1 = (typeof(T).GetCustomAttributes <ServiceContractAttribute>(true).FirstOrDefault()).Name; var serviceType1 = context.ServerBase.GetRegisteredCallbacksTypeByName(attribName1); var find1 = context.ServerBase.FindClientCallbackByType(context.Client, serviceType1); if (find1 == null) { try { throw new Exception($"context client not exist 2 ! {context.Client.SessionId} {context.ServerBase.Callbacks.Count} {context.ServerBase.Services.Count} {DateTime.Now}"); } catch (Exception ex) { AutoLogger.LogError(ex, "GetClientCallbackOfClientContext 2"); } return(null); } return(new ClientContext <T>(find1, context.Client)); } } #if (NETSTANDARD1_6 || NETCOREAPP1_1) var attribName = ((ServiceContractAttribute)typeof(T).GetTypeInfo().GetCustomAttributes(typeof(ServiceContractAttribute), true).FirstOrDefault()).Name; #else var attribName = ((ServiceContractAttribute)typeof(T).GetCustomAttributes(typeof(ServiceContractAttribute), true).FirstOrDefault()).Name; #endif var serviceType = context.ServerBase.GetRegisteredCallbacksTypeByName(attribName); var find = context.ServerBase.FindClientCallbackByType(context.Client, serviceType); if (find != null) { return(new ClientContext <T>(find, context.Client)); } var obj = CSCodeInjection.InstanceServerInterface <T>(serviceType, new List <Type>() { typeof(ServiceContractAttribute) }); //dynamic dobj = obj; if (CSCodeInjection.InvokedServerMethodAction == null) { ServerExtension.Init(); } var field = serviceType #if (NETSTANDARD1_6 || NETCOREAPP1_1) .GetTypeInfo() #endif .GetProperty("InvokedServerMethodAction"); field.SetValue(obj, CSCodeInjection.InvokedServerMethodAction, null); var field2 = serviceType #if (NETSTANDARD1_6 || NETCOREAPP1_1) .GetTypeInfo() #endif .GetProperty("InvokedServerMethodFunction"); field2.SetValue(obj, CSCodeInjection.InvokedServerMethodFunction, null); //dobj.InvokedServerMethodAction = CSCodeInjection.InvokedServerMethodAction; //dobj.InvokedServerMethodFunction = CSCodeInjection.InvokedServerMethodFunction; var op = obj as OperationCalls; op.ServerBase = context.ServerBase; op.CurrentClient = context.Client; //context.ServerBase.RegisteredCallbacksTypes.ContainsKey(attribute.Name); context.ServerBase.Callbacks[context.Client].Add(obj); if (!(obj is OperationCalls)) { Shared.Log.AutoLogger.LogText("is not OprationCalls: " + obj.ToString(), true); } return(new ClientContext <T>(obj, context.Client)); } else { Shared.Log.AutoLogger.LogText("is not interface: " + typeof(T).ToString(), true); return(new ClientContext <T>((T)context.ServerBase.FindClientCallbackByType(context.Client, typeof(T)), context.Client)); } }