简体   繁体   English

这两个代码应该等效吗? 以编程方式设置端点

[英]This two codes should be equivalent? Setting Endpoint programmatically

I'm consuming a WCF service not made by my company. 我使用的不是我公司提供的WCF服务。

I need to have my WCF settings in code (no .config file modification). 我需要在代码中包含WCF设置(无需修改.config文件)。

With that in mind I'm trying to use a method in a plain Project with the settings in Web.config (generated by Add Service Reference ) to test first. 考虑到这一点,我试图在普通项目中使用Web.config (由Add Service Reference生成)中的设置进行测试的方法。 It does work. 确实有效。

<system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="WSHttpBinding_IIntegracao" />
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://wcf.mydomain.com.br/Integracao.svc/Integracao.svc"
            binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IIntegracao"
            contract="ServiceReference1.IIntegracao" name="WSHttpBinding_IIntegracao">
            <identity>
                <userPrincipalName value="user@mydomain.com.br" />
            </identity>
        </endpoint>
    </client>
</system.serviceModel>

And I'm calling it like this: 我这样称呼它:

using (var client = new ServiceReference1.IntegracaoClient())
{
    var resultado = client.Incluir(new Inclusao());
}

I continue my task removing all settings from Web.config and could'nt manage to make it work. 我继续执行任务,从Web.config删除所有设置,并且无法使其正常运行。

I even tried to leave the Web.config untouched and just inform it in the client constructor like this: 我什至试图保持Web.config不变,只是在客户端构造函数中通知它,如下所示:

string url = ConfigurationManager.AppSettings["same url as above"];

var client2 = new IntegracaoClient(
            "WSHttpBinding_IIntegracao",
            new EndpointAddress(url));

var resultado2 = client2.Incluir(new Inclusao());

Didn't work 没工作

System.ServiceModel.Security.SecurityNegotiationException was unhandled HResult=-2146233087 Message=SOAP security negotiation with ' http://wcf.mydomain.com.br/Integracao.svc/Integracao.svc ' for target ' http://wcf.mydomain.com.br/Integracao.svc/Integracao.svc ' failed. 未处理System.ServiceModel.Security.SecurityNegotiationException HResult = -2146233087 Message =使用针对目标' http://wcf.mydomain的 ' http://wcf.mydomain.com.br/Integracao.svc/Integracao.svc '的SOAP安全协商.com.br / Integracao.svc / Integracao.svc '失败。 See inner exception for more details. 有关更多详细信息,请参见内部异常。

InnerException: System.ComponentModel.Win32Exception HResult=-2147467259 Message=Security Support Provider Interface (SSPI) authentication failed. InnerException:System.ComponentModel.Win32Exception HResult = -2147467259消息=安全支持提供程序接口(SSPI)身份验证失败。 The server may not be running in an account with identity 'host/wcf.mydomain.com.br'. 服务器可能未在标识为“ host / wcf.mydomain.com.br”的帐户中运行。 If the server is running in a service account (Network Service for example), specify the account's ServicePrincipalName as the identity in the EndpointAddress for the server. 如果服务器在服务帐户(例如,网络服务)中运行,请在服务器的EndpointAddress中将帐户的ServicePrincipalName指定为标识。 If the server is running in a user account, specify the account's UserPrincipalName as the identity in the EndpointAddress for the server. 如果服务器以用户帐户运行,请在服务器的EndpointAddress中将帐户的UserPrincipalName指定为标识。 Source=System.ServiceModel 源= System.ServiceModel

My question is: Is the second call equivalent to the first one? 我的问题是:第二个电话是否等于第一个电话?

AS I understand the second call says to the client to use the same configuration used in the first call. 据我了解,第二个电话告诉客户使用与第一个电话相同的配置。 Why does the second fail? 为什么第二次失败?

No, they are not equiivalent. 不,它们不相等。 In config you set the endpoint identity to some user, in code you don't do that. 在config中,您将端点标识设置为某个用户,而在代码中,则不这样做。

ServiceEndpoint ep = myServiceHost.AddServiceEndpoint(
                typeof(ICalculator),
                new WSHttpBinding(),
                String.Empty);
EndpointAddress myEndpointAdd = new EndpointAddress(new Uri("http://localhost:8088/calc"),
     EndpointIdentity.CreateDnsIdentity("contoso.com"));
ep.Address = myEndpointAdd;

Take a look at: https://msdn.microsoft.com/en-us/library/bb628618.aspx Programmatically set identity on WCF EndpointAddress 看看: https : //msdn.microsoft.com/zh-cn/library/bb628618.aspx以 编程方式在WCF EndpointAddress上设置身份

After @Jocke posted hist answer I elaborate more and could find the solution. @Jocke发布历史答案后,我会详细说明并找到解决方案。

Like he said the configuration has Identity in the config but when setting in the code it is the address that should take care of it. 就像他说的配置有身份config ,但在代码中设置时,这是一个应该照顾它的地址。

Since I couldn't find the same constructor as he said I needed some changes to make it apropriate for my code (Framework 4): 由于找不到与他说的相同的构造函数,因此我需要进行一些更改以使其适合我的代码(框架4):

var endpoint = new EndpointAddress(
    new Uri(ConfigurationManager.AppSettings["same url as above"]),
    EndpointIdentity.CreateUpnIdentity("user@domain.com.br"),
    (System.ServiceModel.Channels.AddressHeaderCollection)null);

var client3 = new ServiceReference1.IntegracaoClient(
    new WSHttpBinding(),
    endpoint);

This last constructor parameter casting null is just to clarify to the compiler whitch overload I wanted. 最后一个构造方法参数强制为null只是为了澄清我想要的编译器重载。 And I used CreateUpnIdentity instead. 我改用了CreateUpnIdentity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM