2
I need to create a WS with Ioc.
- I created a new Web project with Web API dependencies ;
- I added a Web Service (asmx);
- 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â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()