从app.config获取ServiceHost实例

时间:2018-01-21 14:17:37

标签: c# wcf servicehost

可以获取app.config创建服务主机的实例吗?我有两个实例的A类,每个实例都有不同的参数,我需要将这两个实例作为服务主机托管在不同的网址上。

源代码:

public void RegisterService(object serviceInstance, params object[] args)
    {
        ServiceHost service = new ServiceHost(serviceInstance);
        service.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
        service.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new Common.AuthenticationValidator();

        if (serviceInstance is A)
        {
            if(args.Length > 0 && args[0] is ServiceId)
            {
                if((ServiceId)args[0] == ServiceId.Foo)
                {
                    WSHttpBinding binding = new WSHttpBinding();

                    service.AddServiceEndpoint(typeof(A), binding, "http://localhost/Foo");
                }
                else if((ServiceId)args[0] == ServiceId.Bar)
                {
                    WSHttpBinding binding = new WSHttpBinding();

                    service.AddServiceEndpoint(typeof(A), binding, "http://localhost/Bar");
                }
            }
        }

        service.Open();

        services.Add(service);
    }

如果我通过RegisterService注册它,我会收到错误400.我有什么遗漏吗?

0 个答案:

没有答案