public override string GetProxyCode(Incubator serviceProvider, IHttpContext context)
        {
            IRequest request             = context.Request;
            bool     includeLocalMethods = request.UserHostAddress.StartsWith("127.0.0.1");

            return(ServiceProxySystem.GenerateJsProxyScript(serviceProvider, serviceProvider.ClassNames, includeLocalMethods, context.Request).ToString());
        }
Example #2
0
        public override void ExecuteResult(ControllerContext context)
        {
            string defaultBaseAddress = ServiceProxySystem.GetBaseAddress(new RequestWrapper(context.HttpContext.Request));

            StringBuilder code = GenerateCSharpProxyCode(defaultBaseAddress);

            context.HttpContext.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName + ".cs");
            context.HttpContext.Response.AddHeader("Content-Type", "text/plain");

            context.HttpContext.Response.Write(code.ToString());
        }
        public override string GetProxyCode(Incubator serviceProvider, IHttpContext context)
        {
            IRequest  request            = context.Request;
            IResponse response           = context.Response;
            string    defaultBaseAddress = ServiceProxySystem.GetBaseAddress(request);
            string    nameSpace          = request.QueryString["namespace"] ?? "ServiceProxyClients";
            string    contractNameSpace  = "{0}.Contracts"._Format(nameSpace);

            string[] classNames          = request.QueryString["classes"] == null ? serviceProvider.ClassNames : request.QueryString["classes"].DelimitSplit(",", ";");
            bool     includeLocalMethods = request.UserHostAddress.StartsWith("127.0.0.1");

            return(Generate(defaultBaseAddress, classNames, nameSpace, contractNameSpace, serviceProvider, Logger, includeLocalMethods).ToString());
        }
Example #4
0
        public static StringBuilder GenerateCSharpProxyCode(string defaultBaseAddress, string[] classNames, string nameSpace, string contractNamespace, Incubator incubator, ILogger logger = null, bool includeLocalMethods = false)
        {
            logger = logger ?? Log.Default;
            List <Type> types = new List <Type>();

            classNames.Each(new { Logger = logger, Types = types }, (ctx, cn) =>
            {
                Type type = incubator[cn];
                if (type == null)
                {
                    ctx.Logger.AddEntry("Specified class name was not registered: {0}", LogEventType.Warning, cn);
                }
                else
                {
                    ctx.Types.Add(type);
                }
            });
            Args.ThrowIf(types.Count == 0, "None of the specified classes were found: {0}", string.Join(", ", classNames));
            return(ServiceProxySystem.GenerateCSharpProxyCode(defaultBaseAddress, nameSpace, contractNamespace, types.ToArray(), includeLocalMethods));
        }
Example #5
0
        protected internal PartialViewResult Html(ExecutionRequest request)
        {
            string viewName = string.Format("Void/{0}", request.ViewName);

            if (request.Result != null)
            {
                Type t = request.Result.GetType();
                viewName = string.Format("{0}/{1}", t.Name, request.ViewName);
                if (!ServiceProxySystem.ServiceProxyPartialExists(t, request.ViewName))
                {
                    ServiceProxySystem.WriteServiceProxyPartial(t, request.ViewName);
                }
            }
            else if (!ServiceProxySystem.ServiceProxyPartialExists("Void", request.ViewName))
            {
                ServiceProxySystem.WriteVoidServiceProxyPartial(request.ViewName);
            }

            return(PartialView(viewName, request.Result));
        }
Example #6
0
 public ServiceProxyInfo(Type type)
 {
     this.ClassName = "{0}.{1}"._Format(type.Namespace, type.Name);
     this.VarName   = ServiceProxySystem.GetVarName(type);
     this.Type      = type.FullName;
 }
