连接到 Web 服务时的系统空引用

SystemNullReference while connecting to WebService

我在 2015 年 Visual Studio 之前创建了一个服务参考。通信工作正常,但我必须获得没有 app.config 文件的应用程序。我尝试创建自己的绑定和端点,但实际上我收到了 SystemNullReference。

我的代码示例:

    var binding = CreateBinding();
    var endpoint = new EndpointAddress("http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx");
    var Client = new Testowy.Emailver.EmailVerNoTestEmailSoapClient(binding, endpoint );

private static BasicHttpBinding CreateBinding()
        {
            var binding = new BasicHttpBinding();
            binding.Name = "EmailVerNoTestEmailSoap";
            binding.CloseTimeout = TimeSpan.FromMinutes(1);
            binding.OpenTimeout = TimeSpan.FromMinutes(1);
            binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
            binding.SendTimeout = TimeSpan.FromMinutes(1);
            binding.AllowCookies = false;
            binding.BypassProxyOnLocal = false;
            binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            binding.MaxBufferSize = 65536;
            binding.MaxBufferPoolSize = 524288;
            binding.MessageEncoding = WSMessageEncoding.Text;
            binding.TextEncoding = System.Text.Encoding.UTF8;
            binding.TransferMode = TransferMode.Buffered;
            binding.UseDefaultWebProxy = true;

            binding.ReaderQuotas.MaxDepth = 32;
            binding.ReaderQuotas.MaxStringContentLength = 8192;
            binding.ReaderQuotas.MaxArrayLength = 16384;
            binding.ReaderQuotas.MaxBytesPerRead = 4096;
            binding.ReaderQuotas.MaxNameTableCharCount = 16384;

            binding.Security.Mode = BasicHttpSecurityMode.None;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
            binding.Security.Transport.Realm = "";
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            binding.Security.Message.AlgorithmSuite = System.ServiceModel.Security.SecurityAlgorithmSuite.Default;
            return binding;
        }

System.NullReferenceException: Object reference not set to an instance of an object.

Server stack trace: w System.ServiceModel.Security.IssuanceTokenProviderBase1.DoNegotiation(TimeSpan timeout) w System.ServiceModel.Security.SspiNegotiationTokenProvider.OnOpen(TimeSpan timeout) w System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) w System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) w System.ServiceModel.Security.CommunicationObjectSecurityTokenProvider.Open(TimeSpan timeout) w System.ServiceModel.Security.SymmetricSecurityProtocol.OnOpen(TimeSpan timeout) w System.ServiceModel.Security.WrapperSecurityCommunicationObject.OnOpen(TimeSpan timeout) w System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) w System.ServiceModel.Channels.SecurityChannelFactory1.ClientSecurityChannel1.OnOpen(TimeSpan timeout) w System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) w System.ServiceModel.Security.SecuritySessionSecurityTokenProvider.DoOperation(SecuritySessionOperation operation, EndpointAddress target, Uri via, SecurityToken currentToken, TimeSpan timeout) w System.ServiceModel.Security.SecuritySessionSecurityTokenProvider.GetTokenCore(TimeSpan timeout) w System.IdentityModel.Selectors.SecurityTokenProvider.GetToken(TimeSpan timeout) w System.ServiceModel.Security.SecuritySessionClientSettings1.ClientSecuritySessionChannel.OnOpen(TimeSpan timeout) w System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) w System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
w System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) w System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout) w System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade) w System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout) w System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) w System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) w System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: w System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) w System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) w WebServices.Testowy.Emailver.EmailVerNoTestEmailSoap.AdvancedVerifyEmail(String email, Int32 timeout, String LicenseKey) w WebServices.Test.GetWebService() w C:\Users\michal.warchulinski\Source\Repos\Aplixcom_CommonComponents\Aplixcom_CommonComponents\WebServices\ClientClasses.cs:wiersz 65 w App.Main() w C:\Users\michal.warchulinski\Documents\Visual Studio 2015\Projects\Soap\Soap\Program.cs:wiersz 22

打扰一下。有什么线索可以提供这个例外?

尝试调用客户端的Open方法,然后再调用webservice方法。有时它有助于解决一些对象初始化问题。

我们可以根据您的堆栈跟踪看到,当您调用 webservice 方法时,它实际上尝试打开客户端。但显然,与 Open 方法相比,它尝试以不同的方式打开频道。

不幸的是,我没有知识来解释为什么有时需要 Open 调用有时不需要。

如果有人也能解释一下,我会很高兴。