问题
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.
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;
});
回答1:
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"
来源:https://stackoverflow.com/questions/44462093/inject-service-reference-into-net-with-appsettings-json-and-startup-cs