11

我正在尝试将经典的 ASMX Web 服务转换为 WCF 服务,为此我使用的是 codeplex 提供的 WCFExtras。

我可以看到在代理中创建的方法,但是当我尝试运行应用程序时,我收到“405,不允许方法”错误。

我正在尝试通过 SOAP 标头中的凭据进行服务调用身份验证。

请在下面找到我正在使用的源代码和配置文件。让我知道如何解决这个问题。

该应用程序是在 VS2012 中构建的。

问候, 马诺伊

网络服务接口

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 服务 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>

测试客户端

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);
            }
        }
    }
}

客户端 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>
4

1 回答 1

23

问题是,未启用 WCF 的 Windows 功能。

感谢这篇文章“ ProtocolException Unhandled/(405) Method not allowed with WCF; Bindings and Endpoints look right but ”,它显示了启用 WCF 发布请求所需的功能。

问候, 马诺伊

于 2013-10-10T13:41:05.623 回答