Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed

Tags:

soap

wcf

I am trying to convert the classic ASMX webservice into WCF Service, For this I am using WCFExtras provided by codeplex.

I am able to see the method created in proxy, but when I try to run the application, i am getting "405, Method Not Allowed" error.

I am trying to do Service call authentication passing the credential in SOAP Header.

Please find below the source code and config files which I am using. Let me know how to resolve this issue.

The application is build in VS2012.

Regards, Manoj

WebServiceInterface

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using WCFExtras.Soap;

namespace WebServiceToWCFService
{
    [SoapHeaders]
    [ServiceContract]  
    public interface WebServiceInterface
    {
        [SoapHeader("AuthenticationHeader", typeof(AuthenticationHeader), Direction =SoapHeaderDirection.In)]
        [OperationContract]
        string HelloWorld(string inputValue);

    }
}

WebService1.asmx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.ServiceModel;
using System.Runtime.Serialization;
using WCFExtras.Soap;
//using System.Web.Services.Protocols;

namespace WebServiceToWCFService
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    //[WebService(Namespace = "http://tempuri.org/")]
    //[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    //[System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX,     uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]

    public class WebService1 : System.Web.Services.WebService, WebServiceInterface
    {
        public string HelloWorld(string inputValue)
        {
            AuthenticationHeader header =     SoapHeaderHelper<AuthenticationHeader>.GetInputHeader("AuthenticationHeader");
            if (header != null)
                return "Hello World " + inputValue;
            else
                return "No Header";
        }
    }
}

AuthenticationHeader.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Web;
using System.Web.Services.Protocols;

namespace WebServiceToWCFService
{
    [DataContract]
    public sealed class AuthenticationHeader// : SoapHeader
    {
        private string m_username;
        private DateTime m_dateTime;
        private string m_hash;

        /// <summary>
        /// The username used for authentication
        /// </summary>
        [DataMember]
        public string Username
        {
            get { return m_username; }
            set { m_username = value; }
        }

        /// <summary>
        /// The current time
        /// </summary>
        [DataMember]
        public DateTime DateTime
        {
            get { return m_dateTime; }
            set { m_dateTime = value; }
        }

        /// <summary>
        /// A SHA1 hash made from Username|Password|DateTime
        /// </summary>
        [DataMember]
        public string Hash
        {
            get { return m_hash; }
            set { m_hash = value; }
        }
    }
}

WCF Service Web.config

<?xml version="1.0" encoding="UTF-8"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5">
      <buildProviders>
        <remove extension=".asmx" />
        <add extension=".asmx"     type="System.ServiceModel.Activation.ServiceBuildProvider, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </buildProviders>
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WebServiceToWCFService.WebService1"     behaviorConfiguration="WebServiceToWCFService.SoapHeadersSampleBehavior">
        <endpoint address="" behaviorConfiguration="WebServiceToWCFService.SoapHeadersSampleEndpointBehavior" binding="basicHttpBinding" contract="WebServiceToWCFService.WebServiceInterface"/>
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" behaviorConfiguration="WebServiceToWCFService.SoapHeadersSampleEndpointBehavior" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
         <behavior name="WebServiceToWCFService.SoapHeadersSampleBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="WebServiceToWCFService.SoapHeadersSampleEndpointBehavior">
          <wsdlExtensions location="http://localhost/WebServiceToWCFService/Service1.svc" singleFile="true"/>
        </behavior>
    </endpointBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <!-- Declare that we have an extension called WSDL Extras-->
        <add name="wsdlExtensions" type="WCFExtras.Wsdl.WsdlExtensionsConfig, WCFExtras, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
      </behaviorExtensions>
    </extensions>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

  <system.webServer>
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

TestClient

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCFExtras.Soap;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {

            TestWCFClient.WebServiceInterfaceClient client = new TestWCFClient.WebServiceInterfaceClient();
            client.AuthHeader = new TestWCFClient.AuthenticationHeader() { Username = "abcd", Hash="asdfasdf", DateTime= Convert.ToDateTime("11/11/11")};
            string result = client.TestWorld("test");
            Console.ReadLine();

        }
    }
}

namespace TestApp.TestWCFClient
{
    public partial class WebServiceInterfaceClient
    {
        public AuthenticationHeader AuthHeader
        {
            get {
                return InnerChannel.GetHeader<AuthenticationHeader>    ("AuthenticationHeader");
            }
            set {
                InnerChannel.SetHeader("AuthenticationHeader", value);
            }
        }
    }
}

Client Web.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_WebServiceInterface" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
      maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
      useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
          realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/WebServiceToWCFService/Service1.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_WebServiceInterface"
    contract="TestWCFClient.WebServiceInterface" name="BasicHttpBinding_WebServiceInterface" />
      <metadata>
        <wsdlImporters>
          <extension type="WCFExtras.Soap.SoapHeaderImporter, WCFExtras" />
          <extension type="WCFExtras.Wsdl.Documentation.XmlCommentsImporter, WCFExtras" />
        </wsdlImporters>
      </metadata>
    </client>
  </system.serviceModel>
</configuration>
like image 869
Aryan Avatar asked Oct 10 '13 10:10

Aryan


1 Answers

The issue was, windows features for WCF was not enabled.

Thanks to this post "ProtocolException Unhandled/(405) Method not allowed with WCF; Bindings and Endpoints look right though" which was showing the required features to enable the post request for WCF.

Regards, Manoj

like image 138
Aryan Avatar answered Nov 06 '22 20:11

Aryan