StructureMap Exception Code: 202 No Default Instance defined for PluginFamily

為{幸葍}努か 提交于 2019-12-25 16:51:51

问题


I'm new to StuctureMap and I am writing a WCF service. In the existing code the previous developer used SturctureMap.

I get this error:

StructureMap Exception Code: 202
No Default Instance defined for PluginFamily MyCompany.SMS.Data.DataEntitys, MyCompany.SMS.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Here is the Code:

class a
{  method aa
    {
       var db = ObjectFactory.GetInstance<nsclaimsEntities>();
    }
}

When we are calling this code from an aspx.cs file it is working fine without any error, but I tried to wrap this code in a WCF webservice and it is throwing this exception.

Here is the stack trace:

at StructureMap.BuildSession.<.ctor>b__0(Type t)
at StructureMap.Util.Cache`2.get_Item(KEY key)
at StructureMap.BuildSession.CreateInstance(Type pluginType)
at StructureMap.Container.GetInstance(Type pluginType)
at StructureMap.Container.GetInstance[T]()
at StructureMap.ObjectFactory.GetInstance[PLUGINTYPE]()
at NicorNational.SMS.CustDemographic.GetByAccountNumber(String acctNum) in C:\\Projects\\NicorNational.SMS\\CustDemographic.cs:line 105
at NicorNational.Services.eCommerce.EligibilityService.GetEligibilityById(String accountId) in C:\\Projects\\Solutions\\NicorNational.Services.eCommerce\\EligibiltyService.svc.cs:line 23
at SyncInvokeGetEligibilityById(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)"

Why do I get this exception?


回答1:


As you've already found out, the problem migrating straight from a web application to a WCF service is where to place the bootstrapping code.

I just wanted to point out that you should move away from the service location behaviour and introduce dependency injection instead to have loose coupling and make your service easier to maintain/change.

Jimmy Bogard has written an excellent blog entry on how to bootstrap a WCF service using Structure Map. He uses a custom ServiceHostFactory instead of a static constructor to bootstrap the registry.




回答2:


I fixed this issue by adding static constructor to the WCF service class. Find this link. stack overflow previous link

public Service : IContract
{
    public Service(){ // regular constructor }
    static Service("Write your Configuration logic here"){ // Only called first time it's used. }
}


来源:https://stackoverflow.com/questions/4963623/structuremap-exception-code-202-no-default-instance-defined-for-pluginfamily

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!