小编Tom*_*Tom的帖子

类型参数'System.Net.Http.Headers.MediaTypeHeaderValue'违反了类型参数'T'的约束

我有一个Web API解决方案(针对.NET 4.6),其中包含几个相当轻量级的.NET Core项目.我已将.NET Core项目打包为NuGet包,并将它们安装到Web API项目中.

一切都很好,但运行时,我在应用程序初始化时得到以下异常.

Method System.Net.Http.CloneableExtensions.Clone: type argument 'System.Net.Http.Headers.MediaTypeHeaderValue' violates the constraint of type parameter 'T'.

[VerificationException: Method System.Net.Http.CloneableExtensions.Clone: type argument 'System.Net.Http.Headers.MediaTypeHeaderValue' violates the constraint of type parameter 'T'.]
   System.Net.Http.Formatting.MediaTypeConstants.get_ApplicationJsonMediaType() +0
   System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() +64
   System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters() +41
   System.Web.Http.HttpConfiguration.DefaultFormatters(HttpConfiguration config) +26
   System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) +214
   System.Web.Http.GlobalConfiguration.<CreateConfiguration>b__0() +60
   System.Lazy`1.CreateValue() +411
   System.Lazy`1.LazyInitValue() +183
   System.Lazy`1.get_Value() +75
   System.Web.Http.GlobalConfiguration.get_Configuration() +27
   Runpath.Platform.Web.DependencyResolution.StructureMapBootStrapper.Initialise() in C:\Code3\Runpath\Markets\Platform\Main - Copy\Runpath.Platform.Web\DependencyResolution\StructureMapBootStrapper.cs:15
   Runpath.Platform.Web.WebApiApplication.Application_Start() in C:\Code3\Runpath\Markets\Platform\Main - Copy\Runpath.Platform.Web\Global.asax.cs:30

[HttpException (0x80004005): Method System.Net.Http.CloneableExtensions.Clone: type argument 'System.Net.Http.Headers.MediaTypeHeaderValue' violates the constraint of type parameter 'T'.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api .net-core

44
推荐指数
3
解决办法
2万
查看次数

禁用实体框架代理创建

根据我的阅读,设置ProxyCreationEnabled = false将阻止更改跟踪和延迟加载.但是,我不清楚跟踪的变化.

如果我禁用它并从数据库中获取实体,对其进行更改并提交,则会保存这些更改.我还可以从ChangeTracker获得修改后的条目:

ChangeTracker.Entries<IAuditable>().Where(x => x.State == EntityState.Modified).ToList()
Run Code Online (Sandbox Code Playgroud)

我禁用代理创建时是否可以这样做?我想禁用它,但我想明确我禁用的内容.

c# entity-framework

10
推荐指数
2
解决办法
9191
查看次数

运行ASP.NET Web应用程序时找不到System.Runtime 4.1.0.0

我用以下projects.json创建了一个可移植类库

{
  "supports": {},
  "dependencies": {
    "Microsoft.CSharp": "4.0.1",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0",
    "System.Runtime.Serialization.Primitives": "4.1.1",
    "System.Runtime": "4.1.0"
  },
  "frameworks": {
    "net451": {},
    "netstandard1.5": {}
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,当从ASP.NET应用程序(而不是ASP.NET Core)引用它时,我得到以下运行时异常:

Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
Run Code Online (Sandbox Code Playgroud)

如果我从控制台应用程序引用它,它运行没有问题.

有任何想法吗?

编辑

在这里找到解决方案:https://stackoverflow.com/a/37639003/691045

asp.net .net-core

8
推荐指数
1
解决办法
2万
查看次数

NHibernate - 左连接

我有以下两个表:

作业 AreaID,JobNo(复合键)

记录 LogID,AreaID,JobNo

我需要获得所有没有与之关联的日志的作业.在SQL中我可以这样做:

SELECT Jobs.AreaID,
       Jobs.JobNo
FROM   Jobs
       LEFT JOIN Logs
           ON Jobs.AreaID = Logs.AreaID
           AND Jobs.JobNo = Logs.JobNo
WHERE  Logs.LogID is null
Run Code Online (Sandbox Code Playgroud)

但我不知道如何用NHibernate来实现这一目标.任何人都可以提供任何指示吗?

这是我的映射:

<class name="Job" table="Jobs">
    <composite-key name="Id">
        <key-property name="JobNo"/>
        <key-many-to-one name="Area" class="Area" column="AreaID"/>
    </composite-key>
</class>

<class name="Log" table="Logs">
    <id name="Id" column="LogID">
        <generator class="identity"/>
    </id>
    <property name="JobNo"/>
    <many-to-one name="Area" class="Area" column="AreaID"/>
</class>
Run Code Online (Sandbox Code Playgroud)

谢谢

更新

好的,我略微修改了诺西拉的答案,现在正在做我想要的事情:

Log logs = null;

return session.QueryOver<Job>()
    .Left.JoinAlias(x => x.Logs, () => logs)
    .Where(x => logs.Id == null)
    .List<Job>();
Run Code Online (Sandbox Code Playgroud)

我还必须将此添加到我的Job映射中:

<bag …
Run Code Online (Sandbox Code Playgroud)

nhibernate join left-join outer-join

6
推荐指数
2
解决办法
1万
查看次数

反序列化XML为集合属性返回null

我正在尝试反序列化以下XML:

<?xml version="1.0" encoding="utf-8" ?> 
<mf:somedata xmlns:mf="urn:somedata">
    <CurrentAccount>
        <AccountType>test</AccountType>
        <Charge>
            <ChargeType>test</ChargeType>
        </Charge>
    </CurrentAccount>
    <CurrentAccount>
        <AccountType>test 2</AccountType>
        <Charge>
            <ChargeType>test 2</ChargeType>
        </Charge>
    </CurrentAccount>
</mf:somedata>
Run Code Online (Sandbox Code Playgroud)

使用以下类:

[XmlRoot("somedata", Namespace = "urn:somedata")]
public class MfCurrentAccounts
{
    [XmlElement("CurrentAccount")]
    public CurrentAccount[] CurrentAccounts { get; set; }
}

public class CurrentAccount
{
    public string AccountType { get; set; }

    [XmlElement("Charge")]
    public Charge[] Charges { get; set; }
}

public class Charge
{
    public string ChargeType { get; set; }
}

var c = new XmlSerializer(typeof(MfCurrentAccounts)).Deserialize(new StringReader(xml)) as MfCurrentAccounts; …
Run Code Online (Sandbox Code Playgroud)

c# xml xmlserializer deserialization

6
推荐指数
1
解决办法
4243
查看次数

在.NET 4.6项目中使用HttpContextAccessor

我创建了一个.NET Core项目(一个类库),它也面向.NET 4.6,它需要能够访问当前的HTTP上下文.我看到我们不能再使用静态了HttpContext.Current,必须注入一个实例IHttpContextAccessor.这是否仍然可以用于针对.NET 4.6的Web API项目?到目前为止,HttpContextAccessor.HttpContext除了null之外,我无法返回任何内容.

c# .net-core asp.net-core

6
推荐指数
1
解决办法
1512
查看次数

生成 NSwag 客户端作为构建的一部分

我有一个项目,它使用 NSwag 从 swagger 文件生成客户端和合同。我不希望 git 跟踪这些生成的文件,因此当项目在构建服务器上构建时,它会将它们作为构建的一部分生成。

我一直在尝试使用 MSBuild 目标来尝试让它工作,它生成文件但随后构建失败,因为还有一些其他类引用了生成的类。

这是我目前在 csproj 文件中的内容:

<Target Name="NSwag" BeforeTargets="BeforeBuild;BeforeRebuild">
    <Exec Command="$(NSwagExe_Core21) run nswag.json /variables:Configuration=$(Configuration)" IgnoreExitCode="true" />
</Target>
Run Code Online (Sandbox Code Playgroud)

这有可能吗?

编辑

Nswag 规范在这里:

{
  "runtime": "NetCore21",
  "defaultVariables": null,
  "swaggerGenerator": {
    "fromSwagger": {
      "url": "swagger.json",
      "output": null
    }
  },
  "codeGenerators": {
    "swaggerToCSharpClient": {
      "clientBaseClass": "ClientBase", //name of your client base class
      "configurationClass": null,
      "generateClientClasses": true,
      "generateClientInterfaces": true,
      "generateDtoTypes": true,
      "injectHttpClient": true,
      "disposeHttpClient": true,
      "protectedMethods": [],
      "generateExceptionClasses": true,
      "exceptionClass": "SwaggerException",
      "wrapDtoExceptions": true,
      "useHttpClientCreationMethod": false,
      "httpClientType": "System.Net.Http.HttpClient",
      "useHttpRequestMessageCreationMethod": …
Run Code Online (Sandbox Code Playgroud)

c# msbuild asp.net-core nswag

5
推荐指数
1
解决办法
2642
查看次数

使用StringEnumConverter生成模式

是否可以使用Json.NET生成一个模式,将枚举值输出为字符串而不是整数?我注意到有人分叉代码来执行此操作,但是想知道是否有其他方法可以执行此操作,或者是否有任何计划要执行此操作.

编辑

要清楚,我正在尝试使用它来生成一个模式:

var schemaGenerator = new JsonSchemaGenerator();
var schema = schemaGenerator.Generate(typeof(Class1));

return schema.ToString();
Run Code Online (Sandbox Code Playgroud)

schema json.net

3
推荐指数
2
解决办法
1481
查看次数