WCF服务405不允许的方法异常 [英] WCF Service 405 Method Not Allowed Exception

查看:511
本文介绍了WCF服务405不允许的方法异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我创建IIS7中一个新的Web站点自己的应用程序有针对性的ASP.NET 2.0。我加了.NET 3.0框架功能的服务器角色。我已经通过所有涉及* .SVC扩展和aspnet_isapi模块的步骤了。我已经运行ServiceModelReg -r并重新启动IIS以及服务器本身。



我的项目面向.NET框架3.5的基础之上。我的解决方案引用该项目作为使用UNC路径到服务器的Web目录网站。该项目建立没有错误,我可以成功我的客户项目中添加一个服务引用我的WCF服务(在我的Win XP的开发虚拟机)。我的客户项目的建造和运行,但服务方法中抛出一个异常( CheckDatabaseConnection 的)调用。我的web.config和服务类附着下面,其次由客户端应用程序。 !我已经完全江郎才尽



的web.config:



 < system.serviceModel> 
< serviceHostingEnvironment aspNetCompatibilityEnabled =FALSE/>
<服务和GT;
<服务behaviorConfiguration =ConnectivityBehaviorNAME =EDCO.Web.Services.Connectivity>
<端点地址=htt​​p://validurl.net绑定=的wsHttpBinding合同=EDCO.Web.Services.IConnectivity>
< /端点>
<端点地址=MEX绑定=mexHttpBinding合同=IMetadataExchange接口/>
< /服务>
< /服务>
<&行为GT;
< serviceBehaviors>
<行为NAME =ConnectivityBehavior>
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =FALSE/>
< /行为>
< / serviceBehaviors>
< /行为>
< /system.serviceModel>



IConnectivity.cs:



  [的ServiceContract(命名空间=htt​​p://validurl.net)] 
公共接口IConnectivity
{
#区域$的方法b
$ b〔WebGet()]
[WebInvoke(方法=GET)]
[OperationContract的()]
布尔CheckDatabaseConnection(字符串服务器,字符串数据库,字符串用户,字符串密码);

#endregion方法
} //接口IConnectivity

< STRONG> Connectivity.svc.cs:

 公共类连接:IConnectivity 
{
#地区会员

常量字符串CONN_STRING =服务器= {0};数据库= {1};用户ID = {2};密码= {3};;

#endregion会员

#地区的方法

公共BOOL CheckDatabaseConnection(字符串服务器,数据库字符串,字符串用户,字符串密码)
{
的SqlConnection康恩= NULL;


{
康恩=新的SqlConnection(的String.Format(CONN_STRING,服务器,数据库,用户,密码));
conn.Open();
}

{
返回FALSE;
}
终于
{
如果(参数conn!= NULL)
{
如果(conn.State!= ConnectionState.Closed)
{
conn.Close();
}
conn.Dispose();
}
}

返回真;
} // CheckDatabaseConnection(服务器,数据库,用户,密码)

#endregion方法
} //类连接

客户端的Program.cs:

 类节目
{
#地区的方法

静态无效的主要(字串[] args)
{

Connectivity.ConnectivityClient连接=新Connectivity.ConnectivityClient();

Console.WriteLine(建立连接...);
如果(connect.CheckDatabaseConnection(DBSERVER,DBNAME,登录,密码))
{
Console.WriteLine(连接成功!);
}
,否则
{
Console.WriteLine(连接失败。);
}
到Console.ReadLine();
} //主()

#endregion方法
} //类节目


解决方案

确定,它看起来像我加入一个标识元素,我的web.config解决了这个问题。奇怪的是,这并没有得到答案流传在互联网上的一个,但我通过它跌跌撞撞,而在寻找一个几乎不相关的文章。此修复程序是这样的:

 <服务behaviorConfiguration =ConnectivityBehaviorNAME =EDCO.Web.Services.Connectivity> ; 
<端点地址=htt​​p://validurl.net/Connectivity.svc绑定=的wsHttpBinding合同=EDCO.Web.Services.IConnectivity>
<同一性GT;
< D​​NS值=validurl.net/>
< /身份>
< /端点>
<端点地址=MEX绑定=mexHttpBinding合同=IMetadataExchange接口/>
< /服务>


I'm writing a WCF service in Visual Studio 2008 to be hosted on a Windows 2008 Standard server.

I've created a new Web Site in IIS7 with its own application targeted to ASP.NET 2.0. I added .NET 3.0 Framework features to the server role. I've already gone through all of the steps involving *.svc extensions and the aspnet_isapi module. I've run "ServiceModelReg -r" and restarted IIS as well as the server itself.

My project targets the .NET framework 3.5 for builds. My solution references the project as a Web site using a UNC path to the server's Web directory. The project builds without errors and I can successfully add a service reference to my WCF service in my client project (on my Win XP development virtual machine). My client project builds and runs, but throws an exception during the service method (CheckDatabaseConnection) call. My web.config and service classes are attached below, followed by the client app. I've completely run out of ideas!

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

client 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

解决方案

OK, it looks like I solved the problem by adding an identity element to my web.config. Oddly enough, this hasn't been one of the answers floating around on the Internet but I stumbled across it while looking at a nearly-unrelated article. The fix goes like this:

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

这篇关于WCF服务405不允许的方法异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