Exemplo n.º 1
0
 public void AjaxResponseSendingEvent_Test()
 {
     SnowflakeEventManager.InitEventSource();
     new StandardEvents().RegisterSnowflakeEvents(SnowflakeEventManager.EventSource);
     var fakeCoreService = new Mock<ICoreService>();
     var fakeJsResponse = new Mock<IJSResponse>();
     var args = new AjaxResponseSendingEventArgs(fakeCoreService.Object, fakeJsResponse.Object);
     SnowflakeEventManager.EventSource.Subscribe<AjaxResponseSendingEventArgs>((s, e) =>
     {
         Assert.Equal(args, e);
     });
     SnowflakeEventManager.EventSource.RaiseEvent(args);
 }
Exemplo n.º 2
0
        public string CallMethod(IJSRequest request)
        {
            var callMethodEvent = new AjaxRequestReceivedEventArgs(this.CoreInstance, request);
            SnowflakeEventManager.EventSource.RaiseEvent(callMethodEvent);
            request = callMethodEvent.ReceivedRequest;
            AjaxResponseSendingEventArgs sendResultEvent;
            try
            {
                IJSResponse result;
                IJSMethod jsMethod = this.GlobalNamespace[request.NameSpace].JavascriptMethods[request.MethodName];
                foreach (AjaxMethodParameterAttribute attr in jsMethod.MethodInfo.GetCustomAttributes<AjaxMethodParameterAttribute>()
                    .Where(attr => attr.Required)
                    .Where(attr => !(request.MethodParameters.Keys.Contains(attr.ParameterName))))
                {
                    result = new JSResponse(request, JSResponse.GetErrorResponse($"missing required param {attr.ParameterName}"), false);
                    sendResultEvent = new AjaxResponseSendingEventArgs(this.CoreInstance, result);
                    SnowflakeEventManager.EventSource.RaiseEvent(sendResultEvent);
                    return sendResultEvent.SendingResponse.GetJson();
                }

                result = jsMethod.Method.Invoke(request);
                sendResultEvent = new AjaxResponseSendingEventArgs(this.CoreInstance, result);
                SnowflakeEventManager.EventSource.RaiseEvent(sendResultEvent);
                return sendResultEvent.SendingResponse.GetJson();
            }
            catch (KeyNotFoundException)
            {
                var result = new JSResponse(request, JSResponse.GetErrorResponse($"method {request.MethodName} not found in namespace {request.NameSpace}"), false);
                sendResultEvent = new AjaxResponseSendingEventArgs(this.CoreInstance, result);
                SnowflakeEventManager.EventSource.RaiseEvent(sendResultEvent);
                return sendResultEvent.SendingResponse.GetJson();
            }
            catch (Exception e)
            {
                var result = new JSResponse(request, e, false);
                sendResultEvent = new AjaxResponseSendingEventArgs(this.CoreInstance, result);
                SnowflakeEventManager.EventSource.RaiseEvent(sendResultEvent);
                return sendResultEvent.SendingResponse.GetJson();
            }
        }