WCF 405 webmethod 未找到问题

发布于 2024-11-25 14:56:43 字数 7202 浏览 2 评论 0原文

我需要创建一个Web服务,其中客户端和服务将在Simple Soap中进行对话(请求和响应将是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 not body 任何人都可以告诉我这是什么问题,在此之前我是 WCF 的新手创建了一个示例,这是我的第二个应用程序

I have a requirement of creating a webservice where the client and the service will talk in Simple Soap (that request and response will be soap), I tried all to find a sample example on net where this thing is already done or some code sample so that I can get started but I think I am bad in searching google, that is why can't find any one so far, Some one suggested to use WCF so get an article

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

But again my problem is not solved, I tried to create an application with this sample (with so many issues :( )

Created a console application and the Program.cs is

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

the app.config which I generated using svcutil.exe is like this

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

My webservioce is like (it is a WCF website in which the port is provided by me and is 2000

Service contract is

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

}

and the web.config is

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

My issue is 405 webmethod not body can anyone please tell me what is an issue in this, I am new to WCF before this created a sample and this is my second application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2024-12-02 14:56:44

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

You do not need to create the SOAP message manually, just use basic http binding and that will rturn SOAP.

神也荒唐 2024-12-02 14:56:44

当您拥有 WCF 服务时,重点是您可以定义诸如字符串、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 上的 WCF 开发人员中心的初学者指南 - 它包含许多关于如何开始使用 WCF 的非常好的视频、截屏视频和文章。 -

您可能还想查看 DotNet Rocks TV第 135 集:Keith Elder 揭秘 WCF

When you have a WCF service, the whole point is that you can define e.g. parameters like strings, int and so forth - and you don't have to mess with loads of XML and SOAP headers and bodies.

So basically, your service contract should be something like:

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

and your service implementation would then just implement that method, add the two numbers, and return the result:

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

No mess with Message or XML manipulation or anything.

A client that wants to call your service would create a WCF client-side proxy using svcutil or the Visual Studio Add Service Reference method, and it would get a proxy class that has the same methods as the service it connects to - and you would call them, using the straight, easy parameters - something like:

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

So basically, I think you need to get back to the drawing board and read up on the WCF basics again - it should not be that difficult, really! (that's the whole point - it should make services easy ...)

Check out the Beginner's Guide at the WCF Developer Center at MSDN - it contains lots of really good videos, screencasts, articles on how to get started with WCF.-

You might also want to check out the DotNet Rocks TV episode #135: Keith Elder Demystifies WCF

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文