使用unity Dependency Injection Container在MVC项目中设置WCF ClientCredentials

时间:2013-06-20 13:14:34

标签: c# wcf dependency-injection unity-container

我想知道你是否认为这种或类似的东西可以统一?我想为我在我的解决方案项目中使用的WCF服务引用OrderServiceClientClient()注入用户名和密码凭据。

container.RegisterType<IOrderServiceClient, OrderServiceClientClient>(
   new InjectionConstructor(
      new InjectionProperty("ClientCredentials.UserName.UserName", "test"), 
      new InjectionProperty("ClientCredentials.UserName.Password", "password")));

是这样的吗?

编辑:

查看服务引用,该类的定义看起来像这样:

class ProductMasterServiceClientClient : System.ServiceModel.ClientBase<com.luzern.co40.web.pm.wsProductMasterService.IProductMasterServiceClient>, com.luzern.co40.web.pm.wsProductMasterService.IProductMasterServiceClient

父类ClientBase类包含ClientCredential成员的定义..

我不想替换整个ClientCredential会员,我只想在其上设置2个属性的用户名和密码!

编辑2 !!

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class OrderServiceClientClient : System.ServiceModel.ClientBase<com.luzern.co40.web.pm.wsOrderService.IOrderServiceClient>, com.luzern.co40.web.pm.wsOrderService.IOrderServiceClient {

    public OrderServiceClientClient() {
    }

    public OrderServiceClientClient(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }

    public OrderServiceClientClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public OrderServiceClientClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public OrderServiceClientClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

    public bool GenerateInvoice(int orderId, string email) {
        return base.Channel.GenerateInvoice(orderId, email);
    }

    public System.Threading.Tasks.Task<bool> GenerateInvoiceAsync(int orderId, string email) {
        return base.Channel.GenerateInvoiceAsync(orderId, email);
    }
}

1 个答案:

答案 0 :(得分:1)

好的我有一个修复..我将创建我的服务引用的部分类并添加一个新的构造函数,它将ClientCredential的登录凭据作为参数,然后我可以只注入正常的字符串参数!