如何在ASP.Net Core中注入WCF服务客户端? [英] How to inject WCF service client in ASP.Net core?
问题描述
我有需要从ASP.NET Core访问的WCF服务。我已经安装了 WCF连接预览,并成功创建了代理。 p>
它创建了界面&客户端如下所示
[System.CodeDom.Compiler.GeneratedCodeAttribute( dotnet-svcutil, 0.3.0.0)]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName = ServiceReference1.IDocumentIntegration)]
公共接口IDocumentIntegration
{
[System.ServiceModel.OperationContractAttribute(Action = http://tempuri.org/IDocumentIntegration/SubmitDocument,ReplyAction = http://tempuri.org/IDocumentIntegration/SubmitDocumentResponse)]
[System.ServiceModel.FaultContractAttribute(typeof((ServiceReference1.FaultDetail)),Action =
系统 http://tempuri.org/IDocumentIntegration/SubmitDocumentFaultDetailFault、Name=\"FaultDetail\"、Namespace=\"http://schemas.datacontract.org/2004/07/MyCompany.Framework.Wcf)] .Threading.Tasks.Task< string> SubmitDocumentAsync(string documentXml);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute( dotnet-svcutil, 0.3.0.0)]
公共接口IDocumentIntegrationChannel:ServiceReference1.IDocumentIntegration,System.ServiceModel .IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute( dotnet-svcutil, 0.3 .0.0)]
公共局部类DocumentIntegrationClient:System.ServiceModel.ClientBase< ServiceReference1.IDocumentIntegration> ;, ServiceReference1.IDocumentIntegration
{
//此处的构造函数和方法
}
调用该服务的消费者类别如下
公共类Consumer
{
private IDocumentIntegration _client;
公共消费者(IDocumentIntegration客户端)
{
_client =客户端;
}
公共异步任务过程(字符串ID)
{
await _client.SubmitDocumentAsync(id);
}
}
如何在Startup中使用IConfigIntegrationServices方法注册IDocumentIntegration类?
我要设置RemoteAddress&注册期间的clientCredentials
public void ConfigureServices(IServiceCollection服务)
{
services.AddApplicationInsightsTelemetry(Configuration) ;
services.AddMvc();
//如何在此处注入DocumentIntegrationClient?
var client = new DocumentIntegrationClient();
client.ClientCredentials.UserName.UserName = myusername;
client.ClientCredentials.UserName.Password =密码;
client.Endpoint.Address =新的EndpointAddress(基于环境的url)
}
使用工厂方法重载似乎是合适的用例。
services.AddScoped< IDocumentIntegration>(provider => {
var client = new DocumentIntegrationClient();
//使用配置对象从appconfig.json
client.ClientCredentials.UserName.UserName = Configuration [ MyService: Username];
client.ClientCredentials.UserName.Password = Configuration [ MyService:Password];
client.Endpoint.Address = new EndpointAddress(Configuration [ MyService:BaseUrl]);
返还客户;
});
您的应用设置如下
{
...
MyService:
{
Username: guest,
Password: guest,
BaseUrl: http://www.example.com/
}
}
或者,通过options模式注入Options。由于 DocumentIntegrationClient
是局部的,因此您可以创建一个新文件并添加一个参数化的构造函数。
公共局部类DocumentIntegrationClient:
System.ServiceModel.ClientBase< ServiceReference1.IDocumentIntegration> ,ServiceReference1.IDocumentIntegration
{
public DocumentIntegrationClient(IOptions< DocumentServiceOptions>选项):base()
{
if(options == null)
{
抛出新的ArgumentNullException(nameof(options));
}
this.ClientCredentials.Username.Username = options.Username;
this.ClientCredentials.Username.Password = options.Password;
this.Endpoint.Address = new EndpointAddress(options.BaseUrl);
}
}
并创建选项类
公共类DocumentServiceOptions
{
公共字符串用户名{get;组; }
公用字符串密码{get;组; }
公共字符串BaseUrl {get;组; }
}
并从 appsettings.json填充
。
services.Configure< DocumentServiceOptions>(Configuration.GetSection( MyService ));
I have WCF service that I need to access from ASP.NET Core. I have installed WCF Connected Preview and created proxy successfully.
It created interface & client something like below
[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.3.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IDocumentIntegration")]
public interface IDocumentIntegration
{
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IDocumentIntegration/SubmitDocument", ReplyAction="http://tempuri.org/IDocumentIntegration/SubmitDocumentResponse")]
[System.ServiceModel.FaultContractAttribute(typeof(ServiceReference1.FaultDetail), Action="http://tempuri.org/IDocumentIntegration/SubmitDocumentFaultDetailFault", Name="FaultDetail", Namespace="http://schemas.datacontract.org/2004/07/MyCompany.Framework.Wcf")]
System.Threading.Tasks.Task<string> SubmitDocumentAsync(string documentXml);
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.3.0.0")]
public interface IDocumentIntegrationChannel : ServiceReference1.IDocumentIntegration, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.3.0.0")]
public partial class DocumentIntegrationClient : System.ServiceModel.ClientBase<ServiceReference1.IDocumentIntegration>, ServiceReference1.IDocumentIntegration
{
// constructors and methods here
}
The consumer class that calls the service looks like below
public class Consumer
{
private IDocumentIntegration _client;
public Consumer(IDocumentIntegration client)
{
_client = client;
}
public async Task Process(string id)
{
await _client.SubmitDocumentAsync(id);
}
}
How do I register the IDocumentIntegration with ConfigureServices method in Startup class? I want to setup RemoteAddress & clientCredentials during the registration
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc();
// how do I inject DocumentIntegrationClient here??
var client = new DocumentIntegrationClient();
client.ClientCredentials.UserName.UserName = "myusername";
client.ClientCredentials.UserName.Password = "password";
client.Endpoint.Address = new EndpointAddress(urlbasedonenvironment)
}
Using the factory method overload seems suitable use case for it.
services.AddScoped<IDocumentIntegration>(provider => {
var client = new DocumentIntegrationClient();
// Use configuration object to read it from appconfig.json
client.ClientCredentials.UserName.UserName = Configuration["MyService:Username"];
client.ClientCredentials.UserName.Password = Configuration["MyService:Password"];
client.Endpoint.Address = new EndpointAddress(Configuration["MyService:BaseUrl"]);
return client;
});
Where your appsettings would look like
{
...
"MyService" :
{
"Username": "guest",
"Password": "guest",
"BaseUrl": "http://www.example.com/"
}
}
Alternatively, inject the Options via options pattern. Since the DocumentIntegrationClient
is partial, you can create a new file and add a parameterized constructor.
public partial class DocumentIntegrationClient :
System.ServiceModel.ClientBase<ServiceReference1.IDocumentIntegration>, ServiceReference1.IDocumentIntegration
{
public DocumentIntegrationClient(IOptions<DocumentServiceOptions> options) : base()
{
if(options==null)
{
throw new ArgumentNullException(nameof(options));
}
this.ClientCredentials.Username.Username = options.Username;
this.ClientCredentials.Username.Password = options.Password;
this.Endpoint.Address = new EndpointAddress(options.BaseUrl);
}
}
And create a options class
public class DocumentServiceOptions
{
public string Username { get; set; }
public string Password { get; set; }
public string BaseUrl { get; set; }
}
and populate it from appsettings.json
.
services.Configure<DocumentServiceOptions>(Configuration.GetSection("MyService"));
这篇关于如何在ASP.Net Core中注入WCF服务客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!