获得“对象是只读的".在WCF中设置ClientCredentials时出错 [英] Getting "Object is read only" error when setting ClientCredentials in WCF
问题描述
我有一个由Visual Studio(客户端)生成的名为ServerClient的代理对象.我正在尝试使用以下代码打开新连接之前设置ClientCredentials.UserName.UserName/Password:
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 connection using this code:
InstanceContext context = new InstanceContext(this);
m_client = new ServerClient(context);
m_client.ClientCredentials.UserName.UserName = "Sample";
代码一到达UserName行,它就会失败,并显示"Object is read-only"(对象为只读)错误.我知道如果连接已经打开或出现故障,就会发生这种情况,但是目前我还没有调用context.Open().
As soon as the code hits the UserName line it fails with an "Object is read-only" error. I know this can happen if the connection is already open or faulted, but at this point I haven't called context.Open() yet.
我已将Bindings(使用netTcpBinding)配置为使用Message作为其安全模式,并将MessageClientCredentialType设置为UserName.
I have configured the Bindings (which uses netTcpBinding) to use Message as it's security mode, and MessageClientCredentialType is set to UserName.
有什么想法吗?
推荐答案
似乎您只能在实例化周期的早期访问这些属性.如果在代理类(ServerClient)中重写构造函数,则可以设置以下属性:
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";
我开始感谢那些建议不要使用VS提供的自动构建的代理的人.
I'm beginning to appreciate the people who suggest not using the automatically built proxies provided by VS.
这篇关于获得“对象是只读的".在WCF中设置ClientCredentials时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!