Link to home
Start Free TrialLog in
Avatar of jjp2
jjp2

asked on

405 Method Not Allowed when calling from .NET 3.5 DLL but not console application

I am getting the response 405 Method not allowed when I call a web service from a .NET 3.5 Class Library (DLL).  This is a secure web service call, so I set the credentials and add them to the headers.  The same code functions from a console application.  The same code also functions if I break in the Visual Studio 2010 debugger and toggle the binding.Security.Transport.ProxyCredentialType from HttpProxyCredentialType.None to Nmtl or viceversa.  The call always fails no matter what regardless to what the binding.Security.Transport.ProxyCredentialType is initially set to, but changing it in the debugger makes it work.  Changing it via code does not.

Here is the code
                  try
                    {
 
                        BasicHttpBinding binding = new BasicHttpBinding();
                        binding.Name = "InterfacePortBinding";
                        binding.CloseTimeout = new TimeSpan(0, 1, 0);
                        binding.OpenTimeout = new TimeSpan(0, 1, 0);
                        binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
                        binding.SendTimeout = new TimeSpan(0, 1, 0);
                        binding.AllowCookies = false;
                        binding.BypassProxyOnLocal = false;
                        binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                        binding.MaxBufferSize = 65536;
                        binding.MaxBufferPoolSize = 524288;
                        binding.MaxReceivedMessageSize = 65536;
                        binding.MessageEncoding = WSMessageEncoding.Text;
                        binding.TextEncoding = Encoding.UTF8;
                        binding.TransferMode = TransferMode.Buffered;
                        binding.UseDefaultWebProxy = true;
                        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;
                        binding.ReaderQuotas.MaxDepth = 32;
                        binding.ReaderQuotas.MaxStringContentLength = 8192;
                        binding.ReaderQuotas.MaxArrayLength = 16384;
                        binding.ReaderQuotas.MaxBytesPerRead = 4096;
                        binding.ReaderQuotas.MaxNameTableCharCount = 16384;
                        binding.UseDefaultWebProxy = true;
                       
 
                        EndpointAddress endpointAddress = new EndpointAddress(VSI.WebServiceURL);
                                 
                        WebServices.GetInfoFromIdResult miResult;
                        WebServices.InterfaceClient wsCaller = new  WebServices.InterfaceClient(binding, endpointAddress);
 
                        wsCaller.ClientCredentials.UserName.UserName = VSI.WebServiceUserID;
                        wsCaller.ClientCredentials.UserName.Password = VSI.WebServicePassword;
 
                        using (OperationContextScope scope = new OperationContextScope(wsCaller.InnerChannel))
                        {
                            var httpRequestProperty = new HttpRequestMessageProperty();
                            httpRequestProperty.Headers[System.Net.HttpRequestHeader.Authorization] = "Basic" + Convert.ToBase64String(Encoding.ASCII.GetBytes(wsCaller.ClientCredentials.UserName.UserName + ":" + wsCaller.ClientCredentials.UserName.Password));
                            OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
 
 
                            string resultMsg = wsCaller.getInfo(info.Sender.Value).ReturnMessage;
 
                        }
                    }
                    catch (Exception ex)
                    {
                        result.ValidationPassed = false;
                        result.Message = "Error calling web service - " + ex.Message;
                        return result;
                    }


Any help is very much appreciated.

Thanks
Avatar of jjp2
jjp2

ASKER

I've found that just breaking in to the debugger and looking at the httprequestproperty object is enough to make the above work.  After it works once, it will always work even without breaking.  It will consistantly not work (many calls to the DLL), until the code is stepped into.

The same code when executed in Visual Studio with no breakpoints will eventually succeed and then always succeed.  It will never succeed as a compiled DLL.

The compiled DLL always displays the 405 error.
ASKER CERTIFIED SOLUTION
Avatar of jjp2
jjp2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial