使用AppSettings.json和Startup.cs将服务引用注入.NET [英] Inject Service Reference into .NET with AppSettings.json and Startup.cs

查看:139
本文介绍了使用AppSettings.json和Startup.cs将服务引用注入.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目在运行时找不到服务引用端点。我认为这是由于Startup.cs中的注入错误所致。我是appsettings.json和Startup.cs配置方法的新手,但已成功在Startup.cs中确定了我的类库和Dbcontext的范围。

My project is not finding the service reference endpoint in runtime. I believe it's due to incorrect injection in my Startup.cs. I'm new to the appsettings.json and Startup.cs method of configuration but have successfully scoped my class library and Dbcontext in the Startup.cs.

注意,如果有所作为, 此VS解决方案包含一个类库和一个.NET / angular2网络项目。对服务的调用是从有角度的网站启动到Web API,Web API会在发生实际处理的类库上调用方法。

Note, if it makes a difference, this VS solution contains a class library and a .NET/angular2 web project. The call to the Service is initiated from angular website to the Web API, which calls methods on the class library where actual processing occurs.

服务引用 Cyber​​sourceTrxnProcessor显示在我的类库项目中(参见图片),并且ITransactionProcessor可以访问并且可以访问(即,代码提示工作正常)。 Web项目在解决方案资源管理器中没有服务参考。

The service reference "CybersourceTrxnProcessor" shows up in my class library project (see image) and ITransactionProcessor is exposed and accessible (i.e. code-hinting working perfectly). The web project DOES NOT have the service reference in the solution explorer.

当我添加参考时,这些部分是添加到app.config文件(见下文)中,然后将它们复制到Web项目中的web.config中。

When I added the reference, the sections were added to the app.config file (see below) and I copied them to the web.config in the web project.

如何在appsettings和Startup中重新创建 web.config设置?

How do I 'recreate' the web.config settings in the appsettings and Startup?

尝试处理测试付款时,以下代码行引发异常:

TransactionProcessorClient proc = new TransactionProcessorClient("ITransactionProcessor");

我也尝试过手动定义端点,但之前的错误结果相同:

I have also tried defining the endpoint manually just prior but the same error results:

 System.ServiceModel.EndpointAddress theendpoint =  new System.ServiceModel.EndpointAddress("https://ics2wstesta.ic3.com/commerce/1.x/transactionProcessor");
 TransactionProcessorClient proc = new TransactionProcessorClient("ITransactionProcessor", theendpoint);

这是错误:

尝试处理您的付款时发生异常。请再试一次。在ServiceModel客户端配置部分中找不到名称为 ITransactionProcessor且合同为 Cyber​​sourceTrxnProcessor.ITransactionProcessor的终结点元素。这可能是因为找不到应用程序的配置文件,或者是因为在客户端元素中找不到与该名称匹配的终结点元素。

这是当我在Visual Studio中将服务引用添加到项目中时生成的配置文件的样子(并且与旧的MVC项目中的内容匹配):

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
          <binding name="ITransactionProcessor">
             <security mode="TransportWithMessageCredential" />
           </binding>
           <binding name="ITransactionProcessor1" />
      </basicHttpBinding>
   </bindings>
   <client>
         <endpoint address="https://ics2wstesta.ic3.com/commerce/1.x/transactionProcessor" binding="basicHttpBinding" bindingConfiguration="ITransactionProcessor"
    contract="CybersourceTrxnProcessor.ITransactionProcessor" name="portXML" />
   </client>
</system.serviceModel>

这是appsettings.json:

"ITransactionProcessor": {
    "security": { "mode": "TransportWithMessageCredential" },
    "client": {
         "endpoint": {
             "address": "https://ics2wstesta.ic3.com/commerce/1.x/transactionProcessor",
             "binding": "basicHttpBinding",
             "bindingConfiguration": "ITransactionProcessor",
             "contract": "CybersourceTrxnProcessor.ITransactionProcessor",
             "name": "portXML"
          }
    }
}

这就是我在Startup.cs中的内容(还需要将安全性模式设置为Cyber​​source文档规定的TransportWithMessageCredential):

services.AddScoped<ITransactionProcessor>(provider => {
            var client = new TransactionProcessorClient();               
            client.Endpoint.Address = new EndpointAddress(Configuration["ITransactionProcessor:client:endpoint:address"]);
            client.Endpoint.Contract = new System.ServiceModel.Description.ContractDescription(Configuration["ITransactionProcessor:client:endpoint:contract"]);
            client.Endpoint.Binding = new System.ServiceModel.BasicHttpBinding();
            client.Endpoint.Name = "portXML";   
            return client;
        });


推荐答案

仅供参考,我终于明白了。除了一件很小的事情(不是总归结为简单的事情),我的一切都是正确的。该错误实际上告诉我确切需要什么。我只需要像这样更改我的appsettings.json:

Just FYI, I finally figured this out. Everything I had was correct except ONE tiny thing (doesn't it almost always come down to something simple). The error actually told me exactly what it needed. I simply needed to change my appsettings.json like so:

"name": "portXML"

"name": "ITransactionProcessor"

这篇关于使用AppSettings.json和Startup.cs将服务引用注入.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