简体   繁体   English

在WCF服务中找不到方法?

[英]Method Not Found in WCF service?

I am newbie in creating WCF web service. 我是创建WCF Web服务的新手。 I am using VS2012 with target framework 4.5. 我正在将VS2012与目标框架4.5一起使用。 I have added a WCF Service file in my project. 我在项目中添加了WCF服务文件。 In "IService.cs" I have written the following code 在“ 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;}
        }

    }

and in "Service.svc.cs" 并在“ 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;
        }
    }
}

But the problem comes when I run my service its not showing my method Calulate, When I pass my URL as Following localhost:2121/Service1.svc/Calculate it shows "method not allowed" error. 但是问题出在我运行服务时未显示我的方法“计算”,当我将URL传递为以下本地主机时:2121 / Service1.svc / Calculate,它显示“不允许的方法”错误。

I have done some Googling and enabled my IIS manager Directory Browsing. 我已经完成了一些Google搜索,并启用了IIS管理器目录浏览功能。 My config file is following 我的配置文件如下

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

I can see a few possible issues. 我可以看到一些可能的问题。

  1. Your Calculate method is set for a HTTP POST request. 您的Calculate方法是为HTTP POST请求设置的。 You should make a HTTP POST request to get proper response. 您应该发出HTTP POST请求以获得正确的响应。
  2. Your request format is JSON (RequestFormat attribute property value), so make sure your request body contains the parameters in JSON format ({ "Coil_type" : "type", "Finned_length": 12 }). 您的请求格式为JSON(RequestFormat属性属性值),因此请确保您的请求正文包含JSON格式的参数({“ Coil_type”:“ type”,“ Finned_length”:12})。
  3. Why do you have the [DataMember] public string input on the service implementation? 为什么在服务实现上有[DataMember]公共字符串输入? Service implementations should generally carry operation contracts. 服务实施通常应携带运营合同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM