C# WCF 服务 405 方法不允许异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2202500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF Service 405 Method Not Allowed Exception
提问by CoderHead
I'm writing a WCF service in Visual Studio 2008 to be hosted on a Windows 2008 Standard server.
我正在 Visual Studio 2008 中编写 WCF 服务以托管在 Windows 2008 Standard 服务器上。
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.
我在 IIS7 中创建了一个新的网站,它自己的应用程序针对 ASP.NET 2.0。我向服务器角色添加了 .NET 3.0 Framework 功能。我已经完成了涉及 *.svc 扩展和 aspnet_isapi 模块的所有步骤。我已经运行了“ServiceModelReg -r”并重新启动了 IIS 以及服务器本身。
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!
我的项目针对 .NET 框架 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:
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:
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:
客户端程序.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
采纳答案by CoderHead
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:
好的,看起来我通过向 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>