Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.Net内核中注入WCF服务客户端?_C#_Asp.net Core_.net Core_Coreclr - Fatal编程技术网

C# 如何在ASP.Net内核中注入WCF服务客户端?

C# 如何在ASP.Net内核中注入WCF服务客户端?,c#,asp.net-core,.net-core,coreclr,C#,Asp.net Core,.net Core,Coreclr,我需要从ASP.NET Core访问WCF服务。我已成功安装并创建了代理 它创建了界面和客户端,如下所示 [System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "0.3.0.0")] [System.ServiceModel.ServiceContractAttribute(ConfigurationName="ServiceReference1.IDocumentIntegration")]

我需要从ASP.NET Core访问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);
  }
} 
如何在Startup类中使用ConfigureServices方法注册IDocumentIntegration? 我想在注册期间设置RemoteAddress&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)

    }

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

services.AddScoped(提供者=>{
var client=new DocumentIntegrationClient();
//使用配置对象从appconfig.json读取它
client.ClientCredentials.UserName.UserName=配置[“MyService:UserName”];
client.ClientCredentials.UserName.Password=配置[“MyService:Password”];
client.Endpoint.Address=新的端点地址(配置[“MyService:BaseUrl”]);
返回客户;
});
您的应用程序设置看起来像什么

{
...
“我的服务”:
{
“用户名”:“来宾”,
“密码”:“来宾”,
“BaseUrl”:http://www.example.com/"
}
}
或者,通过选项模式注入选项。由于
DocumentIntegrationClient
是部分的,因此可以创建新文件并添加参数化构造函数

公共部分类DocumentIntegrationClient:
System.ServiceModel.ClientBase,ServiceReference1.IDocumentIntegration
{
公共文档集成客户端(IOptions选项):base()
{
如果(选项==null)
{
抛出新ArgumentNullException(nameof(options));
}
this.ClientCredentials.Username.Username=options.Username;
this.ClientCredentials.Username.Password=options.Password;
this.Endpoint.Address=新的EndpointAddress(options.BaseUrl);
}
}
并创建一个选项类

公共类DocumentServiceOptions
{
公共字符串用户名{get;set;}
公共字符串密码{get;set;}
公共字符串BaseUrl{get;set;}
}
并从
appsettings.json
中填充它

services.Configure(Configuration.GetSection(“MyService”);

您是否尝试过使用factory方法来添加XXX方法?我就是这么想的。我试图使用AddScope。。但是我想知道语法?谢谢这正是我想要的。我的匿名函数Syntax中出现错误。使用此服务的正确方式是什么?似乎我们应该使用临时服务,因为这将允许WCF服务代理在连接管理方面做自己的事情。只是不确定在代理上调用close/abort时应该是什么样子。