WCF 405 webmethod未找到问题

时间:2011-07-21 18:02:54

标签: wcf web-services http-status-code-405

我需要创建一个Web服务,其中客户端和服务将在Simple Soap中进行通信(请求和响应将是肥皂),我试图在网上找到一个示例,其中已经完成了这个事情或者一些代码示例,以便我可以开始,但我认为我在搜索谷歌时很难,这就是为什么到目前为止找不到任何一个,有人建议使用WCF所以得到一篇文章

http://csharping.com/wcf/building-a-soap-response-envelope-manually-with-the-message-class/

但是我的问题再次解决,我试图用这个样本创建一个应用程序(有很多问题:()

创建了一个控制台应用程序,而Program.cs是

using System;
using System.IO;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Text;
using System.Runtime.Serialization;


namespace ServiceConsole
{
    public class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost serviceHost = new ServiceHost(typeof(ServiceClient), new Uri("http://localhost:2000/")))
            {
                ServiceEndpoint serviceEndpoint = new ServiceEndpoint(
                    ContractDescription.GetContract(typeof(IService)));               

                ServiceEndpoint metadataEndpoint = new ServiceEndpoint(
                    ContractDescription.GetContract(typeof(IMetadataExchange)));               

                ServiceMetadataBehavior metadataBehavior = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (metadataBehavior == null)
                {
                    metadataBehavior = new ServiceMetadataBehavior();
                    metadataBehavior.HttpGetEnabled = true;
                    serviceHost.Description.Behaviors.Add(metadataBehavior);
                }


                serviceHost.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "http://localhost:2000/");
                serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "http://localhost:2000/WCFService/mex");
                serviceHost.Open();

                string requestData = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Header><h:HeaderItem xmlns:h=\"http://tempuri.org/\">a header item</h:HeaderItem><ActivityId CorrelationId=\"090c553b-bfcc-4e4f-94cd-1b4333fe82a9\" xmlns=\"http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics\">377a454b-b543-4c6f-b4ac-3981029b60e6</ActivityId></s:Header><s:Body><string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">a body item</string></s:Body></s:Envelope>";
                byte[] requestDataBytes = Encoding.UTF8.GetBytes(requestData);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/WCFService/");
                request.Method = "POST";
                request.ContentType = "text/xml; charset=utf-8";
                request.Headers.Add("SOAPAction", "http://tempuri.org/IWebService/GetMessage");
                request.ContentLength = requestDataBytes.Length;

                StreamWriter streamWriter = new StreamWriter(request.GetRequestStream());
                streamWriter.Write(requestData);
                streamWriter.Flush();
                streamWriter.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader streamReader = new StreamReader(response.GetResponseStream());
                string responseBody = streamReader.ReadToEnd();

                Console.WriteLine("Service returned the following response...");
                Console.WriteLine("");
                Console.WriteLine(responseBody);
                Console.ReadKey();
                serviceHost.Close();
            }
        }
    }
}

我使用svcutil.exe生成的app.config就像这样

     <?xml version="1.0" encoding="utf-8"?>
        <configuration>
          <system.serviceModel>
            <bindings>
              <basicHttpBinding>
                <binding name="BasicHttpBinding_IService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                  <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                      maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                  <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                  </security>
                </binding>
              </basicHttpBinding>
            </bindings>
            <client>
              <endpoint address="http://localhost:2000/WebService/Service.svc"
                  binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
                  contract="IService" name="BasicHttpBinding_IService" />
            </client>
          </system.serviceModel>
        </configuration>

我的webservioce就像(这是一个WCF网站,我的端口由我提供,是2000

服务合同

[ServiceContract]
public interface IService
{
    [OperationContract]
    Message GetMessage(Message s);
}

[ServiceBehavior]
public class Service : IService
{
    public Message GetMessage(Message message)
    {
        string body = message.GetBody<string>();
        return Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IWebService/GetMessageResponse", "body is " + body);
    }

}

web.config

 <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="Service">
        <endpoint address="http://localhost:2000/WebService/Service.svc" binding="basicHttpBinding" bindingConfiguration=""
          contract="IService" >
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <timeouts closeTimeout="00:01:10" />
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" policyVersion="Policy15" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

我的问题是405 webmethod没有身体可以请任何人告诉我这是什么问题,我是新来的WCF之前创建了一个样本,这是我的第二个应用程序

2 个答案:

答案 0 :(得分:0)

您不需要手动创建SOAP消息,只需使用基本的http绑定,即可以使用SOAP。

答案 1 :(得分:0)

当您拥有WCF服务时,重点是您可以定义例如像strings,int等参数 - 你不必乱用大量的XML和SOAP标题和正文。

基本上,你的服务合同应该是这样的:

[ServiceContract]
public interface IService
{
    [OperationContract]
    int DoSomeMathAddTowNumbers(int num1, int num2);
}

然后您的服务实现将只实现该方法,添加两个数字,并返回结果:

public class Service : IService
{
    int DoSomeMathAddTowNumbers(int num1, int num2)
    {
         return num1 + num2;  
    }
}

没有Message或XML操作或其他任何内容。

想要调用您的服务的客户端将使用svcutil或Visual Studio Add Service Reference方法创建WCF客户端代理,并且它将获得一个代理类,其方法与它连接到的服务 - 您可以使用简单直接的参数调用它们,例如:

ServiceClient client = new ServiceClient();
int result = client.DoSomeMathAddTwoNumbers(42, 100);

所以基本上,我认为你需要回到绘图板并再次阅读WCF基础知识 - 它应该那么困难,真的! (这就是重点 - 它应该使服务容易 ......)

查看MSDN上的Beginner's Guide at the WCF Developer Center - 它包含许多非常好的视频,截屏视频,有关如何开始使用WCF的文章.-

您可能还想查看DotNet Rocks电视剧#135:Keith Elder Demystifies WCF