WCF restful приложения PUT, POST, DELETE получают ту же ошибку: «метод не разрешен». - PullRequest
0 голосов

Я создаю сервер, реализованный на WCF с архитектурой RESTFUL. Написано в C # с использованием Entity Framework и MySQL DB. Код приведен ниже. VS не выдает никаких ошибок, метод GET работает нормально, он выводит данные из базы данных. Однако все другие методы (PUT, POST, DELETE, GetDetails) получают ту же ошибку: «метод не разрешен». Может быть, вы могли бы сказать, в чем моя ошибка? Если несколько ошибок, то буду благодарен. Я только начал изучать это, поэтому я допускаю, что может быть много ошибок.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace WcfRestFullService
{
   [DataContract]
    public class CustomerDataContract
    {
        [DataMember]
        public string Id_Cus { get; set; }
        [DataMember]
        public string FirstName_Cus { get; set; }
        [DataMember]
        public string LastName_Cus { get; set; }
        [DataMember]
        public string PhoneNum_Cus { get; set; }
        [DataMember]
        public string Email_Cus { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfRestFullService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ICustomerSevice" in both code and config file together.
    [ServiceContract]
    public interface ICustomerSevice
    {
          [OperationContract]
          [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json,
              ResponseFormat = WebMessageFormat.Json, UriTemplate = "/GetAllCustomer/")]//ok
          List<CustomerDataContract> GetAllCustomer();

         [OperationContract]
         [WebGet(RequestFormat = WebMessageFormat.Json,
          ResponseFormat = WebMessageFormat.Json,
          UriTemplate = "/CustomerDetails/{Id_Cus}")]
         CustomerDataContract CustomerDetails(String Id_Cus);

        [OperationContract]
         [WebInvoke(Method = "DELETE", ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare)]
         void DeleteCustomer(String Id_Cus);

         [OperationContract]
         [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Wrapped,
          UriTemplate = "/InsertCustomer/")]//problem
         void InsertCustomer(CustomerDataContract customerDataContract);

         [OperationContract]
         [WebInvoke(Method = "PUT",ResponseFormat = WebMessageFormat.Json,
          RequestFormat = WebMessageFormat.Json,
          BodyStyle = WebMessageBodyStyle.Bare,
          UriTemplate = "/UpdateCustomer/")]//problem
         void UpdateCustomer(CustomerDataContract customerDataContract);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.8" />
    <httpRuntime targetFramework="4.8" />
  </system.web>
  <system.serviceModel>

    <services>
      <service name="WcfRestFullService.CustomerSevice"  behaviorConfiguration="serviceBehavior">
        <endpoint address=""
                  binding="webHttpBinding"
                  contract="WcfRestFullService.ICustomerSevice"
                  behaviorConfiguration="web"></endpoint>
        </service>
          </services>

    <!--<services>
      <service name="WcfRestFullService.CustomerPreferences"  behaviorConfiguration="serviceBehavior">
        <endpoint address=""
                  binding="webHttpBinding"
                  contract="WcfRestFullService.ICustomerPreferences"
                  behaviorConfiguration="web"></endpoint>
      </service>
    </services>-->


      <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="webHttpBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Configuration.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.DependencyInjection.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Caching.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Options" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.DependencyInjection" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.ComponentModel.Annotations" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.1.0" newVersion="4.2.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.19.0" newVersion="8.0.19.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.EntityFrameworkCore" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.EntityFrameworkCore.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Logging" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.5.0" newVersion="4.0.5.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Google.Protobuf" publicKeyToken="a7d26565bac4d604" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.11.4.0" newVersion="3.11.4.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="BouncyCastle.Crypto" publicKeyToken="0e99375e54769942" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.8.5.0" newVersion="1.8.5.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.EntityFrameworkCore.Relational" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <!--<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.10.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
      </provider>-->
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.EntityFramework, Version=8.0.19.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="MySQLEntities" connectionString="metadata=res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;password=l10ksfnq5h2c;database=chik-chak&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data;
using System.Data.Entity;

using WcfRestFullService.Model;

namespace WcfRestFullService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CustomerSevice" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select CustomerSevice.svc or CustomerSevice.svc.cs at the Solution Explorer and start debugging.
    public class CustomerSevice : ICustomerSevice
    {
        MySQLEntities dc;
        public CustomerSevice()
        {
            dc = new MySQLEntities();
        }

        public List<CustomerDataContract> GetAllCustomer()
        {
            var query = (from a in dc.customers
                         select a).Distinct();

            List<CustomerDataContract> CustomersList = new List<CustomerDataContract>();

            query.ToList().ForEach(x =>
            {
                CustomersList.Add(new CustomerDataContract
                {
                    Id_Cus = Convert.ToString(x.Id_Cus),
                    FirstName_Cus = x.FirstName_Cus,
                    LastName_Cus = x.LastName_Cus,
                    PhoneNum_Cus = x.PhoneNum_Cus.ToString(),
                    Email_Cus = x.Email_Cus,
                });
            });
            return CustomersList;
        }

        public CustomerDataContract CustomerDetails(string Id_Cus)
        {
            CustomerDataContract Cust = new CustomerDataContract();
            try
            {
                var query = (from a in dc.customers
                             where a.Id_Cus.Equals(Id_Cus)
                             select a).Distinct().FirstOrDefault();
                Cust.Id_Cus = query.Id_Cus.ToString();
                Cust.FirstName_Cus = query.FirstName_Cus;
                Cust.LastName_Cus = query.LastName_Cus;
                Cust.PhoneNum_Cus = query.PhoneNum_Cus.ToString();
                Cust.Email_Cus = query.Email_Cus;
            }
            catch (Exception ex)
            {
                throw new FaultException<string>(ex.Message);
            }
            return Cust;
        }

           // DELETE

          public void DeleteCustomer(string Id_Cus) 
          {
              MySQLEntities Cust = new MySQLEntities(); //check the file Model.edmx->ModelContext.tt->MySQLEntitys

              int k = Convert.ToInt32(Id_Cus);
              customer cur = (from n in dc.customers
                           where n.Id_Cus == k
                           select n).First();
              Cust.customers.Remove(cur);
              Cust.SaveChanges();
          }

          //Insert/POST

          public void InsertCustomer(CustomerDataContract customerDataContract)
          {
            MySQLEntities Cust = new MySQLEntities();
            customer cust = new customer();

              cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);
              cust.FirstName_Cus = customerDataContract.FirstName_Cus;
              cust.LastName_Cus = customerDataContract.LastName_Cus;
              cust.PhoneNum_Cus = Convert.ToInt32(customerDataContract.PhoneNum_Cus);
              cust.Email_Cus = customerDataContract.Email_Cus;
              Cust.customers.Add(cust);
              Cust.SaveChanges();
          }

            //Update/PUT
          public void UpdateCustomer(CustomerDataContract customerDataContract)
          {
            //using (CustomerDataContract Cust = new CustomerDataContract())
            using (MySQLEntities Cust = new MySQLEntities()) 
            {
                  customer cust = Cust.customers.Where(n => n.Id_Cus == (Convert.ToInt32(customerDataContract.Id_Cus))).First();

                  cust.Id_Cus =Convert.ToInt32(customerDataContract.Id_Cus);
                  cust.FirstName_Cus = customerDataContract.FirstName_Cus;
                  cust.LastName_Cus = customerDataContract.LastName_Cus;
                  cust.PhoneNum_Cus =Convert.ToInt32(customerDataContract.PhoneNum_Cus);
                  cust.Email_Cus = customerDataContract.Email_Cus;

                  Cust.SaveChanges();
            }
          }
    }
}
namespace WcfRestFullService.Model
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class MySQLEntities : DbContext
    {
        public MySQLEntities()
            : base("name=MySQLEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<customer> customers { get; set; }
        public virtual DbSet<customerpreference> customerpreferences { get; set; }
        public virtual DbSet<dish> dishes { get; set; }
        public virtual DbSet<dishesranking> dishesrankings { get; set; }
        public virtual DbSet<ingridient> ingridients { get; set; }
        public virtual DbSet<order> orders { get; set; }
        public virtual DbSet<restaraunt> restaraunts { get; set; }
        public virtual DbSet<type_dishes> type_dishes { get; set; }
        public object Parameters { get; internal set; }
    }
}

введите описание изображения здесь

введите описание изображения здесь

введите описание изображения здесь

введите описание изображения здесь

введите описание изображения здесь

1 Ответ

1 голос
/ 19 марта 2020

Вы должны знать, что запрос по умолчанию, отправляемый браузером, использует Get HTTP-глагол, в то время как операционный контракт требует DELETE HTTP-глагол.
Нажмите F12, Сеть, чтобы просмотреть детали запроса.
enter image description here
Правильный способ проверки запроса POST/DELETE/PUT - отправить запрос с помощью программного обеспечения PostMan (или другого инструмента, такого как Fiddler).
enter image description here
Определение функции.

[OperationContract]
    [WebInvoke(Method ="DELETE",RequestFormat =WebMessageFormat.Json,ResponseFormat =WebMessageFormat.Json)]
    string GetResult(string id);

Запрос в Fiddler.
enter image description here
Если параметр является составным типом, следует обратить внимание на формат данных запроса.
Подробнее см. По ссылке ниже.
Как вызвать службу RestFul WCF POST для Custom Object с использованием POSTMAN или любого клиентского приложения?
Не стесняйтесь, дайте мне знать, если что-то есть. Я могу помочь с.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...