gpt4 book ai didi

wcf - ChannelFactory Credentials + 对象是只读的

转载 作者:行者123 更新时间:2023-12-03 14:34:01 26 4
gpt4 key购买 nike

问候,当我尝试为我的工厂设置凭据时出现以下问题:

ChannelFactory<IWCFSeekService> factory = Factory;
if (factory != null)
{
factory.Credentials.UserName.UserName = CServiceCredentials.Instance.Username;
_Channel = factory.CreateChannel();
}

我得到一个对象是只读的异常。当我想设置用户名时会发生这种情况。

最佳答案

是的,MSDN documentation很清楚:

C#
public ClientCredentials Credentials { get; }

楼盘 仅限 有一个 get访问器 - 没有设置访问器 -> 它是只读的。

同样在 MSDN 文档中:

Remarks
The ClientCredentials object is stored as a type of endpoint behavior and can be accessed through the Behaviors property.

The OnOpened method initializes a read-only copy of the ClientCredentials object for the factory.



那你在这里做什么??

更新:您无法设置客户端代理应该在 channel 工厂上使用的用户凭据。看到这个 excellent blog post关于如何做 - 有点绕道:
  • 首先,从工厂中删除默认端点行为
  • 其次,实例化您自己的凭据
  • 第三,将这些新凭据设置为工厂的新端点行为
    // step one - find and remove default endpoint behavior 
    var defaultCredentials = factory.Endpoint.Behaviors.Find<ClientCredentials>();
    factory.Endpoint.Behaviors.Remove(defaultCredentials);

    // step two - instantiate your credentials
    ClientCredentials loginCredentials = new ClientCredentials();
    loginCredentials.UserName.UserName = CServiceCredentials.Instance.Username;
    loginCredentials.UserName.Password = “Password123″;

    // step three - set that as new endpoint behavior on factory
    factory.Endpoint.Behaviors.Add(loginCredentials); //add required ones

  • 似乎有点奇怪和复杂,但这似乎是实现这一目标的唯一方法!

    关于wcf - ChannelFactory Credentials + 对象是只读的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2397733/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com