.net核心项目中按环境加载WCF服务 [英] load WCF service by environment in .net core project
问题描述
在.NET核心项目中添加WCF时遇到问题. 过去使用.net时,我可以在web.config中添加多个环境,以便可以在运行时加载正确的Web服务(Dev,Rec,Prod).
I have a problem while adding WCF in .NET core project. When I used .net in the past I can add multiple environment in web.config so I can load the correct web service at runtime (Dev, Rec, Prod).
.net核心项目中的问题,当我添加对WCF服务的引用作为Connected Service时,它创建了一个文件ConnectedService.json,其中包含WCF服务的URL.
The problem in .net core project when I added a reference of my WCF service as Connected Service it created one file ConnectedService.json that contains a url for the WCF service.
{
"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": {}
}
}
我的问题是如何根据使用的环境加载正确的服务.
My question how can I load the correct service based on the used environment.
注意.
在我的项目中,我既没有appsettings也没有网络配置.这是一个.net核心类库,在ASP.NET核心应用程序中被称为中间件.
In my Project, I did not have an appsettings neither web config. It is a .net core class library and it is called in ASP.NET core Application as Middleware.
推荐答案
该解决方案对谁感兴趣 我在每个appseetings.{environment} .json中添加了服务的终结点,并在Service类中根据环境变量ASPNETCORE_ENVIRONMENT注入了服务的新实例
For whom are interested by the solution I added an endpoint for my service in each appseetings.{environment}.json and in Service class I inject new Instance of my service based on the environment variable ASPNETCORE_ENVIRONMENT
services.AddTransient<Transverse.TokenService.ITokenService>(provider =>
{
var client = new Transverse.TokenService.TokenServiceClient();
client.Endpoint.Address = new System.ServiceModel.EndpointAddress(Configuration["Services:TokenService"]);
return client;
});
也许不是最好的,但是效果很好.
Maybe is not the best but it works fine.
这篇关于.net核心项目中按环境加载WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!