Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject Service Reference into .NET with AppSettings.json and Startup.cs

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.

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.

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.

enter image description here

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.

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

When attempting to process a test payment, this line of code throws an exception:

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

This is the error:

An Exception occurred while trying to process your payment. Please try again. Could not find endpoint element with name 'ITransactionProcessor' and contract 'CybersourceTrxnProcessor.ITransactionProcessor' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.

Here is what config file looks like, generated when I added the service reference to the project in Visual Studio (and also matches what's in an older MVC project):

<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>

This is the 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"
          }
    }
}

This is what I have in Startup.cs (also need to set the Security mode to TransportWithMessageCredential as prescribed by Cybersource docs):

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;
        });
like image 620
EHeine Avatar asked Jun 09 '17 15:06

EHeine


People also ask

How do I get Appsettings json value in startup CS?

In order to add AppSettings. json file, right click on the Project in Solution Explorer. Then click Add, then New Item and then choose App Settings File option (shown below) and click Add button. Once the File is created, it will have a DefaultConnection, below that a new AppSettings entry is added.

How do I use IConfiguration in .NET core?

The IConfiguration is an interface for . Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings.

How do you inject IConfiguration in net core 6?

You could add the IConfiguration instance to the service collection as a singleton object in ConfigureServices : public void ConfigureServices(IServiceCollection service) { services. AddSingleton<IConfiguration>(Configuration); //... }

How do I add Appsettings json in .NET core console app?

Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.


1 Answers

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"

to

"name": "ITransactionProcessor"
like image 124
EHeine Avatar answered Nov 15 '22 00:11

EHeine