的ProtocolException未处理/(405)的方法不与WCF允许的;绑定和端点看的权利,虽然绑定、不与、权利、方法

2023-09-02 01:58:40 作者:残垣

我只是学习如何使用WCF和我想从头开始编写一个小HelloWorld程序(包括主机端和客户端)。我已经得到一个的ProtocolException未处理每当我的客户端尝试使用的服务,我想不通这是为什么。我使用的是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.

关于我的样子有事情设置:在此的视频,并在此概述article.基本上,我已经为每个的解决方案中得到了不同的项目。

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://本地主机: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未处理,(405)不允许的方法。

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

我一直在试图找出了这一点,几个小时,我还没有取得很大进展。如果有人至少可以点我在正确的方向,我会非常AP preciative。

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.

谢谢!

推荐答案

好了,找到了解决办法,虽然我不完全知道为什么它的工作原理。我想,当您使用卡西尼或IIS或任何主办的网站,你不应该在web.config文件中的端点指定地址。我所要做的就是将其更改为地址=,并开始正常工作。一定要确保你的服务器上托管的服务的端口在你的app.config文件相匹配的code。

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.