2016-09-27 78 views
3

我有我需要从ASP.NET Core访问的WCF服务。我已经安装了WCF Connected Preview并成功创建了代理。如何在ASP.Net核心中注入WCF服务客户端?

它创建的接口&客户端类似下面

[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 
    } 

调用该服务的消费者类看起来像下面

public class Consumer 
{ 
    private IDocumentIntegration _client; 
    public Consumer(IDocumentIntegration client) 
    { 
    _client = client; 
    } 

    public async Task Process(string id) 
    { 
    await _client.SubmitDocumentAsync(id); 
    } 
} 

我如何注册在启动类ConfigureServices方法IDocumentIntegration? 我想设置RemoteAddress登记

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) 

    } 
+0

您是否尝试过使用AddXxx方法重载的工厂方法? – Tseng

+0

这就是我的想法。我试图使用AddScoped ..但我想知道语法? – LP13

回答

7

使用工厂方法过载期间& clientCredentials似乎合适的使用情况下它。

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; 
}); 

在您的AppSettings看起来像

{ 
    ... 
    "MyService" : 
    { 
     "Username": "guest", 
     "Password": "guest", 
     "BaseUrl": "http://www.example.com/" 
    } 
} 

另外,通过选择模式注入选项。由于DocumentIntegrationClient是局部的,因此您可以创建一个新文件并添加一个参数化的构造函数。

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); 
    } 
} 

,并创建一个选项类别

public class DocumentServiceOptions 
{ 
    public string Username { get; set; } 
    public string Password { get; set; } 
    public string BaseUrl { get; set; } 
} 

appsettings.json填充它。

services.Configure<DocumentServiceOptions>(Configuration.GetSection("MyService")); 
+0

感谢这正是我正在寻找。我的匿名函数语法出错 – LP13