Пример #1
0
        internal static string GetClientProxyScript(Type contractType, string path, bool debugMode, ServiceEndpoint serviceEndpoint)
        {
            ContractDescription            contract       = ContractDescription.GetContract(contractType);
            WebServiceData                 webServiceData = GetWebServiceData(contract);
            WCFServiceClientProxyGenerator proxyGenerator = new WCFServiceClientProxyGenerator(path, debugMode, serviceEndpoint);

            return(proxyGenerator.GetClientProxyScript(webServiceData));
        }
Пример #2
0
        static WebServiceData GetWebServiceData(ContractDescription contract)
        {
            WebServiceData serviceData = new WebServiceData();

            //build method dictionary
            Dictionary <string, WebServiceMethodData> methodDataDictionary = new Dictionary <string, WebServiceMethodData>();

            // set service type
            serviceData.Initialize(new WebServiceTypeData(XmlConvert.DecodeName(contract.Name), XmlConvert.DecodeName(contract.Namespace), contract.ContractType),
                                   methodDataDictionary);

            foreach (OperationDescription operation in contract.Operations)
            {
                Dictionary <string, WebServiceParameterData> parameterDataDictionary = new Dictionary <string, WebServiceParameterData>();
                bool useHttpGet = operation.Behaviors.Find <WebGetAttribute>() != null;
                WebServiceMethodData methodData = new WebServiceMethodData(serviceData, XmlConvert.DecodeName(operation.Name), parameterDataDictionary, useHttpGet);
                // build parameter dictionary
                MessageDescription requestMessage = operation.Messages[0];
                if (requestMessage != null)
                {
                    int numMessageParts = requestMessage.Body.Parts.Count;
                    for (int p = 0; p < numMessageParts; p++)
                    {
                        MessagePartDescription messagePart = requestMessage.Body.Parts[p];
                        // DevDiv 129964:JS proxy generation fails for a WCF service that uses an untyped message
                        // Message or its derived class are special, used for untyped operation contracts.
                        // As per the WCF team proxy generated for them should treat Message equivalent to Object type.
                        Type paramType = ReplaceMessageWithObject(messagePart.Type);
                        WebServiceParameterData parameterData = new WebServiceParameterData(XmlConvert.DecodeName(messagePart.Name), paramType, p);
                        parameterDataDictionary[parameterData.ParameterName] = parameterData;
                        serviceData.ProcessClientType(paramType, false, true);
                    }
                }
                if (operation.Messages.Count > 1)
                {
                    // its a two way operation, get type information from return message
                    MessageDescription responseMessage = operation.Messages[1];
                    if (responseMessage != null)
                    {
                        if (responseMessage.Body.ReturnValue != null && responseMessage.Body.ReturnValue.Type != null)
                        {
                            // operation has a return type, add type to list of type proxy to generate
                            serviceData.ProcessClientType(ReplaceMessageWithObject(responseMessage.Body.ReturnValue.Type), false, true);
                        }
                    }
                }

                //add known types at operation level
                for (int t = 0; t < operation.KnownTypes.Count; t++)
                {
                    serviceData.ProcessClientType(operation.KnownTypes[t], false, true);
                }

                methodDataDictionary[methodData.MethodName] = methodData;
            }
            serviceData.ClearProcessedTypes();
            return(serviceData);
        }
