ProtocolException Unhandled/(405) WCF 不允许方法;绑定和端点看起来不错 [英] ProtocolException Unhandled/(405) Method not allowed with WCF; Bindings and Endpoints look right though

查看:14
本文介绍了ProtocolException Unhandled/(405) WCF 不允许方法;绑定和端点看起来不错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm just learning how to use WCF and I am trying to write a little HelloWorld program from scratch (both the host and client sides). I've been getting a ProtocolException Unhandled whenever my client tries to use the service, and I can't figure out why. I'm hosting the service using IIS.

关于我的设置方式:我正在尽我最大的努力将客户端、代理、主机、服务和合同分开,详情请参见 video 和本文章.基本上,我在每个解决方案中都有不同的项目.

Regarding the way I have things set up: I'm doing my best to separate the client, proxy, host, service, and contract as detailed in this video and as outlined in this article. Basically I've got different projects within the solution for each of those.

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

Here are some different files showing what I'm talking about:

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().还有一个类实际上创建了表单的一个实例.

The client is just a form with a textbox and a button, and it tries to execute GetMessage() using whatever is in the textbox as a parameter. There is another class that actually creates an instance of the form.

这是我的网站 web.config:

Here's my web.config for the website:

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

And here's my app.config that goes with the client:

<?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文件很短,就是:

My svc file is very short, just:

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

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

I know the service is running, because when I navigate to http://localhost:8002/HelloWorldService.svc in my browser I get the screen that says

您已经创建了一个服务.

You have created a service.

要测试此服务,您需要创建一个客户端并将其用于调用服务.

To test this service, you will need to create a client and use it to call the service.

所以这里是问题发生的地方:服务正在使用 IIS 运行,我启动一个客户端实例,出现带有文本框和按钮的窗口,我输入一些字母,点击按钮,然后程序崩溃,我得到 ProtocolException Unhandled, (405) Method not allowed. 错误发生在代理类的这一行:

So here's where the snag happens: the service is running using IIS, I start an instance of the client, the window with the textbox and the button come up, I type in some letters, hit the button, and then the program crashes and I get the ProtocolException Unhandled, (405) Method not allowed. The error happens on this line of the Proxy class:

return Channel.GetMessage(name);

我已经尝试解决这个问题好几个小时了,但没有取得太大进展.如果有人至少能指出我正确的方向,我将不胜感激.

I've been trying to figure this out for hours and hours, and I haven't made much progress. If someone could at least point me in the right direction, I would be very appreciative.

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

Last thing: I want to write the client and proxy from scratch, without using svcutil.exe.

推荐答案

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

Ok, found a solution, although I'm not entirely sure why it works. I guess when you are using Cassini or IIS or whatever to host the website, you're not supposed to specify an address in the endpoint in the web.config file. All I had to do was change it to address="" and it started working properly. Be sure to make sure the port your server is hosting the service on matches the code in your app.config file.

这篇关于ProtocolException Unhandled/(405) WCF 不允许方法;绑定和端点看起来不错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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