Skip to content

Latest commit

 

History

History
108 lines (95 loc) · 2.8 KB

Readme.md

File metadata and controls

108 lines (95 loc) · 2.8 KB

Install

Install-Package ServiceToController
# or
dotnet add package ServiceToController

Sample2

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

Sample3 - Wcf Auto Generated Proxy Service From WSDL To Controller

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