Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject WCF service client in ASP.Net core?

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)

    }
like image 849
LP13 Avatar asked Sep 27 '16 18:09

LP13


People also ask

Can we call WCF service from .NET Core?

To use WCF services in . NET Core, you need to create a proxy client of the required service. Proxy is actually a contract equivalent of actual service and contains complete details of the Interface and method exposed by WCF service. One can also use the Channel factory technique to connect to the WCF service easily.

How do you call a WCF service reference in C#?

With the service running, right click the project that will contain the WCF client proxy and select Add > Service Reference. In the Add Service Reference Dialog, type in the URL to the service you want to call and click the Go button. The dialog will display a list of services available at the address you specify.

Is gRPC faster than WCF?

GRPC is better than WCF in terms of performance.


1 Answers

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"));
like image 135
Tseng Avatar answered Oct 05 '22 17:10

Tseng