c# - 在 .net core 项目中按环境加载 WCF 服务
问题描述
在 .NET 核心项目中添加 WCF 时遇到问题。当我过去使用 .net 时,我可以添加多个环境,web.config
以便在运行时加载正确的 Web 服务(Dev、Rec、Prod)。
当我将 WCF 服务的引用添加为 Connected Service 时,.net 核心项目中的问题创建了一个文件 ConnectedService.json,其中包含 WCF 服务的 URL。
{
"ProviderId": "Microsoft.VisualStudio.ConnectedService.Wcf",
"Version": "15.0.20406.879",
"GettingStartedDocument": {
"Uri": "https://go.microsoft.com/fwlink/?linkid=858517"
},
"ExtendedData": {
"Uri": "*****?singleWsdl",
"Namespace": "Transverse.TokenService",
"SelectedAccessLevelForGeneratedClass": "Public",
"GenerateMessageContract": false,
"ReuseTypesinReferencedAssemblies": true,
"ReuseTypesinAllReferencedAssemblies": true,
"CollectionTypeReference": {
"Item1": "System.Collections.Generic.List`1",
"Item2": "System.Collections.dll"
},
"DictionaryCollectionTypeReference": {
"Item1": "System.Collections.Generic.Dictionary`2",
"Item2": "System.Collections.dll"
},
"CheckedReferencedAssemblies": [],
"InstanceId": null,
"Name": "Transverse.TokenService",
"Metadata": {}
}
}
我的问题是如何根据使用的环境加载正确的服务。
笔记。
在我的项目中,我没有appsettings
web 配置。它是一个 .net 核心类库,在 ASP.NET 核心应用程序中被称为中间件。
解决方案
正如我从这篇文章中了解到的,这是微软的建议:
- 添加新的类文件
- 添加服务reference.cs的相同命名空间
- 添加 Partial Class 以扩展引用服务类(在 Reference.cs 中声明)
- 以及实现 ConfigureEndpoint() 的 Partial 方法(在 Reference.cs 中声明)
ConfigureEndpoint()
通过为 Endpoint 设置新值来实现方法
例子:
namespace Your_Reference_Service_Namespace
{
public partial class Your_Reference_Service_Client
{
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials)
{
serviceEndpoint.Address =
new System.ServiceModel.EndpointAddress(new System.Uri("http://your_web_service_address"),
new System.ServiceModel.DnsEndpointIdentity(""));
}
}
}
在这里,您可以从
appsettings.json
文件中获取值新 System.Uri(configuration.GetValue("yourServiceAddress")
推荐阅读
- php - 不情愿地返回一个目录
- javascript - 使用Javascript隐藏基于正文的元素类
- mysql - 使用 Laravel DB::select 和 DB:row 使用 MySql 服务器功能
- uvm - 我可以在虚拟音序器上调用 start_item
- ajax - Ajax 的加载时间因调用脚本而异?
- tensorflow - 如何使用 Pycharm 内置服务器启动 tensorboard?
- search - HAPI FHIR 自定义搜索响应
- java - 我如何调用这个 Ffun() 并访问这个类?
- python - 使用 Beautiful Soup 从可点击链接下载 CSV
- fortran - 如何从彩色波长转换为 RGB 或 HSL?