Ioc with Web Service

Asked

Viewed 121 times

2

I need to create a WS with Ioc.

  1. I created a new Web project with Web API dependencies ;
  2. I added a Web Service (asmx);
  3. I installed the Simpleinjector;

The code went like this :

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class WSI : System.Web.Services.WebService
    {

            private readonly ICrypto _crypto;

            public WSI(ICrypto crypto)
            {
                _crypto = crypto;
            }

            [WebMethod]
            public string HelloWorld()
            {
                return _crypto.Encrypt("Hello World");
            }
    }

Simpleinjector startup file is as follows :

 public static void Initialize()
    {
        var container = new Container();
        container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();

        InitializeContainer(container);

        container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

        container.Verify();

        GlobalConfiguration.Configuration.DependencyResolver =
            new SimpleInjectorWebApiDependencyResolver(container);
    }

    private static void InitializeContainer(Container container)
    {
        container.Register<ICrypto, Crypto>();
    }

When I run I get the error :

System.MissingMethodException: Nenhum construtor sem par&#226;metros foi definido para este objeto.
em System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
em System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
em System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
em System.Activator.CreateInstance(Type type, Boolean nonPublic)
em System.Activator.CreateInstance(Type type)
em System.Web.Services.Protocols.ServerProtocol.CreateServerInstance()
em System.Web.Services.Protocols.WebServiceHandler.Invoke()
em System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

1 answer

2


If I understand your problem, it must be because Webservice(asmx) does not allow constructors, because its implementation is via proxy (the one that is added as a reference to the Service in the client) that is replaced.

If it is really necessary to use the Simpleinjector recommend changing the approach to WCF.

More information: Codeproject

I hope I’ve helped in some way. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.