GVKun编程网logo

JSONConfiguration在Jersey 2.5.x中去了哪里?(jsonserializeroptions)

6

在这篇文章中,我们将为您详细介绍JSONConfiguration在Jersey2.5.x中去了哪里?的内容,并且讨论关于jsonserializeroptions的相关问题。此外,我们还会涉及一些关

在这篇文章中,我们将为您详细介绍JSONConfiguration在Jersey 2.5.x中去了哪里?的内容,并且讨论关于jsonserializeroptions的相关问题。此外,我们还会涉及一些关于c# – ConfigurationSection ConfigurationManager.GetSection()始终返回null、c# – container.RegisterWebApiControllers(GlobalConfiguration.Configuration)导致InvalidOperationException、c# – System.Configuration.ConfigurationManager未解析、Component Configuration.js - 所有支持属性列表 - configuration priority的知识,以帮助您更全面地了解这个主题。

本文目录一览:

JSONConfiguration在Jersey 2.5.x中去了哪里?(jsonserializeroptions)

JSONConfiguration在Jersey 2.5.x中去了哪里?(jsonserializeroptions)

我正在将某些服务器代码从jersey 1.1版本移植到2.5.1,但似乎找不到JSONConfiguration。

是JAXB上下文解析器不再是Jersey 2.5的一部分,还是组ID已更改?还有另一种机制可以实现ContextResolver吗?

请帮忙。

答案1

小编典典

这是一个愚蠢的问题。如果其他人面临完成港口的压力,我会回答。

删除了使用 JAXBContext 的Jersey / Jackson ContextResovlerContextResovler
的新模型 使用 ObjectMapper __

有关示例,请参见Jersey示例目录中的ObjectMapperProvider。

c# – ConfigurationSection ConfigurationManager.GetSection()始终返回null

c# – ConfigurationSection ConfigurationManager.GetSection()始终返回null

我正在尝试学习如何使用ConfigurationSection类.我曾经使用IConfigurationSectionHandler,但发布它已被贬值.所以成为一个好小伙子,我正在尝试“正确”的方式.我的问题是它总是返回null.

我有一个控制台应用程序和一个DLL.

class Program
{
    static void Main(string[] args)
    {           
        StandardConfigSectionHandler section = StandardConfigSectionHandler.GetConfiguration();

        string value = section.Value;
    }
}

应用配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <sectionGroup name="ConfigSectionGroup">
      <section name="ConfigSection" type="Controller.StandardConfigSectionHandler,Controller" />
    </sectionGroup>
  </configSections>

  <ConfigSectionGroup>
    <ConfigSection>
      <test value="1" />
    </ConfigSection>
  </ConfigSectionGroup>

</configuration>

DLL中的section handler:

namespace Controller
{    
    public class StandardConfigSectionHandler : ConfigurationSection
    {
    private const string ConfigPath = "ConfigSectionGroup/ConfigSection/";

    public static StandardConfigSectionHandler GetConfiguration()
    {
        object section = ConfigurationManager.GetSection(ConfigPath);
        return section as StandardWcfConfigSectionHandler;
    }

    [ConfigurationProperty("value")]
    public string Value
    {
        get { return (string)this["value"]; }
        set { this["value"] = value; }
    }
  }
}

有什么值得我尝试的“ConfigPath”它将返回null,或者抛出一个错误,说“test”是一个无法识别的元素.我试过的价值:

> ConfigSectionGroup
> ConfigSectionGroup /
> ConfigSectionGroup / ConfigSection
> ConfigSectionGroup / ConfigSection /
> ConfigSectionGroup / ConfigSection / test
> ConfigSectionGroup / ConfigSection / test /

解决方法

你的代码有几个错误.

>你总是在你的GetConfiguration方法中返回null,但是我将假设这只是在问题中,而不是在你的实际代码中.
>更重要的是,ConfigPath值的格式不正确.您有一个尾部斜杠ConfigSectionGroup / ConfigSection /,删除最后一个斜杠,它将能够找到该部分.
>最重要的是,您声明部分的配置系统将会将您的“值”存储在ConfigSection元素的属性中.喜欢这个

<ConfigSectionGroup>
  <ConfigSection value="foo" />
</ConfigSectionGroup>

所以,把它放在一起:

public class StandardConfigSectionHandler : ConfigurationSection
{
    private const string ConfigPath = "ConfigSectionGroup/ConfigSection";

    public static StandardConfigSectionHandler GetConfiguration()
    {
        return (StandardConfigSectionHandler)ConfigurationManager.GetSection(ConfigPath);
    }

    [ConfigurationProperty("value")]
    public string Value
    {
        get { return (string)this["value"]; }
        set { this["value"] = value; }
    }
}

要了解有关如何配置配置部分的更多信息,请参阅这个出色的MSDN文档:How to: Create Custom Configuration Sections Using ConfigurationSection.它还包含有关如何将配置值存储在(像您的测试元素)的子元素中的信息.

c# – container.RegisterWebApiControllers(GlobalConfiguration.Configuration)导致InvalidOperationException

c# – container.RegisterWebApiControllers(GlobalConfiguration.Configuration)导致InvalidOperationException

在我的集成测试中,我使用的是我正在测试的Web API项目中构建的相同的SimpleInjector.Container.

但是组合根类中的这一行:

container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

导致异常:

System.TypeInitializationException : The type initializer for 'MyProject.Api.Test.Integration.HttpClientFactory' threw an exception.
---- system.invalidOperationException : This method cannot be called during the application's pre-start initialization phase.
Result StackTrace:  
at MyProject.Api.Test.Integration.HttpClientFactory.Create()
   at MyProject.Api.Test.Integration.Controllers.ProductControllerIntegrationTest.<GetProductBarcode_Should_Return_Status_BadRequest_When_Barcode_Is_Empty>d__0.MoveNext() in d:\Projects\My\MyProject.Api.Test.Integration\Controllers\ProductControllerIntegrationTest.cs:line 26
