2016-09-27 83 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