Example #7
0
        public static StringBuilder GenerateCSharpProxyCode(string defaultBaseAddress, string nameSpace, string contractNamespace, Type[] types, bool includeLocalMethods = false)
        {
            StringBuilder    code            = new StringBuilder();
            StringBuilder    classes         = new StringBuilder();
            StringBuilder    interfaces      = new StringBuilder();
            HashSet <string> usingNamespaces = new HashSet <string>
            {
                "System",
                "Bam.Net.Configuration",
                "Bam.Net.ServiceProxy",
                "Bam.Net.ServiceProxy.Secure",
                contractNamespace
            };

            foreach (Type type in types)
            {
                StringBuilder methods          = new StringBuilder();
                StringBuilder interfaceMethods = new StringBuilder();
                foreach (MethodInfo method in ServiceProxySystem.GetProxiedMethods(type, includeLocalMethods))
                {
                    System.Reflection.ParameterInfo[] parameters = method.GetParameters();
                    MethodGenerationInfo methodGenInfo           = new MethodGenerationInfo(method);
                    bool   isVoidReturn = methodGenInfo.IsVoidReturn;
                    string returnType   = methodGenInfo.ReturnTypeCodeString;
                    methodGenInfo.UsingNamespaces.Each(ns =>
                    {
                        usingNamespaces.Add(ns);
                    });

                    string returnOrBlank      = isVoidReturn ? "" : "return ";
                    string genericTypeOrBlank = isVoidReturn ? "" : string.Format("<{0}>", returnType);
                    string invoke             = string.Format("{0}Invoke{1}", returnOrBlank, genericTypeOrBlank);

                    string methodParams         = methodGenInfo.MethodSignature;
                    string wrapped              = parameters.ToDelimited(p => p.Name.CamelCase()); // wrapped as object array
                    string methodApiKeyRequired = method.HasCustomAttributeOfType <ApiKeyRequiredAttribute>() ? "\r\n\t[ApiKeyRequired]" : "";
                    methods.AppendFormat(MethodFormat, methodApiKeyRequired, returnType, method.Name, methodParams, wrapped, invoke);
                    interfaceMethods.AppendFormat(InterfaceMethodFormat, returnType, method.Name, methodParams);
                }

                string serverName = type.Name;
                string clientName = serverName;
                if (clientName.EndsWith("Server"))
                {
                    clientName = clientName.Truncate(6);
                }

                string classFormatToUse   = type.HasCustomAttributeOfType <EncryptAttribute>() ? SecureClassFormat : ClassFormat;
                string typeApiKeyRequired = type.HasCustomAttributeOfType <ApiKeyRequiredAttribute>() ? "\r\n\t[ApiKeyRequired]" : "";
                classes.AppendFormat(classFormatToUse, typeApiKeyRequired, clientName, contractNamespace, serverName, defaultBaseAddress, methods.ToString());
                interfaces.AppendFormat(InterfaceFormat, serverName, interfaceMethods.ToString());
            }

            StringBuilder usings = new StringBuilder();

            usingNamespaces.Each(ns =>
            {
                usings.AppendFormat(UsingFormat, ns);
            });

            string usingStatements = usings.ToString();

            code.AppendFormat(HeaderFormat, defaultBaseAddress);
            code.AppendFormat(NameSpaceFormat, nameSpace, usingStatements, classes.ToString());
            code.AppendFormat(NameSpaceFormat, contractNamespace, usingStatements, interfaces.ToString());
            return(code);
        }
 static UrlHelperExtensions()
 {
     ResourceScripts.LoadScripts(typeof(Js.PlaceHolder));
     ServiceProxySystem.Initialize();
 }
Example #9
0
 private StringBuilder GenerateCSharpProxyCode(string defaultBaseAddress)
 {
     return(ServiceProxySystem.GenerateCSharpProxyCode(defaultBaseAddress, ClassNames, Namespace, ContractNamespace));
 }
Example #10
0
 public string Proxies()
 {
     return(ServiceProxySystem.GenerateJsProxyScript(_serviceProvider, _serviceProvider.ClassNames).ToString());
 }
Example #11
0
 private StringBuilder GetScript(string[] classNames)
 {
     return(ServiceProxySystem.GenerateJsProxyScript(_serviceProvider, classNames));
 }