I have a class file: BandUtil.cs
public class BandUtil
{
private readonly IOptions<BandSettings> _bandOptions;
public BandUtil (IOptions<BandSettings> bandOptions)
{
_bandOptions= bandOptions;
}
public BandSettings BandValues()
{
return _bandOptions.Value;
}
public Client Client()
{
return new Client(BandValues().UserID, BandValues().ApiToken,BandValues().ApiSecret);
}
}
I have another class BandTest.cs
In this class BandTest.cs , I am trying to access Client() method from BandUtil class .
I tried multiple options, but not able to solve it. Can someone please guide me on this?
You need to either
change the constructor name to match the class name:
public BandUtil(IOptions<BandSettings> bandOptions)
or
give it a void
return type to make it a setter method instead:
public void BandWidthClient(IOptions<BandSettings> bandOptions)
If you take option 1, add this in your BandTest.cs
file:
var bandSettings = new BandSettings();
var bandOptions = new OptionsWrapper<BandSettings>(bandSettings);
var bandUtil = new BandUtil(bandOptions);
var client = bandUtil.Client();
If you take option 2, add this in your BandTest.cs
file:
var bandUtil = new BandUtil();
var bandSettings = new BandSettings();
var bandOptions = new OptionsWrapper<BandSettings>(bandSettings);
bandUtil.BandWidthClient(bandOptions);
var client = bandUtil.Client();
The first issue I see is that the constructor (BandWithClient) doesn't match the class name (BandUtil).
I am not sure what else is wrong but here is a quick and dirty recreation showing how to be able to use Client()
using System;
public class IOptions<T> {
public T Value { get; set; }
}
public class BandSettings {
public string UserID { get; set; }
public string ApiToken { get; set; }
public string ApiSecret { get; set; }
}
public class Client {
public Client(string userId, string apiToken, string secret) {
}
}
public class BandUtil
{
private readonly IOptions<BandSettings> _bandOptions;
public BandUtil (IOptions<BandSettings> bandOptions)
{
_bandOptions= bandOptions;
}
public BandSettings BandValues()
{
return _bandOptions.Value;
}
public Client Client()
{
return new Client(BandValues().UserID, BandValues().ApiToken, BandValues().ApiSecret);
}
}
public class Program
{
public static void Main()
{
var bandSettings = new BandSettings {
UserID = "Foo",
ApiToken = "Baz",
ApiSecret = "Bar"
};
var options = new IOptions<BandSettings> { Value = bandSettings };
var util = new BandUtil(options);
var client = util.Client();
}
}
Here is a dotnetfiddle with the code above: https://dotnetfiddle.net/srx9kM
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.