ChannelFactory 凭证 +对象是只读的

发布于 2024-08-24 08:53:31 字数 306 浏览 6 评论 0原文

您好,当我尝试按如下方式设置工厂凭据时,问题是什么:

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

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

Greetings, what is the problem that when I try to set credentials for my factory as follows:

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

I get an exception that object is read-only. It occurs when I want to set username.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空气里的味道 2024-08-31 08:53:31

是的,MSDN 文档非常清楚:

C#
public ClientCredentials Credentials { get; }

该属性有一个get访问器 - 无set访问器 -->它是只读的。

另外在 MSDN 文档中:

备注
存储 ClientCredentials 对象
作为一种端点行为,可以
通过行为访问
属性。

OnOpened 方法初始化一个
的只读副本

ClientCredentials 对象
工厂。

那你来这里做什么?

更新:您无法设置客户端代理应该在通道工厂上使用的用户凭据。请参阅此 关于如何做到这一点的优秀博客文章 - 有点绕道:

  • 首先,从工厂中删除默认端点行为,
  • 其次,实例化您自己的凭据
  • 第三,将这些新凭据设置为新凭据工厂中的端点行为

    // 第一步 - 查找并删除默认端点行为 
    var defaultCredentials = factory.Endpoint.Behaviors.Find();
    工厂.Endpoint.Behaviors.Remove(defaultCredentials); 
    
    // 第二步 - 实例化您的凭证
    ClientCredentials 登录凭据 = new ClientCredentials();
    loginCredentials.UserName.UserName = CServiceCredentials.Instance.Username;
    loginCredentials.UserName.Password = “密码123”;
    
    // 第三步 - 将其设置为工厂中的新端点行为
    工厂.Endpoint.Behaviors.Add(loginCredentials); //添加需要的
    

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

Yes, the MSDN documentation is pretty clear:

C#
public ClientCredentials Credentials { get; }

The property only has a get accessor - no set accessor --> it's readonly.

Also in the MSDN docs:

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.

So what is it you're doing to do here??

UPDATE: you cannot set the user credentials that your client proxy is supposed to use on the channel factory. See this excellent blog post on how to do it anyway - with a bit of a detour:

  • first, remove the default endpoint behavior from the factory
  • secondly, instantiate your own credentials
  • thirdly, set those new credentials as new endpoint behavior on factory

    // 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
    

Seems a bit odd and complicated, but that seems to be the one and only way to achieve this!

独夜无伴 2024-08-31 08:53:31

为了完成这个答案,它对每个人都起作用的实际方式,如

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/4668e261-0fd0-4ca5-91d2-497aa479f2a9/

您不需要删除,而是覆盖找到凭据:

var credentialBehaviour = factory.Endpoint.Behaviors.Find < ClientCredentials > ();
credentialBehaviour.UserName.UserName = "test";
credentialBehaviour.UserName.Password = "test";

这解决了我的问题。

To complete this answer, the actual way in which it worked for everyone as explained at

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/4668e261-0fd0-4ca5-91d2-497aa479f2a9/

You need not to remove, but override found credentials:

var credentialBehaviour = factory.Endpoint.Behaviors.Find < ClientCredentials > ();
credentialBehaviour.UserName.UserName = "test";
credentialBehaviour.UserName.Password = "test";

This has solved my problem.

但可醉心 2024-08-31 08:53:31

如果通过->添加服务引用则不会发生这种情况。添加服务引用->高级->添加Web引用-> Url/wsdl(本地磁盘文件)。

生成的 reference.cs 文件有所不同,并且允许您设置凭据。
该错误是因为您可能已通过第一个屏幕本身添加了引用(添加服务引用)

This will not happen if the service reference is added through -> Add service reference ->Advanced->Add Web Reference-> Url/wsdl (local disk file).

The reference.cs file generated is different and will allow you to set credentials.
The error is because you might have added the reference through first screen itself (Add service reference)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文