C# WCF不允许使用ProtocolException Unhandled/(405)方法;不过绑定和端点看起来不错
我正在学习如何使用WCF,我正在尝试从头开始编写一个小HelloWorld程序(主机和客户端)。每当我的客户尝试使用该服务时,我都会收到一个未经处理的C# WCF不允许使用ProtocolException Unhandled/(405)方法;不过绑定和端点看起来不错,c#,.net,wcf,iis,C#,.net,Wcf,Iis,我正在学习如何使用WCF,我正在尝试从头开始编写一个小HelloWorld程序(主机和客户端)。每当我的客户尝试使用该服务时,我都会收到一个未经处理的协议异常,我不知道为什么。我正在使用IIS托管服务 关于我的设置方式:我正在尽我最大的努力将客户机、代理、主机、服务和合同分离开来,如本文和本文所述。基本上,我在每个解决方案中都有不同的项目 下面是一些不同的文件,显示了我所说的内容: 服务 合同 代理 } 客户端只是一个带有文本框和按钮的表单,它尝试使用文本框中的任何内容作为参数来执行GetMes
协议异常
,我不知道为什么。我正在使用IIS托管服务
关于我的设置方式:我正在尽我最大的努力将客户机、代理、主机、服务和合同分离开来,如本文和本文所述。基本上,我在每个解决方案中都有不同的项目
下面是一些不同的文件,显示了我所说的内容:
服务
合同
代理
}
客户端只是一个带有文本框和按钮的表单,它尝试使用文本框中的任何内容作为参数来执行GetMessage()。还有另一个类实际创建表单的实例
以下是网站的my web.config:
Web.config
我已经花了好几个小时试图弄明白这一点,但我没有取得多大进展。如果有人能给我指出正确的方向,我会非常感激
最后一件事:我想从头开始编写客户端和代理,而不使用svcuti.exe。当您转到基址(.svc)时,它工作的原因是这是一个HTTP GET操作,用于获取服务的元数据。当您在服务契约上调用方法时,您正在执行POST操作,很可能您只是没有启用此功能。根据您的目标.NET版本,它将是其中一个以红色突出显示的版本
好的,找到了一个解决方案,尽管我不完全确定它为什么有效。我猜当您使用Cassini或IIS或任何东西来托管网站时,您不应该在web.config文件的端点中指定地址。我所要做的就是把它改成
address=”“
,它就开始正常工作了。确保服务器承载服务的端口与app.config文件中的代码匹配。您必须在客户端配置的端点标记中指定服务的完整地址。
像这样:
<endpoint address="http://localhost:8002/HelloWorldService.svc" binding="basicHttpBinding" contract="HelloWorld.IHelloWorldService" />
不要忘记在端点标记的address属性中的服务名称之后添加'.svc'扩展名。这对我有用。希望你现在能得到解决方案
客户端配置将如下所示:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService"/>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:61569/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService" contract="WcfServiceApp.IService1" name="WSHttpBinding_IService"/>
</client>
您必须在客户端配置的端点标记中指定服务的完整地址 地址=”http://localhost:8002/HelloWorldService.svc,其中放置相同的端点配置
当我陷入地狱时,它确实对我起了作用…更新:我运行了以下命令:“%WINDIR%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe”-r并且错误已更改。它在同一行,但现在它说:内容类型text/html;响应消息的charset=utf-8与绑定的内容类型(text/xml;charset=utf-8)不匹配。如果使用自定义编码器,请确保IsContentTypeSupported方法得到了正确实现。此外,当我使用此新错误的详细解决方案时,它会切换回我之前得到的错误;405方法不允许。当我在前面的注释中运行该命令时,它会再次切换回“内容类型不匹配”错误。也许这会给某人提供额外的线索?当我导航到Windows功能时,.NET Framework 3.5及其子文件夹的选项已经被选中。没有用于.NET Framework 4.5高级服务的文件夹。这很奇怪,因为它安装在计算机上(如果我导航到可以卸载程序的菜单,我会看到Microsoft.NET Framework、Microsoft.NET Framework多目标包和Microsoft.NET Framework SDK)。你知道为什么这些选项不会出现吗?这救了我一天!非常感谢。也许我只是不太了解Windows或IIS,但这些Windows功能设置似乎太随机、太隐蔽、太不必要了。
namespace HelloWorld
{
public class Proxy : ClientBase<IHelloWorldService>, IHelloWorldService
{
#region IHelloWorldService Members
public String GetMessage(String name)
{
return Channel.GetMessage(name);
}
#endregion
}
namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click_1(object sender, EventArgs e)
{
Proxy proxy = new Proxy();
MessageBox.Show(proxy.GetMessage(textBox1.Text));
}
}
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="HelloWorld.HelloWorldService" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="http://localhost:8002/" binding="basicHttpBinding" contract="HelloWorld.IHelloWorldService"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
</system.serviceModel>
</configuration>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:8002/" binding="basicHttpBinding" contract="HelloWorld.IHelloWorldService" />
</client>
</system.serviceModel>
</configuration>
<%@ServiceHost Service="HelloWorld.HelloWorldService"%>
return Channel.GetMessage(name);
<endpoint address="http://localhost:8002/HelloWorldService.svc" binding="basicHttpBinding" contract="HelloWorld.IHelloWorldService" />
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IService"/>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:61569/Service1.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IService" contract="WcfServiceApp.IService1" name="WSHttpBinding_IService"/>
</client>