Install-Package ServiceToController
# or
dotnet add package ServiceToController
using ServiceToController;
...
...
...
public class Startup
{
...
...
public void ConfigureServices(IServiceCollection services)
{
...
...
var builder = services.AddControllersWithViews().AddControllersAsServices();
builder.AddCastedService<MyService>();
}
...
...
}
public class MyService
{
public Task<MyObject> GetAll(MyObject obj)
{
return Task.FromResult(obj);
}
}
public class MyObject
{
public string Name { get; set; }
}
using ServiceToController;
...
...
...
public class Startup
{
...
...
public void ConfigureServices(IServiceCollection services)
{
...
...
var builder = services.AddControllersWithViews().AddControllersAsServices();
builder.AddCastedService(new CastOptions<WcfAutoGeneratedClient>
{
AddTestMethod = false,
ApiPath = "/api/WcfToRest",
UseNewInstanceEveryMethod = true,
CustomClassName = "WcfToRestController",
MethodNameRefactor = name => name.Replace("Async", ""),
MethodFilter = (
x => !x.Name.Contains("OpenAsync") &&
!x.Name.Contains("CloseAsync") &&
x.Name.Contains("Async") &&
x.IsPublic &&
(x.ReturnType == typeof(Task) || (x.ReturnType.IsGenericType && x.ReturnType.GetGenericTypeDefinition() == typeof(Task<>)))
),
CreateInstanceFunc = type => (WcfAutoGeneratedClient)Activator.CreateInstance(type, null, remoteAddress),
BeforeMethod = (client) =>
{
try
{
client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication =
new X509ServiceCertificateAuthentication()
{
CertificateValidationMode = X509CertificateValidationMode.None,
RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck
};
if (remoteAddress.Contains("https"))
{
client.ClientCredentials.UserName.UserName = "WS_USERNAME";
client.ClientCredentials.UserName.Password = "WS_PASSWORD";
}
client.OpenAsync().Wait();
}
catch { }
},
AfterMethod = (client, res) => { try { client.Close(); } catch { } return res; }
});
}
...
...
}