繁体   English   中英

ProtocolException Unhandled/(405) WCF 不允许的方法; 绑定和端点看起来不错

[英]ProtocolException Unhandled/(405) Method not allowed with WCF; Bindings and Endpoints look right though

我正在学习如何使用 WCF 并且我正在尝试从头开始编写一个小的 HelloWorld 程序(主机端和客户端)。 每当我的客户尝试使用该服务时,我都会收到ProtocolException Unhandled ,我不明白为什么。 我正在使用 IIS 托管该服务。

关于我的设置方式:我正在尽最大努力将客户端、代理、主机、服务和合同分开,如本视频中详述的和本文中概述的那样。 基本上我在每个项目的解决方案中都有不同的项目。

这里有一些不同的文件显示我在说什么:

服务

namespace HelloWorld
{
  public class HelloWorldService : IHelloWorldService
  {
    public String GetMessage(String name)
    {
        return "Hello World from " + name + "!";
    }
  }
}

合同

namespace HelloWorld
{
  [ServiceContract]
  public interface IHelloWorldService
  {
      [OperationContract]
      String GetMessage(String name);
  }
}

代理人

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));
    }
  }

}

客户端只是一个带有文本框和按钮的表单,它尝试使用文本框中的任何内容作为参数来执行 GetMessage()。 还有另一个 class 实际上创建了一个表单实例。

这是我的网站 web.config:

Web.config

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

这是我的客户端随附的 app.config:

应用配置

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

我的 svc 文件很短,只是:

HelloWorldService.svc

<%@ServiceHost Service="HelloWorld.HelloWorldService"%>

我知道该服务正在运行,因为当我在浏览器中导航到http://localhost:8002/HelloWorldService.svc时,我看到的屏幕显示

您已经创建了一个服务。

要测试此服务,您需要创建一个客户端并使用它来调用该服务。

所以这就是障碍发生的地方:服务正在使用 IIS 运行,我启动了一个客户端实例,带有文本框的 window 和按钮出现了,我输入了一些字母,点击了按钮,然后程序崩溃了,我获取ProtocolException Unhandled, (405) Method not allowed. 错误发生在 Proxy class 的这一行:

return Channel.GetMessage(name);

几个小时以来,我一直在努力解决这个问题,但我没有取得太大进展。 如果有人至少能指出我正确的方向,我将不胜感激。

最后一件事:我想从头开始编写客户端和代理,而不使用 svcutil.exe。

转到基地址(.svc)时起作用的原因是这是用于获取服务元数据的HTTP GET操作。 当您在服务合同上调用某个方法时,您正在执行POST操作,而且很有可能只是未启用此功能。 根据您要定位的.NET版本,它将以红色突出显示。

在此处输入图片说明

您必须在“客户端配置”的端点标记中指定服务的完整地址。 像这样:

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

您必须在“客户端配置”的终结点标记中指定服务的完整地址

address =“ http:// localhost:8002 / HelloWorldService.svc”,在其中放置相同的端点配置。

当我陷入地狱的时候,它确实为我工作。

好的,找到了一个解决方案,尽管我不完全确定为什么会起作用。 我猜想当您使用Cassini或IIS或其他任何网站来托管网站时,不应在web.config文件的终结点中指定地址。 我要做的就是将其更改为address="" ,它开始正常工作。 确保确保服务器托管服务的端口与app.config文件中的代码匹配。

启用以下两个组件,对我有用。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM