2016-09-27 1 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을 등록 할 아래 모양을 만들어? 내가 & 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 메서드에 과부하 인 factory 메서드를 사용하여 시도 했습니까? – Tseng

+0

그건 내가 생각한거야. AddScoped ..를 사용하려고했지만 구문을 알고 싶습니다. – LP13

답변

7

동안 설치 RemoteAddress하려는 것이 적합한 사용 사례 보인다. 당신의 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

관련 문제