【问题标题】:Method Not Found in WCF service?在 WCF 服务中找不到方法?
【发布时间】:2013-09-30 07:17:58
【问题描述】:

我是创建 WCF Web 服务的新手。我正在使用带有目标框架 4.5 的 VS2012。我在我的项目中添加了一个 WCF 服务文件。 在“IService.cs”中,我编写了以下代码

   namespace _3TWebServ
    {
        // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped,
                          RequestFormat = WebMessageFormat.Json,
                          UriTemplate = "Calculate")]
            String Calculate(Inputs ip1);
        }


        [DataContract]
        public class Inputs
        {
            [DataMember(Name = "Coil_type")]
            public string Coil_type { get; set;}

            [DataMember(Name = "Finned_length")]
            public string Finned_length { get; set;}
        }

    }

在“Service.svc.cs”中

namespace _3TWebServ
{
    public class Service1 : IService1
    {
        [DataMember]
        public string input;

        public String Calculate(Inputs ip1)
        {
            String str = ip1.Coil_type + ip1.Finned_length;
            return str;
        }
    }
}

但是问题来了,当我运行我的服务时,它没有显示我的方法 Calulate,当我传递我的 URL 时如下 localhost:2121/Service1.svc/Calculate 它显示“方法不允许”错误。

我做了一些谷歌搜索并启用了我的 IIS 管理器目录浏览。我的配置文件如下

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
  </system.web>

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="_3TWebServ.IService1"  name="_3TWebServ.Service1">
        <endpoint  address="" behaviorConfiguration="Rest" binding="webHttpBinding" contract="_3TWebServ.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <!--endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />-->
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="_3TWebServ.IService1">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="Rest">
          <webHttp />
        </behavior>
      </endpointBehaviors>

    </behaviors>
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

【问题讨论】:

    标签: c# wcf web-services


    【解决方案1】:

    我可以看到一些可能的问题。

    1. 您的Calculate 方法是为HTTP POST 请求设置的。您应该发出 HTTP POST 请求以获得正确的响应。
    2. 您的请求格式为 JSON(RequestFormat 属性属性值),因此请确保您的请求正文包含 JSON 格式的参数({ "Coil_type" : "type", "Finned_length": 12 })。
    3. 为什么在服务实现中有 [DataMember] 公共字符串输入?服务实现通常应该带有操作合同。

    【讨论】:

    • 请告诉如何发出HTTP post请求
    • 这取决于您的客户端应用程序。您可以使用 HttpClient、HttpWebRequest、ajax/javasript、fiddler 发出 POST 请求。您可以在互联网上搜索更多详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多