本文介绍了如何在 ASP.Net 核心中注入 WCF 服务客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要从 ASP.NET Core 访问的 WCF 服务.我已经安装了 WCF Connected Preview 并成功创建了代理.>

它创建了界面 &客户端类似下面的内容

 [System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.3.0.0")][System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IDocumentIntegration")]公共接口 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.TaskSubmitDocumentAsync(string documentXml);}[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.3.0.0")]公共接口 IDocumentIntegrationChannel : ServiceReference1.IDocumentIntegration, System.ServiceModel.IClientChannel{}[System.Diagnostics.DebuggerStepThroughAttribute()][System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.3.0.0")]公共部分类 DocumentIntegrationClient : System.ServiceModel.ClientBase, ServiceReference1.IDocumentIntegration{//构造函数和方法在这里}

调用服务的消费者类如下图

公共类消费者{私人 IDocumentIntegration _client;公共消费者(IDocumentIntegration 客户端){_client = 客户端;}公共异步任务进程(字符串 id){等待 _client.SubmitDocumentAsync(id);}}

如何在 Startup 类中使用 ConfigureServices 方法注册 IDocumentIntegration?我想设置 RemoteAddress &注册期间的clientCredentials

 public void ConfigureServices(IServiceCollection services){services.AddApplicationInsightsTelemetry(Configuration);服务.AddMvc();//我如何在这里注入 DocumentIntegrationClient?var client = new DocumentIntegrationClient();client.ClientCredentials.UserName.UserName = "我的用户名";client.ClientCredentials.UserName.Password = "密码";client.Endpoint.Address = new EndpointAddress(urlbasedonenvironment)}
解决方案

使用工厂方法重载似乎适合它的用例.

services.AddScoped(provider => {var client = new DocumentIntegrationClient();//使用配置对象从 appconfig.json 中读取client.ClientCredentials.UserName.UserName = Configuration["MyService:Username"];client.ClientCredentials.UserName.Password = Configuration["MyService:Password"];client.Endpoint.Address = new EndpointAddress(Configuration["MyService:BaseUrl"]);回访客户;});

您的应用设置的样子

{...我的服务":{"用户名": "客人","密码": "客人","BaseUrl": "http://www.example.com/"}}

或者,通过选项模式注入选项.由于 DocumentIntegrationClient 是部分的,您可以创建一个新文件并添加参数化构造函数.

公共部分类 DocumentIntegrationClient :System.ServiceModel.ClientBase, ServiceReference1.IDocumentIntegration{public DocumentIntegrationClient(IOptions options) : base(){如果(选项==空){throw new ArgumentNullException(nameof(options));}this.ClientCredentials.Username.Username = options.Username;this.ClientCredentials.Username.Password = options.Password;this.Endpoint.Address = new EndpointAddress(options.BaseUrl);}}

并创建一个选项类

公共类 DocumentServiceOptions{公共字符串用户名 { 获取;放;}公共字符串密码{获取;放;}公共字符串 BaseUrl { 获取;放;}}

并从 appsettings.json 填充它.

services.Configure(Configuration.GetSection("MyService"));

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)

    }
解决方案

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

这篇关于如何在 ASP.Net 核心中注入 WCF 服务客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:30