[BUG] Unable to cast object when initialising interface
Describe the bug What am I trying to do: Connect to an api and retrieve data.
In my Test Solution Project, which i have converted from .Net Framework to .Net 5, it gives me this error.
Unable to cast object of type 'UIHelpersRestClientRefitBugIWoofClient' to type 'UI.Helpers.RestClient.RefitBug.IWoofClient.
To diagnose, I've made a standalone project and this works perfectly (same code), so I know this may not be a bug or even a straightforward bug.
namespace UI.Helpers.RestClient.RefitBug
{
interface IWoofClient
{
[Refit.Get("/woof.json")]
Task<WoofResponseObject> GetDawg();
}
}
public WoofClient()
{
_baseUrl = new Uri("https://random.dog/");
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
{
//below is where the error occurs
IWoofClient = Refit.RestService.For<IWoofClient>(
new HttpClient()
{
BaseAddress = _baseUrl
});
}
}
As I mentioned, this works in the Test Solution that is using .Net Framework.
Environment
OS: Win 10 Visual Studio Pro 16.9.1 .Net 5 Package Version: 6.0.24
I'm going to need a more concrete repro for this
This is from Refit 6.0.24, whose client resolution was reflection/name-based and where exactly this kind of InvalidCastException could surface across assembly/visibility boundaries. That whole mechanism has since been rewritten: generated clients now register a factory keyed on the actual interface Type via RestService.RegisterGeneratedFactory (module initializer), with a Type.GetType fallback - see src/Refit/RestService.cs. The original "works in a standalone project, fails in the test solution" symptom is consistent with the old name-collision behavior and isn't reproducible against the current generator.
Closing as stale. If you can still reproduce on a current Refit release, please reopen with:
- a minimal repro repo,
- the exact Refit version,
- the full interface declaration (including its access modifier and namespace).
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.