c# - Why can't service host from Unity.Wcf resolve service's instance correctly? -


this question has answer here:

i have unity container:

var unitycontainer = new unitycontainer(); 

configured follows:

unitycontainer.registertype<iexampledomainservice, exampledomainservice>();  unitycontainer.registertype<iexamplewebservice, examplewebservice>(); 

examplewebservice type , constructor follows:

[servicebehavior(instancecontextmode = instancecontextmode.single)] public class examplewebservice {     public examplewebservice(iexampledomainservice exampledomainservice)     {         this.exampledomainservice = exampledomainservice;     }  // ... 

and exampledomainservice has no constructor defined (the problem same when explicitly define parameterless constructor type).

next, explained in unity.wcf's documentation:

if hosting wcf service within windows service using servicehost, replace servicehost instance custom unity.wcf.unityservicehost. find unityservicehost takes in unity container first parameter otherwise identical default servicehost.

i following:

 var host = new unityservicehost(unitycontainer, typeof(examplewebservice), baseaddress); 

this throws system.invalidoperationexception following message:

the service type provided not loaded service because not have default (parameter-less) constructor. fix problem, add default constructor type, or pass instance of type host.

looking @ unityservicehost implementation @ github passes given servicetype (typeof(examplewebservice) in case) directly wcf's native servicehost:

public sealed class unityservicehost : servicehost {     public unityservicehost(iunitycontainer container, type servicetype, params uri[] baseaddresses)       : base(servicetype, baseaddresses)              ^^^^^^^^^^^              ??????????? 

which crashes, servicehost not know unity , container , cannot cope when parameterless constructor missing.

is unity.wcf broken non-was/non-iis hosting or (i hope) doing wrong?

i sorry, can not tell why have exception, hope you.

  1. servicehost not need know unity. di done via implementing iinstanceprovider , registering each service behavior
  2. it work me, here basic application work correctly, maybe can find what's problem on side

    public class program {     static void main(string[] args)     {         var container = new unitycontainer();         container.registertype<idependency, dependency>();         container.registertype<ihelloworldservice, helloworldservice>();         uri baseaddress = new uri("http://localhost:8080/hello");         using (servicehost host = new unityservicehost(container, typeof(helloworldservice), baseaddress))         {             servicemetadatabehavior smb = new servicemetadatabehavior();             smb.httpgetenabled = true;             smb.metadataexporter.policyversion = policyversion.policy15;             host.description.behaviors.add(smb);              host.open();              console.writeline("the service ready @ {0}", baseaddress);             console.writeline("press <enter> stop service.");             console.readline();              host.close();         }     } }  [servicecontract] public interface ihelloworldservice {     [operationcontract]     string sayhello(string name); }  public interface idependency {  }  public class dependency : idependency { }  public class helloworldservice : ihelloworldservice {     private readonly idependency _dependency;      public helloworldservice(idependency dependency)     {         _dependency = dependency;     }      public string sayhello(string name)     {         return string.format("hello, {0}", name);     } } 
  3. wcf not call instanceprovider if instancecontextmode.single used. that's why unity.wcf not work it. detailed answer can found here.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -