WCF服务405方法不允许例外

时间:2010-02-04 19:20:25

标签: c# web-services iis-7 wcf

我正在Visual Studio 2008中编写WCF服务以托管在Windows 2008 Standard服务器上。

我在IIS7中创建了一个新网站,其自己的应用程序针对ASP.NET 2.0。我在服务器角色中添加了.NET 3.0 Framework功能。我已经完成了涉及* .svc扩展和aspnet_isapi模块的所有步骤。我运行了“ServiceModelReg -r”并重新启动了IIS以及服务器本身。

我的项目针对构建的.NET framework 3.5。我的解决方案使用服务器Web目录的UNC路径将项目作为Web站点引用。该项目构建没有错误,我可以在我的客户端项目(在我的Win XP开发虚拟机上)成功添加服务引用到我的WCF服务。我的客户端项目构建并运行,但在服务方法( CheckDatabaseConnection )调用期间抛出异常。我的web.config和服务类附在下面,后面是客户端应用程序。我完全没有想法了!

的web.config:

    <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" />
    <services>
        <service behaviorConfiguration="ConnectivityBehavior" name="EDCO.Web.Services.Connectivity">
            <endpoint address="http://validurl.net" binding="wsHttpBinding" contract="EDCO.Web.Services.IConnectivity">
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ConnectivityBehavior">
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

IConnectivity.cs:

    [ServiceContract(Namespace="http://validurl.net")]
public interface IConnectivity
{
    #region Methods

    [WebGet()]
    [WebInvoke(Method="GET")]
    [OperationContract()]
    bool CheckDatabaseConnection(string server, string database, string user, string password);

    #endregion Methods
} // interface IConnectivity

Connectivity.svc.cs:

    public class Connectivity : IConnectivity
{
    #region Members

    const string CONN_STRING = "Server={0};Database={1};User ID={2};Password={3};";

    #endregion Members

    #region Methods

    public bool CheckDatabaseConnection(string server, string database, string user, string password)
    {
        SqlConnection conn = null;

        try
        {
            conn = new SqlConnection(string.Format(CONN_STRING, server, database, user, password));
            conn.Open();
        }
        catch
        {
            return false;
        }
        finally
        {
            if (conn != null)
            {
                if (conn.State != ConnectionState.Closed)
                {
                    conn.Close();
                }
                conn.Dispose();
            }
        }

        return true;
    } // CheckDatabaseConnection(server, database, user, password)

    #endregion Methods
} // class Connectivity

客户端Program.cs:

    class Program
{
    #region Methods

    static void Main(string[] args)
    {

        Connectivity.ConnectivityClient connect = new Connectivity.ConnectivityClient();

        Console.WriteLine("Establishing connection...");
        if (connect.CheckDatabaseConnection("dbServer", "dbName", "login", "password"))
        {
            Console.WriteLine("Successful connection!");
        }
        else
        {
            Console.WriteLine("Connection failed.");
        }
        Console.ReadLine();
    } // Main()

    #endregion Methods
} // class Program

1 个答案:

答案 0 :(得分:4)

好的,看起来我通过在web.config中添加一个identity元素来解决问题。奇怪的是,这并不是互联网上出现的答案之一,但我在看一篇几乎无关的文章时偶然发现了它。解决方案是这样的:

        <service behaviorConfiguration="ConnectivityBehavior" name="EDCO.Web.Services.Connectivity">
            <endpoint address="http://validurl.net/Connectivity.svc" binding="wsHttpBinding" contract="EDCO.Web.Services.IConnectivity">
                <identity>
                    <dns value="validurl.net" />
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>