Skip to main content
 首页 » 编程设计

c#之WCF 服务 405 方法不允许异常

2024年02月24日24bhlsheji

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

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

我的项目针对 .NET Framework 3.5 进行构建。我的解决方案将该项目引用为一个网站,该网站使用指向服务器 Web 目录的 UNC 路径。项目构建没有错误,我可以在我的客户端项目(在我的 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 

客户端程序.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 

请您参考如下方法:

好的,看起来我通过向我的 web.config 添加一个标识元素解决了这个问题。奇怪的是,这并不是互联网上流传的答案之一,但我在查看一篇几乎不相关的文章时偶然发现了它。修复过程如下:

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