Beispiel #1
0
 public JsonContentSerializer(ClientOptions clientOptions, ApiNameMapping nameMapping = ApiNameMapping.Default, JsonSerializerSettings settings = null)
 {
     if (settings == null)
     {
         settings = new JsonSerializerSettings();
         settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()); //serialize enum as names, not numbers
     }
     SerializerSettings = settings;
     if (SerializerSettings.ContractResolver == null)
     {
         SerializerSettings.ContractResolver = new ClientJsonNameContractResolver(nameMapping); //to process NodeAttribute attribute
     }
     JsonSerializer = JsonSerializer.Create(SerializerSettings);
     SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
     SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true));
     _mediaTypes.Add(JsonMediaType);
     _mediaTypes.Add(JsonErrorMediaType);
 }
Beispiel #2
0
 public WebApiClientSettings(string serviceUrl, ClientOptions options = ClientOptions.Default,
                             ApiNameMapping nameMapping    = ApiNameMapping.Default, string clientName = null,
                             IContentSerializer serializer = null, IWebClientLogService log            = null,
                             Type badRequestContentType    = null, Type serverErrorContentType         = null)
 {
     Util.Check(!string.IsNullOrWhiteSpace(serviceUrl), "ServiceUrl may not be empty.");
     if (serviceUrl.EndsWith("/"))
     {
         serviceUrl = serviceUrl.Substring(0, serviceUrl.Length - 1);
     }
     ServiceUrl        = serviceUrl;
     Options           = options;
     NameMapping       = nameMapping;
     ClientName        = clientName;
     ContentSerializer = serializer ?? new JsonContentSerializer(options, nameMapping);
     Log = log;
     ServerErrorContentType       = serverErrorContentType ?? typeof(string);
     BadRequestContentType        = badRequestContentType ?? ServerErrorContentType;
     ThrowClientFaultOnBadRequest = BadRequestContentType == typeof(List <ClientFault>) || BadRequestContentType == typeof(IList <ClientFault>) ||
                                    BadRequestContentType == typeof(ClientFault[]);
 }
Beispiel #3
0
        public static void StartService(string baseAddress, ApiNameMapping jsonMappingMode)
        {
            var config = new HttpSelfHostConfiguration(baseAddress);

            // Add top-level handler first
            config.MessageHandlers.Add(new DiagnosticsHttpHandler());
            //Enable set-time-offset function in diagnostics controller - this should be done in testing environment only
            DiagnosticsController.EnableTimeOffset = true;
            //Add extra controllers we use in tests
            BooksApp.ApiConfiguration.RegisterControllerType(typeof(SpecialMethodsController));
            BooksApp.ApiConfiguration.RegisterController(new SingletonController("Some config info"));
            // Explicitly load ApiController type - to ensure WebApi can find it.
            // Needed for classic API controllers that live in a separate assembly (not in data service host project). Not needed for SlimApi controllers
            var contrTypes = new Type[] { typeof(ClassicWebApiController) };

            WebHelper.ConfigureWebApi(config, BooksApp, LogLevel.Details,
                                      WebHandlerOptions.ReturnBadRequestOnAuthenticationRequired | WebHandlerOptions.ReturnExceptionDetails, jsonMappingMode);
            config.MaxReceivedMessageSize = int.MaxValue;
            config.MaxBufferSize          = int.MaxValue;
            Server = new HttpSelfHostServer(config);
            Task.Run(() => Server.OpenAsync());
            Debug.WriteLine("The service is running on URL: " + baseAddress);
        }
Beispiel #4
0
        public static void ConfigureWebApi(HttpConfiguration httpConfiguration, EntityApp app,
                                           LogLevel logLevel = LogLevel.Basic,
                                           WebHandlerOptions webHandlerOptions = WebHandlerOptions.DefaultDebug,
                                           ApiNameMapping nameMapping          = ApiNameMapping.Default)
        {
            // Logging message handler
            var webHandlerStt     = new WebCallContextHandlerSettings(logLevel, webHandlerOptions);
            var webContextHandler = new WebCallContextHandler(app, webHandlerStt);

            httpConfiguration.MessageHandlers.Add(webContextHandler);

            // Exception handling filter - to handle/save exceptions
            httpConfiguration.Filters.Add(new ExceptionHandlingFilter());

            // Formatters - add formatters with spies, to catch/log deserialization failures
            httpConfiguration.Formatters.Clear();
            httpConfiguration.Formatters.Add(new StreamMediaTypeFormatter("image/jpeg", "image/webp")); //webp is for Chrome
            var xmlFmter = new XmlMediaTypeFormatter();

            httpConfiguration.Formatters.Add(xmlFmter);
            var jsonFmter = new JsonMediaTypeFormatter();

            // add converter that will serialize all enums as strings, not integers
            jsonFmter.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
            jsonFmter.SerializerSettings.ContractResolver     = new JsonNameContractResolver(nameMapping);
            jsonFmter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Unspecified;
            httpConfiguration.Formatters.Add(jsonFmter);

            //Api configuration
            if (app.ApiConfiguration.ControllerInfos.Count > 0)
            {
                ConfigureSlimApiControllers(httpConfiguration, app);
            }

            app.RegisterService <IBackgroundTaskService>(new WebBackgroundTaskService(app));
        }
Beispiel #5
0
 public WebApiClient(OperationContext context, string baseUrl, ClientOptions options = ClientOptions.Default,
                     ApiNameMapping nameMapping = ApiNameMapping.Default, string clientName = null,
                     Type badRequestContentType = null)
     : this(context, new WebApiClientSettings(baseUrl, options, nameMapping, clientName, badRequestContentType : badRequestContentType))
 {
 }
Beispiel #6
0
 public ClientJsonNameContractResolver(ApiNameMapping nameMapping)
 {
     NameMapping = nameMapping;
 }