简体   繁体   中英

Method Not Found in WCF service?

I am newbie in creating WCF web service. I am using VS2012 with target framework 4.5. I have added a WCF Service file in my project. In "IService.cs" I have written the following code

   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"

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.

I have done some Googling and enabled my IIS manager Directory Browsing. 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. You should make a HTTP POST request to get proper response.
  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 }).
  3. Why do you have the [DataMember] public string input on the service implementation? Service implementations should generally carry operation contracts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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