我正在尝试将 Unity 配置从代码移动到 Unity.config XML 文件,但由于某种原因,我无法将 WCF 服务注册到容器(老实说,我只是不知道如何)。
这是完美运行的代码:
var httpBinding = new BasicHttpBinding();
var endpointAddress = new EndpointAddress(serverIP);
var container = new UnityContainer();
container.RegisterType<IMyService, MyServiceClient>
(new ContainerControlledLifetimeManager(), new InjectionConstructor(new ResolvedParameter<BasicHttpBinding>("httpBinding"),
new ResolvedParameter<EndpointAddress>("endpointAddress")));
这是我在 Unity.config 中的内容:
<typeAliases>
<typeAlias alias="IMyService" type="MyService.IMyService,MyService" />
<typeAlias alias="MyServiceClient" type="MyService.MyServiceClient,MyService" />
</typeAliases>
<containers>
<container>
<type name="httpBinding" type="System.ServiceModel.BasicHttpBinding, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<constructor>
<param name="configurationName">
<value value="MyService" />
</param>
</constructor>
</type>
<type name="endpointAddress" type="System.ServiceModel.EndpointAddress, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" />
<register type="IMyService" mapTo="MyServiceClient" />
</container>
</containers>
显然这是行不通的。我得到的错误是: 类型名称或别名 IMyService 无法解析。请检查您的配置文件并验证此类型名称。