Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要提供一种以编程方式连接到多个SOAP端点的方法_C#_Soap_Endpoints - Fatal编程技术网

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

C# 需要提供一种以编程方式连接到多个SOAP端点的方法,c#,soap,endpoints,C#,Soap,Endpoints,给定: 具有Service.asmx页面的多个客户端,其中包含一些我需要连接的web方法 一种实用函数,它接受输入以确定需要连接到哪个客户端。这就是我到目前为止所做的: public static MyServiceSoapClient SoapClientFactory(string clientCode) { var binding = new WSHttpBinding(SecurityMode.Message, false); var remoteAddress = ne

给定:
具有
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("myservice@domain.com"));

    return new MyServiceSoapClient(binding, remoteAddress);
}
此方法完成时没有任何错误(注意:我不知道EndPointIdentity是什么,所以我只是想出了一个字符串放在那里…不确定这是否应该在其他地方的配置设置中,或者是什么?)

问题:
尝试调用远程方法时,会引发异常,并且异常的描述性不强(A
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) )

我找到了问题的答案。我要做的是使用默认构造函数创建一个Soap客户机(这总是有效的,但是我在开发过程中使用了我配置的Uri),然后我要做的是设置
端点
地址
属性,它工作得很好

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;

这是
ex.ToString()
的全部结果吗?没有
内部异常
?此外,所有客户端都会发生这种情况吗?@John Saunders这是
ex.ToString()
的全部结果。另外,
ex.innerExction
null
。如果在工厂简化代码,会发生什么?如果你使用一个静态的,已知的好的URL呢?如果通过配置名称使用已配置的绑定,而不是创建一个绑定,该怎么办?这将有助于缩小问题的范围。当然,这显然是WCF中的一个bug-无论它未能取消引用什么对象,它都应该首先检查它,并且您应该收到一个
ArgumentNullException
。John,我现在已经解决了这个问题。谢谢你的帮助。