----- Inner Stack Trace -----
   at System.Web.Compilation.BuildManager.EnsuretopLevelFilesCompiled()
   at System.Web.Compilation.BuildManager.GetReferencedAssemblies()
   at System.Web.Http.WebHost.WebHostAssembliesResolver.System.Web.Http.dispatcher.IAssembliesResolver.GetAssemblies()
   at System.Web.Http.dispatcher.DefaultHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)
   at System.Web.Http.WebHost.WebHostHttpControllerTypeResolver.GetControllerTypes(IAssembliesResolver assembliesResolver)
   at SimpleInjector.SimpleInjectorWebApiExtensions.GetControllerTypesFromConfiguration(HttpConfiguration configuration)
   at SimpleInjector.SimpleInjectorWebApiExtensions.RegisterWebApiControllers(Container container,HttpConfiguration configuration)
   at MyProject.Api.ContainerConfig.RegisterTypes(Container container) in d:\Projects\My\MyProject.Api\App_Start\ContainerConfig.cs:line 128
   at MyProject.Api.ContainerConfig.CreateWebApiContainer() in d:\Projects\My\MyProject.Api\App_Start\ContainerConfig.cs:line 63
   at MyProject.Api.Test.Integration.HttpClientFactory..cctor() in d:\Projects\My\MyProject.Api.Test.Integration\HttpClientFactory.cs:line 17

评论后,一切正常,网络应用程序本身和测试.

所以问题是:

>例外的原因是什么?
>(这种方法真的需要吗?)

这是HttpClientFactory的代码(一个辅助类,用于创建具有适当头的HttpClient,例如api密钥或授权):

internal static class HttpClientFactory
{
    private static readonly Container _container = ContainerConfig.CreateWebApiContainer();

    public static HttpClient Create()
    {
        var client = new HttpClient { BaseAddress = GetUrl() };
        //...
        return client;
    }
}

解决方法

如果我们仔细观察堆栈跟踪,我们可以准确地看到这里发生了什么. RegisterWebApiControllers扩展方法在从HttpConfiguration获取的IHttpControllerTypeResolver实例上调用GetControllerTypes方法,并传递也从配置中检索的IAssembliesResolver.调用GetControllerTypes方法(WebHostHttpControllerTypeResolver)调用DefaultHttpControllerTypeResolver的GetControllerTypes,最终将调用System.Web.Compilation.BuildManager类的GetReferencedAssemblies.

但是,System.Web.Compilation.BuildManager不能在ASP.NET管道的早期调用,也不能在ASP.NET的上下文之外调用.由于您正在进行测试,BuildManage将抛出您遇到的异常.

所以这里的解决方案(或’技巧’)将在单元测试时替换默认的IAssembliesResolver.我认为旋转变压器看起来像这样:

public class TestAssembliesResolver : IAssembliesResolver
{
    public ICollection<Assembly> GetAssemblies()
    {
        return AppDomain.CurrentDomain.GetAssemblies();
    }
}

[TestMethod]
public void TestMethod1()
{
    // Replace the original IAssembliesResolver.
    GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver),new TestAssembliesResolver());

    var container = SimpleInjectorWebApiInitializer.BuildContainer();

    container.Verify();
}

你不得不处理这个问题有点不幸,特别是因为Simple Injector的设计是可测试的.我们似乎忽略了这一点,将RegisterWebApiControllers扩展方法与Web API深深地集成在一起.我们必须退后一步,思考如何更轻松地验证单元测试中的Web API配置.

c# – System.Configuration.ConfigurationManager未解析

c# – System.Configuration.ConfigurationManager未解析

在我的使用中,我有:

using System.Configuration;

然后在我的代码中:

string primary_adrs_key =  ConfigurationManager.AppSettings["PrimaryAddressK"];

但我得到错误:

“The name ConfigurationManager does not exist in the current context”

为什么?我错过了什么?

解决方法

阅读 ConfigurationManager

在项目中添加引用System.Configuration.dll

Component Configuration.js - 所有支持属性列表 - configuration priority

Component Configuration.js - 所有支持属性列表 - configuration priority

  • mockServerList
  • System defined defaults
  • Server wide defaults, read from sap-ui-config.json
  • Properties of the global configuration object window[“sap-ui-config”]
  • A configuration string in the data-sap-ui-config attribute of the bootstrap tag

Created by Jerry Wang, last modified on Sep 14, 2015

configuration.js的位置
clipboard1
configuration的优先级:
clipboard2

例子

  • in the URL : sap-ui-PARAMETER-NAME=“value”
  • in the DOM : data-sap-ui-PARAMETER-NAME=“value”
  • where PARAMETER-NAME is the name of the parameter in lower case.
  • Values of boolean parameters are case insensitive where “true” and “x” are interpreted as true.
    所有支持的parameter列表:
    clipboard3
    准备初始化odataModel:
    clipboard4
    拿到application里的配置:
    clipboard5

本文同步分享在 博客“汪子熙”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

今天关于JSONConfiguration在Jersey 2.5.x中去了哪里?jsonserializeroptions的介绍到此结束,谢谢您的阅读,有关c# – ConfigurationSection ConfigurationManager.GetSection()始终返回null、c# – container.RegisterWebApiControllers(GlobalConfiguration.Configuration)导致InvalidOperationException、c# – System.Configuration.ConfigurationManager未解析、Component Configuration.js - 所有支持属性列表 - configuration priority等更多相关知识的信息可以在本站进行查询。

本文标签: