Getting “Object is read only” error when setting ClientCredentials in WCF

后端 未结 5 893
迷失自我
迷失自我 2021-02-12 16:11

I have a proxy object generated by Visual Studio (client side) named ServerClient. I am attempting to set ClientCredentials.UserName.UserName/Password before opening up a new co

相关标签:
5条回答
  • 2021-02-12 16:54

    I have similar code that's passing UserName fine:

      FooServiceClient client = new FooServiceClient("BasicHttpBinding_IFooService");
      client.ClientCredentials.UserName.UserName = "user";
      client.ClientCredentials.UserName.Password = "password";
    

    Try creating the proxy with binding name in app.config.

    0 讨论(0)
  • 2021-02-12 16:56

    here is the solution:

    using SysSvcmod = System.ServiceModel.Description;
    
    SysSvcmod.ClientCredentials clientCredentials = new SysSvcmod.ClientCredentials();
    clientCredentials.UserName.UserName = "user_name";
    clientCredentials.UserName.Password = "pass_word";
    
    m_client.ChannelFactory.Endpoint.Behaviors.RemoveAt(1);
    m_client.ChannelFactory.Endpoint.Behaviors.Add(clientCredentials);
    
    0 讨论(0)
  • 2021-02-12 17:04

    I was facing same problem, my code started working when I changed my code i.e. assigning values to Client credential immediately after initializing Client object.

    here is the solution ,

    ProductClient Manager = new  ProductClient();    
    Manager.ClientCredentials.UserName.UserName = txtUserName.Text;
    Manager.ClientCredentials.UserName.Password = txtPassword.Text;
    
    0 讨论(0)
  • 2021-02-12 17:16

    I noticed that after creating an instance of the proxy class for the service, I can set the Username and Password once without errors and do a successful call to my webservice. When I then try to set the Username and Password again on the existing instance (unnecessary of course) I get the 'Object is Read-Only' error you mentioned. Setting the values once per instance lifetime worked for me.

    0 讨论(0)
  • 2021-02-12 17:16

    It appears that you can only access these properties pretty early in the instanciation cycle. If I override the constructor in the proxy class (ServerClient), I'm able to set these properties:

    base.ClientCredentials.UserName.UserName = "Sample";
    

    I'm beginning to appreciate the people who suggest not using the automatically built proxies provided by VS.

    0 讨论(0)
提交回复
热议问题