Пример #3
0
        private static bool ShouldSkipAuthorization(HttpContext context)
        {
            if (context == null || context.Request == null)
            {
                return(false);
            }

            string path = context.Request.FilePath;

            if (ScriptResourceHandler.IsScriptResourceRequest(path))
            {
                return(true);
            }

            // if auth service is disabled, dont bother checking.
            // (NOTE: if a custom webservice is used, it will be up to them to enable anon access to it)
            // if it isn't a rest request dont bother checking.
            if (!ApplicationServiceHelper.AuthenticationServiceEnabled || !RestHandlerFactory.IsRestRequest(context))
            {
                return(false);
            }

            if (context.SkipAuthorization)
            {
                return(true);
            }

            // it may be a rest request to a webservice. It must end in axd if it is an app service.
            if ((path == null) || !path.EndsWith(".axd", StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            // WebServiceData caches the object in cache, so this should be a quick lookup.
            // If it hasnt been cached yet, this will cause it to be cached, so later in the request
            // it will be a cache-hit anyway.
            WebServiceData wsd = WebServiceData.GetWebServiceData(context, path, false, false);

            if ((wsd != null) && (_authenticationServiceType == wsd.TypeData.Type))
            {
                return(true);
            }

            return(false);
        }
Пример #4
0
        private void OnPostAcquireRequestState(object sender, EventArgs eventArgs)
        {
            HttpApplication app     = (HttpApplication)sender;
            HttpRequest     request = app.Context.Request;

            if (app.Context.Handler is Page && RestHandlerFactory.IsRestMethodCall(request))
            {
                // Get the data about the web service being invoked
                WebServiceData webServiceData = WebServiceData.GetWebServiceData(HttpContext.Current, request.FilePath, false, true);

                // Get the method name
                string methodName = request.PathInfo.Substring(1);

                // Get the data about the specific method being called
                WebServiceMethodData methodData = webServiceData.GetMethodData(methodName);
                RestHandler.ExecuteWebServiceCall(HttpContext.Current, methodData);

                // Skip the rest of the page lifecycle
                app.CompleteRequest();
            }
        }
Пример #5
0
        public bool Submit(FormModel formModel, IPublishedContent content)
        {
            if (_configuration.Url == null)
            {
                Log.Warning("Could not submit form data to the web service - the web service endpoint URL was not configured correctly.");
                return(false);
            }

            var client = new WebClient
            {
                Encoding = Encoding.UTF8
            };

            // set content type
            client.Headers[HttpRequestHeader.ContentType] = "application/json";

            if (string.IsNullOrEmpty(_configuration.UserName) == false && string.IsNullOrEmpty(_configuration.Password) == false)
            {
                // basic auth, base64 encode of username:password
                var credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", _configuration.UserName, _configuration.Password)));
                client.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);
            }

            try
            {
                var webServiceData = new WebServiceData
                {
                    UmbracoContentName = content.Name,
                    UmbracoContentId   = content.Id,
                    IndexRowId         = formModel.RowId.ToString(),
                    FormData           = formModel.AllValueFields().Select(field => new FormFieldData
                    {
                        Name           = field.Name,
                        FormSafeName   = field.FormSafeName,
                        Type           = field.Type,
                        SubmittedValue = field.SubmittedValue
                    }).ToArray(),
                    SubmittedValues = formModel.AllValueFields().ToDictionary(
                        field => field.FormSafeName,
                        field => field.SubmittedValue
                        )
                };

                // serialize to JSON using camel casing
                var data = JsonConvert.SerializeObject(webServiceData, new JsonSerializerSettings {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

                // set content type
                client.Headers[HttpRequestHeader.ContentType] = "application/json";

                // POST the JSON and log the response
                var response = client.UploadString(_configuration.Url, "POST", data);
                Log.Info(string.Format("Form data was submitted to endpoint: {0} - response received was: {1}", _configuration.Url, string.IsNullOrEmpty(response) ? "(none)" : response));

                return(true);
            }
            catch (WebException wex)
            {
                using (var reader = new StreamReader(wex.Response.GetResponseStream()))
                {
                    var response = reader.ReadToEnd();
                    Log.Error(wex, string.Format("An error occurred while trying to submit form data to: {0}. Error details: {1}", _configuration.Url, response), null);
                }
                return(false);
            }
            catch (Exception ex)
            {
                Log.Error(ex, string.Format("An error occurred while trying to submit form data to: {0}.", _configuration.Url));
                return(false);
            }
        }
Пример #6
0
 protected override string GetProxyTypeName(WebServiceData data)
 {
     return(GetClientTypeNamespace(data.TypeData.TypeName));
 }