C# – StructureMap Exception Code: 202 No Default Instance defined for PluginFamily

cstructuremapwcf

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?

Best Answer

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.

Related Topic