需要提供一种以编程方式连接到多个 SOAP 端点的方法

发布于 2024-11-27 22:47:38 字数 4238 浏览 3 评论 0原文

给定:
具有 Service.asmx 页面的多个客户端,其中包含一些我需要连接到的 Web 方法。

一个实用程序函数,它接受输入以确定需要连接到哪个客户端。这是我到目前为止所拥有的:

public static MyServiceSoapClient SoapClientFactory(string clientCode)
{
    var binding = new WSHttpBinding(SecurityMode.Message, false);
    var remoteAddress = new EndpointAddress(
        new Uri(string.Format("http://{0}.mydomain.com/Service.asmx", clientCode)),
        new UpnEndpointIdentity("[email protected]"));

    return new MyServiceSoapClient(binding, remoteAddress);
}

此方法完成时没有错误(注意:我不知道 EndPointIdentity 是什么,所以我只是想出了一个字符串放入其中...不确定这是否应该在配置中设置在其他地方还是什么?)

问题:
尝试调用远程方法时,会引发异常,并且其描述性不强(NullReferenceException)。这是代码:

// point to the correct Service.asmx url.
var client = Util.SoapClientFactory(clientCode);

// now make the soap call.
var result = client.GetSomeStuff(someParameter); // throws the exception.

堆栈跟踪:

System.NullReferenceException: Object reference not set to an instance of an object.
    Server stack trace:
    at System.ServiceModel.Security.IssuanceTokenProviderBase`1.DoNegotiation(TimeSpan timeout)
    at System.ServiceModel.Security.SspiNegotiationTokenProvider.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Security.CommunicationObjectSecurityTokenProvider.Open(TimeSpan timeout)
    at System.ServiceModel.Security.SecurityUtils.OpenTokenProviderIfRequired(SecurityTokenProvider tokenProvider, TimeSpan timeout)
    at System.ServiceModel.Security.SymmetricSecurityProtocol.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Channels.SecurityChannelFactory`1.ClientSecurityChannel`1.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Security.SecuritySessionSecurityTokenProvider.DoOperation(SecuritySessionOperation operation, EndpointAddress target, Uri via, SecurityToken currentToken, TimeSpan timeout)
    at System.ServiceModel.Security.SecuritySessionSecurityTokenProvider.GetTokenCore(TimeSpan timeout)
    at System.IdentityModel.Selectors.SecurityTokenProvider.GetToken(TimeSpan timeout)
    at System.ServiceModel.Security.SecuritySessionClientSettings`1.ClientSecuritySessionChannel.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
    at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)    Exception rethrown at [0]:
    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
    at ... my function call. (i.e. client.GetSomeStuff(someParameter) )

Given:
Multiple clients with a Service.asmx page that has some web methods that I need to connect to.

A Utility function that takes input to determine which client needs to be connected to. This is what I have so far:

public static MyServiceSoapClient SoapClientFactory(string clientCode)
{
    var binding = new WSHttpBinding(SecurityMode.Message, false);
    var remoteAddress = new EndpointAddress(
        new Uri(string.Format("http://{0}.mydomain.com/Service.asmx", clientCode)),
        new UpnEndpointIdentity("[email protected]"));

    return new MyServiceSoapClient(binding, remoteAddress);
}

This method completes with no errors (note: I don't know what the EndPointIdentity is so I just came up with a string to put in there... not sure if this should be in a config setting somewhere else or what?)

Problem:
While trying to make the call to the remote method, an exception is thrown and its not very descriptive (A NullReferenceException). Here's the code:

// point to the correct Service.asmx url.
var client = Util.SoapClientFactory(clientCode);

// now make the soap call.
var result = client.GetSomeStuff(someParameter); // throws the exception.

The stack trace:

System.NullReferenceException: Object reference not set to an instance of an object.
    Server stack trace:
    at System.ServiceModel.Security.IssuanceTokenProviderBase`1.DoNegotiation(TimeSpan timeout)
    at System.ServiceModel.Security.SspiNegotiationTokenProvider.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Security.CommunicationObjectSecurityTokenProvider.Open(TimeSpan timeout)
    at System.ServiceModel.Security.SecurityUtils.OpenTokenProviderIfRequired(SecurityTokenProvider tokenProvider, TimeSpan timeout)
    at System.ServiceModel.Security.SymmetricSecurityProtocol.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Channels.SecurityChannelFactory`1.ClientSecurityChannel`1.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Security.SecuritySessionSecurityTokenProvider.DoOperation(SecuritySessionOperation operation, EndpointAddress target, Uri via, SecurityToken currentToken, TimeSpan timeout)
    at System.ServiceModel.Security.SecuritySessionSecurityTokenProvider.GetTokenCore(TimeSpan timeout)
    at System.IdentityModel.Selectors.SecurityTokenProvider.GetToken(TimeSpan timeout)
    at System.ServiceModel.Security.SecuritySessionClientSettings`1.ClientSecuritySessionChannel.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
    at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
    at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
    at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
    at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)    Exception rethrown at [0]:
    at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
    at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
    at ... my function call. (i.e. client.GetSomeStuff(someParameter) )

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

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

发布评论

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

评论(1

孤独陪着我 2024-12-04 22:47:38

我找到了问题的答案。我所做的是使用默认构造函数创建一个 Soap 客户端(这总是有效,但后来我坚持使用我在开发过程中配置的 Uri)然后我要做的是设置 Endpoint 的 < code>Address 属性它就像一个魅力!

var client = new MySoapClient();

// this can also be set using Web.config parameter now or a value stored in a database etc.
client.Endpoint.Address = new EndpointAddress("http://localhost:3000/Service.asmx");
return client;

I found the answer to my problem. What I do is I create a Soap client using the default constructor (this always works but then im stuck with the Uri that I've configured during development) What I do then is set the Endpoint's Address property it worked like a charm!

var client = new MySoapClient();

// this can also be set using Web.config parameter now or a value stored in a database etc.
client.Endpoint.Address = new EndpointAddress("http://localhost:3000/Service.asmx");
return client;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文