2016-09-27 5 views
3

ASP.NET Coreからアクセスする必要があるWCFサービスがあります。 WCF Connected Previewがインストールされ、プロキシが正常に作成されました。ASP.NetコアにWCFサービスクライアントを挿入する方法は?

それはサービスを呼び出すコンシューマークラスは、私はスタートアップクラスのConfigureServicesメソッドでIDocumentIntegrationを登録するにはどうすればよい

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

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

以下のように見える?

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

以下のようなインターフェース&クライアント何かを作成 私は& clientCredentialsファクトリメソッドのオーバーロードを使用して登録

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

時のセットアップ[リモートにしたい、それに適したユースケースはそうです。あなたのAppSettingsは、また

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

のように見えるのオプションパターンを介してオプションを注入う

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

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

関連する問題