従来の ASMX Web サービスを WCF サービスに変換しようとしています。このために、codeplex が提供する WCFExtras を使用しています。
プロキシで作成されたメソッドを確認できますが、アプリケーションを実行しようとすると、「405、メソッドが許可されていません」というエラーが発生します。
SOAPヘッダーで資格情報を渡すサービス呼び出し認証を実行しようとしています。
私が使用しているソース コードと構成ファイルを以下に示します。この問題を解決する方法を教えてください。
アプリケーションは VS2012 でビルドされています。
よろしく、 マノジ
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 サービス 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>