此处的文档并没有对为什么有两种不同的操作来完成相同的事情提供太多解释,所以我想知道它们之间有什么区别。为什么我可以选择使用一种而不是另一种?
我有一个.Net Framework 4.6.1 WPF引用几个.Net Standard 2.0程序集的项目.这些程序集中的每一个都有自己的一个或两个依赖项,从NuGet引入.在Visual Studio内部,一切正常并且运行正常.但是,当我第一次尝试发布应用程序并运行它(在同一台机器上)时,我得到了这个令人讨厌的异常:
无法加载文件或程序集'System.Runtime,Version = 4.1.2.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一.该系统找不到指定的文件.
经过几天拉出我的头发,我终于发现将以下绑定重定向添加到我的项目的App.config解决了这个问题
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)
至少,它把问题转移到了System.ObjectModel.然后我添加了一个绑定重定向后,我得到了一个错误System.Collections,等等...不久之后,我的App.config看起来像这样:
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.ObjectModel" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.11.0" newVersion="4.0.11.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Collections" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.11.0" newVersion="4.0.11.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Reflection.Extensions" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.11.0" newVersion="4.0.11.0" />
</dependentAssembly>
<dependentAssembly> …Run Code Online (Sandbox Code Playgroud) 我一直有一个很难找到如何使用好资源jest.fn()嘲笑类上打字稿类和方法(如快递Request,Response并NextFunction和save()上一个猫鼬模型的方法。)
例如,假设我有以下模型和控制器:
型号/Foo.ts:
import * as mongoose from "mongoose"
export type FooModel = mongoose.Document & {
owner: mongoose.Schema.Types.ObjectId,
bars: string[]
}
const fooSchema = new mongoose.Schema({
owner: { type: mongoose.Schema.Types.ObjectId, ref: "User", index: true },
bars: [String]
}
export const Foo = mongoose.model<FooModel>("Foo", fooSchema)
Run Code Online (Sandbox Code Playgroud)
控制器/foo.ts:
import { Request, Response, NextFunction } from "express";
import { Foo, FooModel } from "../models/Foo";
export let createFoo = async (req: Request, res: Response, next: …Run Code Online (Sandbox Code Playgroud) 请注意:这是不是一个重复无法加载文件或程序集“System.ComponentModel.Annotations,版本= 4.1.0.0。与链接的问题不同,此问题仅在发布应用程序之后发生。正如您将在下面看到的那样,我尝试了该帖子中提出的所有解决方案,但均未成功。
在WPF .Net Framework 4.6.1项目中,该问题引用了.Net Standard 2.0库,该库本身引用了System.ComponentModel.Annotations NuGet包。
该问题在以下项目中再现:https : //github.com/kaitlynbrown/DataAnnotationsError
重现该错误:
您将看到以下错误:
我已经尝试了多种方法来解决此问题,包括:
在WPF项目的.csproj中添加以下行:
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
Run Code Online (Sandbox Code Playgroud)
在App.config中添加以下绑定重定向:
<runtime>
<dependentAssembly>
<assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0"/>
</dependentAssembly>
</runtime>
Run Code Online (Sandbox Code Playgroud)
在WPF项目中添加对System.ComponentModel.Annotations NuGet包的引用
这些都不起作用。
请注意:问题不在 Visual Studio中构建和运行。我能够做到这一点而没有错误。尝试发布应用程序并随后运行发布的应用程序时,会发生此问题。
.net c# .net-assembly assembly-binding-redirect .net-standard
我有一个抽象错误类用于继承服务错误,以使错误处理更容易一些。(ErrorBase来自https://gist.github.com/justmoon/15511f92e5216fa2624b#gistcomment-1928632)
export abstract class ServiceError extends ErrorBase {
abstract statusCode(): number;
abstract readonly errorCode: string;
static readonly defaultMessage: string;
constructor(readonly context?: any, message?: string) { super(message); }
toJSON(key: any) {
return {
errorCode: this.errorCode,
message: this.message,
context: this.context
};
}
}
Run Code Online (Sandbox Code Playgroud)
这是扩展它的类的示例:
export class ExampleError extends ServiceError {
statusCode(): number { return 400; }
errorCode: string = "err-example";
static readonly defaultMessage = "This is an example error";
constructor(context?: any, message?: string) {
super(context, message ? message : ExampleError.defaultMessage); …Run Code Online (Sandbox Code Playgroud) 我有一个电子应用程序,用于electron-builder构建、打包和发布应用程序。
我有以下自动更新代码:
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = "info";
autoUpdater.autoDownload = true;
const updateCheck = () => {
autoUpdater.checkForUpdates().then(resp => {
log.info("autoUpdate response:");
log.info(resp);
});
};
app.on("ready", async () => {
log.info(`Version: ${app.getVersion()}`);
autoUpdater.on("update-downloaded", () => {
log.info("update downloaded");
setImmediate(() => {
try {
log.info("installing update");
// app.relaunch();
autoUpdater.quitAndInstall();
} catch (err) {
log.error("Error installing update");
log.error(err);
}
});
});
autoUpdater.on("error", err => {
log.error("AutoUpdater error");
log.error(err);
});
updateCheck();
schedule.scheduleJob("*/10 * * * *", updateCheck);
});
Run Code Online (Sandbox Code Playgroud)
当我发布新版本时,自动更新程序会检测到它,成功下载它,然后尝试安装它。
在安装更新期间,进度条填满一半,然后消失。
应用程序保持关闭状态,并且在进度条消失后不会自动重新启动。 …
我有十几个服务,每个服务都有自己的无服务器模板文件,在 API Gateway 上共享相同的根 API。我的根serverless.yml文件定义了 API 和授权者:
...
resources:
Resources:
ApiGatewayRestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: ${self:service.name}-${self:provider.stage}
Description: Medimap API Gateway
GatewayResponseDefault4XX:
Type: "AWS::ApiGateway::GatewayResponse"
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_4XX
RestApiId:
Ref: "ApiGatewayRestApi"
Outputs:
RestApiId:
Value:
Ref: ApiGatewayRestApi
Export:
Name: ${self:service.name}-${self:provider.stage}-RestApiId
RootResourceId:
Value:
Fn::GetAtt: ApiGatewayRestApi.RootResourceId
Export:
Name: ${self:service.name}-${self:provider.stage}-ApiGatewayRestApiRootResourceId
AuthenticationService:
Value:
Fn::GetAtt: AuthenticationServiceLambdaFunction.Arn
Export:
Name: ${self:service.name}-${self:provider.stage}-AuthenticationService
functions:
authenticationService:
handler: src/api/common/authenticationService.handler
environment:
JWT_SECRET: ${env:JWT_SECRET}
events:
- http:
path: authenticationService
method: post
cors: true
Run Code Online (Sandbox Code Playgroud)
在我的每项服务的模板中,我都有如下代码:
provider:
...
apiGateway:
restApiId:
"Fn::ImportValue": …Run Code Online (Sandbox Code Playgroud)